[Melbourne-pm] $SIG{CHLD}

Daniel Pittman daniel at rimspace.net
Sun May 4 01:23:54 PDT 2008


Scott Penrose <scottp at dd.com.au> writes:

> To capture exit values of forked daemons and not end up with a set of
> zombie processes, we need to set $SIG{CHLD} to either 'ignore' or do
> it fully. However once you do you loose the ability to capture the
> return value of a 'system' call - unless you do it the hard way
> (record in a hash the value by process id and then use that and remove
> it after your system call).
>
> Anyway to get this all written down I wrote it on my site, but also as
> partly an open question - is there a better way of doing 'system'
> which does not depend on changes to $SIG{CHLD} or other solutions:
>
> http://scott.dd.com.au/wiki/SIG_CHLD
>
> So anyone know of one?

Well, my very strong preference for doing /anything/ related to child
processes is to use the IPC::Run module.  This wraps up a whole bunch of
stuff from a dead simple 'run this' through to a complex 'write to and
read from a filter, looking for specific out' and 'build a pipeline'
stuff.

The interface is sensible, light-weight, and the tool scales very well
from start to finish.  

It also, on investigation, handles $? appropriately internally so that
it does the right stuff as far as I can tell.

You may want to look into it, although it isn't always perfect:

http://www.perlmonks.org/?node_id=674306

Also, not always playing nice with SIG{CHLD} handlers, although this is
very much in the "point gun at foot, pull trigger" style:

http://www.depesz.com/index.php/2008/02/07/failing-ls/

(Answer for those who don't want to read the code below the cut)

Anyway, it should play nicely with existing SIG{CHLD} handlers that are
written such that they don't break random library code and the like, and
certainly beats hand-coding everything.

Regards,
        Daniel

I have not actually tried the Perl co-process support, but everything
else seems solid enough.



The answer is that the install SIG{CHLD} handler will wait for and
collect the exit status from *everything*, which means that my the time
the IPC::Run code in _cleanup (IPC/Run.pm:3157) is called the exit
status is already gone as is the zombie process.

See the manual page (and error code) for the waitpid system call, 


More information about the Melbourne-pm mailing list