SIG{CHLD} problems

Kevin Buettner kev at
Mon Jul 17 18:41:49 CDT 2000

On Jul 17, 11:49am, Mike Cantrell wrote:

> Inside the child's code, I've got a glob() that keeps triggering the
> SIG{CHLD} subroutine.  The interesting thing is that the error
> states that the glob dumped core but it didn't:
>     glob failed (child exited with status -1, core dumped) at ./manager line 152, <_GEN_0> chunk 1.

I'm not sure what this is about, but are you sure you want to be catching
SIGCHLD in the child process?  It seems to me that you might want to


just as soon as you know that you're in the child process.

> I know that sysetm(), backticks, etc cause forks but they should be
> checking for their own children.  Could it be possible that glob()
> forks but doesn't clean up after itself?  Has anyone else had
> similar problems?

I'm sure glob does clean up after itself when $SIG{CHLD} is set to the
default handler.  I too find it a bit surprising that you're seeing the
SIGCHLD signals from system, glob, etc... but after thinking about it
for a moment or two, it's not *that* surprising.

When one calls waitpid() in C, you don't know in advance what it will
return; i.e, it could return you the pid of the child that you're
expecting to see or some other child that you started up a while ago
and didn't expect to see exiting at this point.  In other words,
there's no way for Perl's system(), et al. to call waitpid() only on
the processes that they fork off.

> Here's the assigned routine to $SIG{CHLD}: (yep, straight outta the cookbook)
> sub REAPER {
>     my $pid;
>     $pid = waitpid(-1, &WNOHANG);
>     if ($pid == -1) {
>         # no child waiting.  Ignore it.
>     } elsif (WIFEXITED($?)) {
>         print "Process $pid exited.\n";
>     } else {
>         print "False alarm on $pid.\n";
>     }
>     $SIG{CHLD} = \&REAPER;          # in case of unreliable signals
> }

You might wish to modify this a little bit so that you keep track of
the pids of the child processes that you explicitly fork off.  Then, in
your REAPER sub, you can check to see if it's one that you started and
if so check the exit code...

More information about the Phoenix-pm mailing list