7stud has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

After fork()'ing, I am unable to catch a CHLD signal in the parent. In my example, the child process does nothing and calls die(). The parent would like to know that the CHLD process has terminated.

use strict; use warnings; use 5.010; my $pid = fork; if (!defined $pid) { die "Couldn't fork: $!"; } if ($pid) { #then in parent local $SIG{CHLD} = sub {say 'caught it';}; say 'hello world'; } else { #then in child die; } --output:-- hello world

My expected output:

caught it hello world #or hello world caught it

Replies are listed 'Best First'.
Re: why can't I catch CHLD signal?
by GrandFather (Saint) on Mar 22, 2010 at 02:51 UTC

    Any other issues aside, what guarantees that local $SIG{CHLD} = sub {say 'caught it';}; executes before the child's die?


    True laziness is hard work
Re: why can't I catch CHLD signal?
by 7stud (Deacon) on Mar 22, 2010 at 03:03 UTC

    I figured it out: the parent is exiting before the child. I need to call wait() or waitpid() in the parent.

    --Thanks Grandfather. Points awarded.