Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: How to optionally kill a child, and capture status if not killed.

by pc88mxer (Vicar)
on Apr 25, 2008 at 20:48 UTC ( #682916=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://682916]
help
Chatterbox?
[karlgoethebier]: jedikaiti: íHola! Don't turn out the stars tonight
[jedikaiti]: Howdy! Is that link SFW?
[karlgoethebier]: jedikaiti: safe for work? it's youtube...
[jedikaiti]: Which could be anything
[karlgoethebier]: karlgoethebier: some say shockwave isn't safe for work. no risk, no fun ;-)

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2017-06-27 15:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (609 votes). Check out past polls.