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

Hi Monks,

The below script works if you run it about 3 times in a row on a red hat 5.9 linux machine, but after that, after 7 children have been forked, the children stop being reaped and it gets stuck in the while loop. Please help.

#!/tools/xgs/perl/5.8.8/bin/perl -w use strict; use lib '/home/fisusr/perl-packages/'; use Benchmark; use Getopt::Long; use Data::Dumper; use Parallel::ForkManager; use POSIX ":sys_wait_h"; $| = 1; my %children; my $max = 7; $SIG{CHLD} = \&reaper; sub reaper { local ($!, $?); while(my $pid = waitpid(-1, WNOHANG)){ return if $pid == -1; return unless defined $children{$pid}; delete $children{$pid} and print "deleted $pid\n"; print "SIG{CHLD}: PID $pid , ", kill(0 => $pid) ? 'NOT ' : '', + "reaped\n"; } $SIG{CHLD} = \&reaper; } my @Array=qw(1 2 3 4 5 6 7 8 9 10); foreach my $num (@Array){ print "---$num---\n"; print Dumper(%children); while (keys %children >= $max) { print "Haaaanging\n"; sleep 1; } my $child_pid = fork(); if ($child_pid > 0) { $SIG{CHLD} = \&reaper; $children{$child_pid} = 1; print "There are currently ", scalar(keys %children), " child +processes\n"; print "Parent here with child $child_pid\n"; next; }elsif($child_pid ==0){ printRand(); # exit; }else{ die "Could not fork: $!\n" } exit; } sub printRand{ my $rand=rand(30); print "$rand\n"; }

Replies are listed 'Best First'.
Re: Children aren't being reaped
by taint (Chaplain) on Jun 04, 2014 at 23:43 UTC
    OK. I had to install Parallel::ForkManager to run your script.

    Do note, for reference, this is on a BSD box / 3cores @3.2Ghz & 128Mb ram
    this is output from 4 passes of the script:

    Immediately upon executing PASS_4, I received the following:
    Bizarre copy of ARRAY in anonlist at /usr/local/lib/perl5/site_perl/5. +12/mach/Data/Dumper.pm line 590.
    I'm still examining the output. But felt it worth posting, for others who can't/won't run it, to examine.

    --Chris

    ¡λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

      Well. I haven't quite nailed it down. But something's bugging me about this section:

      foreach my $num (@Array){ print "---$num---\n"; print Dumper(%children); while (keys %children >= $max) { print "Haaaanging\n"; sleep 1; }
      Just thought I'd mention it.

      --Chris

      ¡λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

Re: Children aren't being reaped
by perlfan (Parson) on Jun 05, 2014 at 12:58 UTC
    With all the hub bub regarding each and the fact that Dumper uses it, it is possible that since this is being shared among children that there is some side effect at play. Try not using Dumper to show the state of %childtren, and see if that helps.