From: Nicholas Clark Date: Mon, 10 Sep 2001 22:00:40 +0000 (+0100) Subject: avoiding hoardes of zombies X-Git-Tag: accepted/trunk/20130322.191538~30309 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=816229cf4a3690b9abc75d05aaabf5930497cf24;p=platform%2Fupstream%2Fperl.git avoiding hoardes of zombies Message-ID: <20010910220040.C1512@plum.flirble.org> p4raw-id: //depot/perl@11987 --- diff --git a/pod/perlipc.pod b/pod/perlipc.pod index a1df3e4..56816b1 100644 --- a/pod/perlipc.pod +++ b/pod/perlipc.pod @@ -121,11 +121,15 @@ signal handlers like this: $SIG{CHLD} = \&REAPER; # now do something that forks... -or even the more elaborate: +or better still: use POSIX ":sys_wait_h"; sub REAPER { my $child; + # If a second child dies while in the signal handler caused by the + # first death, we won't get another signal. So must loop here else + # we will leave the unreaped child as a zombie. And the next time + # two children die we get another zombie. And so on. while (($child = waitpid(-1,WNOHANG)) > 0) { $Kid_Status{$child} = $?; } @@ -724,10 +728,13 @@ go back to service a new client. my $waitedpid = 0; my $paddr; + use POSIX ":sys_wait_h"; sub REAPER { - $waitedpid = wait; + my $child; + while (($waitedpid = waitpid(-1,WNOHANG)) > 0) { + logmsg "reaped $waitedpid" . ($? ? " with exit $?" : ''); + } $SIG{CHLD} = \&REAPER; # loathe sysV - logmsg "reaped $waitedpid" . ($? ? " with exit $?" : ''); } $SIG{CHLD} = \&REAPER; @@ -881,10 +888,13 @@ to be on the localhost, and thus everything works right. my $waitedpid; + use POSIX ":sys_wait_h"; sub REAPER { - $waitedpid = wait; + my $child; + while (($waitedpid = waitpid(-1,WNOHANG)) > 0) { + logmsg "reaped $waitedpid" . ($? ? " with exit $?" : ''); + } $SIG{CHLD} = \&REAPER; # loathe sysV - logmsg "reaped $waitedpid" . ($? ? " with exit $?" : ''); } $SIG{CHLD} = \&REAPER;