JBAS-479 clean up forked children in throttle_disbatcher.pl
authorBill Erickson <berickxx@gmail.com>
Fri, 27 Feb 2015 16:00:12 +0000 (08:00 -0800)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
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>
KCLS/linking/throttle_disbatcher.pl

index 13e6a38..12bee51 100755 (executable)
@@ -28,6 +28,7 @@
 
 use Getopt::Long;
 use Time::localtime;
+use POSIX qw/:sys_wait_h/;
 
 $SIG{CHLD} = \&sig_handler;
 
@@ -152,8 +153,12 @@ sub dispatch {
 }
 
 sub sig_handler {
-    $running--;
-    $count++;
+    # clean up child processes
+    while ((my $child = waitpid(-1, WNOHANG)) > 0) {
+        print "reaping child $child\n";
+        $running--;
+        $count++;
+    }
 }
 
 __END__