The REAPER is part of the parent. So having waitpid() in only one place means having it only in REAPER().
Some replacement code to this foreach (@all_kids) loop:
You can also just sleep(20) and print @dead_kids. The focus right now should be on getting this to work without the CTL-C complication and then add that later.while (@dead_kids <10) { sleep(1); } print "all kids dead .. @dead_kids\n";
Update: Oh, I would also add use warnings; either by that statement, or a -w in the hash bang line. This has nothing to do with your current woes, but there are run time checks with warnings enabled that are useful. I leave them on unless some rare, very rare performance or other reason indicates otherwise.
Another Update: Was able to test some code...for some reason, when SIGCHLD happens, this causes the sleep to end. I don't know why. So there is a loop to restart the sleep every 1 seconds. try this code...have to run to an appointment...oh, exit 1 was caused by missing parens in while statement in the reaper. The sleep issue is the real puzzle here.
update: added readmore tag - updated code in later post
<\readmore>#!/usr/bin/perl -w use strict; use POSIX qw(:signal_h :errno_h :sys_wait_h); $SIG{CHLD} = \&REAPER; my (@all_kids, @dead_kids); my @array = qw(a b c d e f g h); for (1 .. 10) { die "Bad Fork! $!\n" if ( !defined(my $pid = fork()) ); if ($pid ==0) #I'm child { my $t=int(rand(5))+1; print "@array sleeping $t secs\n"; sleep ($t); exit (0); } push (@all_kids, $pid); #I'm parent print "Starting another child process - $pid.\n"; } print "All child processes have started...\n"; print "kids: @all_kids\n"; my $count=0; while ($count <20) { sleep(1); $count++; } print "dead kids: @dead_kids\n"; print "All child processes are finished???\n"; sub REAPER { my $pid; while ( ($pid = waitpid(-1, WNOHANG)) > 0) #missed paren before { print "Process $pid exited.\n"; push @dead_kids, $pid; } return; }
In reply to Re^3: Monitoring Child Process
by Marshall
in thread Monitoring Child Process
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |