Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: fork - alarm - output

by ikarius (Initiate)
on Feb 22, 2012 at 17:02 UTC ( [id://955580]=note: print w/replies, xml ) Need Help??


in reply to Re: fork - alarm - output
in thread fork - alarm - output

I came across this looking to do the same thing. Unfortunately, none of the posted solutions worked correctly for me. After testing, I found that it is essential to use "setpgrp(0,0)" in the child, otherwise the kill seems to be ignored. Also, it's unclear what would happen if the child has exec'd something which is hung in an unkillable fashion, so I'll attach a couple of working solutions here. First solution does an alarm and a kill, and doesn't use any fancy libraries.
#!/usr/bin/perl use strict; sub sys_alarm { my ( $cmd_result, $pid ); my $cmd = shift( @_ ); my $timeout = shift( @_ ); eval { local $SIG{ ALRM } = sub { kill -9, $pid; warn "timeout of command \"$cmd\"; killing $pid\n"; }; if ($pid = open(IN, "-|")) { alarm $timeout; while ( <IN>) { $cmd_result .= $_; } close(IN); waitpid $pid, 0; alarm 0; } else { die "cannot fork: $!" unless defined $pid; setpgrp( 0, 0 ); exec( $cmd ); exit; } }; return $cmd_result; } my $x = sys_alarm("sleep 10; echo foo", 5); print "Output: " . $x . "\n\n";
The second solution simply alarms and moves on, and leaves the child to do whatever it will. This may be preferable if there is a potential of unkillable processes. I have the impression that the previous code will not move on if the child does not terminate upon being killed.
#!/usr/bin/perl use strict; sub sys_alarm { my $cmd_result; my $cmd = shift( @_ ); my $timeout = shift( @_ ); eval { local $SIG{ALRM} = sub {die "alarm"}; alarm $timeout; $cmd_result = `$cmd`; alarm 0; }; if ($@) { print STDERR "Command \"$cmd\" timed out.\n"; return undef; } else { return $cmd_result; } } my $x = sys_alarm("sleep 10; echo foo", 5); print "Output: " . $x . "\n\n";

Replies are listed 'Best First'.
Re^3: fork - alarm - output
by ikegami (Patriarch) on Feb 28, 2012 at 08:29 UTC

    You did not "look" to do the same thing, which is why the posted solutions didn't work. You don't want to just kill the child (sh), you want to kill its grandchildren too (sleep). As you've discovered, one does that by creating a new process group for the children (setgrp), and killing the process group (negative signal).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://955580]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-19 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found