reaper - support for reaping child processes via $SIG{CHLD}


reaper - support for reaping child processes via $SIG{CHLD}


  use reaper qw( reaper reapPid pidStatus );
  my $pid = fork;
  if ( $pid == 0 ) { # child
    exec $some_command;
  reapPid ( $pid );
  if ( defined(my $exit = pidStatus($pid)) ) {
    # child exited, check the code...


perl has an annoying little problem with child processes -- well, it is not actually a problem specific to perl, but it is somewhat more difficult with perl: reaping child processes after they exit so they don't hang around as zombies forever, and doing it in a way that accurately captures the exit code of the child.

The right way to do it is to install a $SIG{CHLD} handler which calls waitpid to reap the child process and store $? at that point. But the problem is that different modules may step on each other in installing their on version of the handler, there's no uniform way of doing this.

For some situations, a local $SIG{CHLD} handler is sufficient, but often times the handler is no longer in scope at the time the child process exits -- since the child may exit at any time. The local handler is dynamically scope, not lexically, so it depends entirely what subroutine is being executed at the time the signal is caught.

So the reaper module provides a $SIG{CHLD} handler that can be installed globally as well as locally. It also supports chaining of signal handlers, meaning it will not just replace an existing $SIG{CHLD} handler. It still requires applications to do the right thing in using this module and not installing their own versions. At least it provides a consistent implementation that can be shared between various modules.



Jeremy Slade <>

 reaper - support for reaping child processes via $SIG{CHLD}