in reply to SIGCHLD and return codes

From perlipc:
On most Unix platforms, the `CHLD' (sometimes also known as `CLD') signal has special behavior with respect to a value of `'IGNORE''. Setting `$SIG{CHLD}' to `'IGNORE'' on such a platform has the effect of not creating zombie processes when the parent process fails to `wait()' on its child processes (i.e. child processes are automatically reaped). Calling `wait()' with `$SIG{CHLD}' set to `'IGNORE'' usually returns `-1' on such platforms.
I would install a signal handler that calls wait to reap the child process and get its exit status, as in this excerpt, adapted from perlipc:
sub REAPER { $waitedpid = wait; # now status is in $? print "reaped pid $waitedpid, exited with status ", $? >> 8, "\n"; # loathe sysV: it makes us not only reinstate # the handler, but place it after the wait $SIG{CHLD} = \&REAPER; } $SIG{CHLD} = \&REAPER;


--sacked

Replies are listed 'Best First'.
Re: (sacked) Re: SIGCHLD and return codes
by shrubbery (Acolyte) on Nov 17, 2001 at 00:30 UTC
    Looks like that works but how do I return $? from the handler?
      Use a hash:
      # once again, adapted from perlipc use vars qw( %status ); # our, if you prefer sub REAPER { $waitedpid = wait; # now status is in $? $status{ $waitedpid } = $? >> 8; # loathe sysV: it makes us not only reinstate # the handler, but place it after the wait $SIG{CHLD} = \&REAPER; } $SIG{CHLD} = \&REAPER; while ( my ($k,$v) = each %status ) { print "pid $k\tstatus $v\n" }

      --sacked