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; }