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"; }

In reply to Children aren't being reaped by halecommarachel

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.