cmv has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks-

Consider the attached script.

I believe I should get 2 lines of output from each of the 2 children. Running this under perl, v5.8.8 built for darwin works fine. However, when I run it under perl, v5.8.8 built on solaris, strange things happen:

TRY1: CHILD1: My pid = 21727 CHILD1: 1: THIS IS YOUR PARENT SPEAKING... CHILD1: 2: THIS IS YOUR PARENT SPEAKING... CHILD1: 1: THIS IS YOUR PARENT SPEAKING... CHILD1: 2: THIS IS YOUR PARENT SPEAKING... CHILD2: My pid = 21728 CHILD2: 1: THIS IS YOUR PARENT SPEAKING... CHILD2: 2: THIS IS YOUR PARENT SPEAKING... TRY2: CHILD1: My pid = 22218 CHILD1: 1: THIS IS YOUR PARENT SPEAKING... CHILD1: 2: THIS IS YOUR PARENT SPEAKING... CHILD2: My pid = 22219 CHILD2: 1: THIS IS YOUR PARENT SPEAKING... CHILD2: 2: THIS IS YOUR PARENT SPEAKING... CHILD2: 1: THIS IS YOUR PARENT SPEAKING... CHILD2: 2: THIS IS YOUR PARENT SPEAKING...
First, why do I sometimes get 4 lines from one “bad” kid??

Second, why does the “bad” kid sometimes change?

Any pointers very much appreciated!

Thanks

-Craig

#!/opt/exp/bin/perl use strict; use warnings; foreach my $i (1..2) { _spawn($i); } 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: $_" }; exit } } __DATA__ 1: THIS IS YOUR PARENT SPEAKING... 2: THIS IS YOUR PARENT SPEAKING...

Replies are listed 'Best First'.
Re: Stuttering Children
by dave_the_m (Monsignor) on Sep 19, 2014 at 19:00 UTC
    I can't remember the exact details; but IIRC on solaris the child needs to call POSIX::_exit() rather than exit() to stop shared file handles being messed up somehow.

    Dave.

      dave_the_m

      Thanks for the suggestion, but I tried that and it doesn't seem to make a difference.

      A colleague of mine discovered this bug report however it is listed for linux.

      I'm still mining the web for help on this, any pointers appreciated.

      Thanks

      Craig

        Well you could completely avoid the issue by reading all of __DATA__ into a var before forking (assuming its a DATA issue)