in reply to Re^5: Stuttering Children
in thread Stuttering Children
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: Stuttering Children (shared seek)
by cmv (Chaplain) on Sep 24, 2014 at 12:44 UTC | |
by tye (Sage) on Sep 24, 2014 at 13:08 UTC |