It is more than that. The fork()ed processes also share their file offsets, and not just initially. For example, add a few sleeps to get:

seek( DATA, 0, 1 ) if @ARGV; foreach my $i (1..2) { _spawn($i); sleep 1; } sleep 4; sub _spawn { my $id = shift || die "Missing id\n"; my $pid = fork(); defined $pid or die "bad open (pipe/fork): $!\n"; # Have parent/child run their respective code... if ( $pid ) { # PARENT CODE... return; } else { # CHILD CODE... print "CHILD$id: My pid = $$\n" ; while(<DATA>) { print "CHILD$id: $_"; sleep 3 }; exit } } __DATA__ 1: THIS IS YOUR PARENT SPEAKING... 2: THIS IS YOUR PARENT SPEAKING...

And I get the following output:

CHILD1: My pid = 31325 CHILD1: 1: THIS IS YOUR PARENT SPEAKING... CHILD2: My pid = 31326 CHILD1: 2: THIS IS YOUR PARENT SPEAKING...

(Note how each child outputs just one of the two lines.)

That seek line I added in case one needs to reset the buffering that you talked about. But it isn't needed on my system (which makes me suspect that Perl does the equivalent for the DATA handle, either when it first sets it up or when about to fork; I haven't searched for other evidence of either behavior, though).

I doubt Solaris is different on this point (I don't recall Advanced Programming in the UNIX Environmnet calling this out as only applying to System V descendents, for example). So my guess would be that both children producing output can be explained by the input lines being buffered in the DATA handle and not being flushed before the children are forked.

My even wilder guess is that Perl is trying to do that seek line when fork is called, but, due to some quirk of whichever I/O layer Perl gets built with there, the effect is instead like the equivalent sysseek call, resetting the file position but not flushing the input buffer of the DATA file handle.

So each child reads the two buffered lines from the DATA file handle and then one of them manages to (re)read the two lines from the underlying file descriptor (moving its position so the other child just finds EOF). But I can't explain how that could lead to the repeated output appearing last as in the root node's "TRY2".

But perhaps some of that might help or just prompt somebody else to figure out what is actually going on.

- tye        


In reply to Re^6: Stuttering Children (shared seek) by tye
in thread Stuttering Children by cmv

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.