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


in reply to How to optionally kill a child, and capture status if not killed.

The value of $? is associated with the last successful waitpid call. In your case things are complicated because you are calling waitpid in two places: your main program and the SIGCHLD handler. I wouldn't be surprised if the SIGCHLD handler is causing $? to get clobbered (with respect to the waitpid call in your main program.)

I would get rid of the SIGCHLD handler, and structure your code to look something like this:

my $script_pid = fork(); ...launch script process... my $abort_pid = fork(); ...launch abort button process.., my $pid = wait; # see who finishes first my $st = $?; # save status if ($pid == $script_pid) { # script finished kill 9, $abort_pid; } else { # assume $pid == $abort_pid, or check it # abort button finished kill 9, $script_pid; }

Replies are listed 'Best First'.
Re^2: How to optionally kill a child, and capture status if not killed.
by joelr (Novice) on Apr 25, 2008 at 21:49 UTC
    Thanks pc88mxer!

    Great tips.

    The key was to wait for either process to finish in the parent, and to use "exec" instead of "system" to not consume the status read by "$?". Before that, I could not get "$?" to reflect the status.

    I will update the example for any future searches.