in reply to Re: fork - alarm - output
in thread fork - alarm - output
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, $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";
#!/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 |