Skip to content

Instantly share code, notes, and snippets.

@worldmind
Created May 18, 2012 11:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save worldmind/2724846 to your computer and use it in GitHub Desktop.
Save worldmind/2724846 to your computer and use it in GitHub Desktop.
Perl $SIG{CHLD} handler
# see http://perldoc.perl.org/perlipc.html#Signals
# http://www.calpoly.edu/cgi-bin/man-cgi?wait+2
# and Perl Cookbook, part 16.19. Avoiding Zombie Processes
sub REAPER {
local ($!, $?);
while ( (my $pid = waitpid(-1, WNOHANG)) > 0 ) {
say "Process $pid send CHLD with status $?";
if ( WIFEXITED($?) or WIFSIGNALED($?) ) {
if ( $kids{$pid} ) {
process_event_from_child( $kids{$pid}, 'chld' );
}
else {
say "ERROR: CHLD signal from unknown PID $pid";
}
}
else {
say "False CHLD alarm $? on $pid";
}
}
$SIG{CHLD} = \&REAPER;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment