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 | |
by taint (Chaplain) on Jun 05, 2014 at 00:19 UTC | |
|
Re: Children aren't being reaped
by perlfan (Parson) on Jun 05, 2014 at 12:58 UTC |