When registering a sig handler for CHLD, we have to waitpid
for child procs to clean them up or defunct procs will proliferate.
Also update counts with each reaped child, since that's the only
accurate way to know how many children have completed their tasks.
See http://perldoc.perl.org/perlipc.html#Signals SIGCHLD
Signed-off-by: Bill Erickson <berickxx@gmail.com>
use Getopt::Long;
use Time::localtime;
+use POSIX qw/:sys_wait_h/;
$SIG{CHLD} = \&sig_handler;
}
sub sig_handler {
- $running--;
- $count++;
+ # clean up child processes
+ while ((my $child = waitpid(-1, WNOHANG)) > 0) {
+ print "reaping child $child\n";
+ $running--;
+ $count++;
+ }
}
__END__