Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

fork - alarm - output

by chart3399 (Initiate)
on Jun 08, 2010 at 03:30 UTC ( #843610=perlquestion: print w/ replies, xml ) Need Help??
chart3399 has asked for the wisdom of the Perl Monks concerning the following question:

Hello All, I've successfully figured out how to use a pipe / fork / alarm combination to successfully kill a child process after a given time. What I can not figure out is how to redirect the output of the child command to an array in the parent command if the command runs successfully. I've been searching and can not come up with a reasonable answer.
sub runcmd { my @cmd = @_; defined( my $child_pid = fork() ) or die "fork: $!"; if ($child_pid) { #we are parent eval { local $SIG{ALRM} = sub {timeout($child_pid);}; alarm 5; waitpid $child_pid, 0; alarm 0; }; if ($@){print "oops: $@";} } else { #we are child exec @cmd or die "exec: $!"; } } sub timeout { my $pid = @_; kill 'TERM' => $pid; waitpid $pid, 0; die "reaped $pid\n"; }
Anyone have any suggestions? Thanks, Corey

Comment on fork - alarm - output
Download Code
Re: fork - alarm - output
by BrowserUk (Pope) on Jun 08, 2010 at 04:24 UTC

    Aren't you just reinventing Perl's built-in piped-open?

Re: fork - alarm - output
by ikegami (Pope) on Jun 08, 2010 at 04:43 UTC

    Do you want to kill the child if it's inactive for 5s or if it's not done in 5s? The former is quite simple (alarm around a read), but it looks like you want the latter.

    use Time::HiRes qw( time ); use IO::Select qw( ); ... my $buf = ''; if (!eval { my $pid = open(my $fh, '-|', @cmd) or die("Can't create child: $!\n"); my $deadline = time() + 5; my $sel = IO::Select->new($fh); for (;;) { my $time_left = $deadline - time; die("Timeout\n") if $time_left <= 0; next if !$sel->can_read($time_left); my $rv = sysread($fh, $buf, 64*1024, length($buf); die("Can't read from child: $!\n") if !defined($rv); last if !$rv; } waitpid($pid, 0); die("Can't waitpid: $!\n") if $? == -1; die("Child killed by signal ".($? & 0x7F)."\n") if $? & 0x7F; die("Child returned error ".($? >> 8)."\n") if $? >> 8; 1 }) { my $e = $@; kill(TERM => $pid); waitpid($pid, 0); die($e); } ...

    Not tested.

    Update: Added missing my $pid =.

      where did you assigned value to $pid ? :-)
      You can also miss some data, for example @cmd can handle TERM signal and print partial result. @cmd also can block/not handle TERM signal (or in some case, cannot be KILLed).

      Sample:

      use strict; use warnings; use IO::Pipe; use constant TIMEOUT => 5; my @cmd = (); my $pipe = new IO::Pipe; if (my $pid = fork) { $pipe->reader; $SIG{ALRM} = sub { kill TERM => $pid; $SIG{ALRM} = sub { kill KILL => $pid; $SIG{ALRM} = sub { close $pipe; }; alarm TIMEOUT; }; alarm TIMEOUT; }; $SIG{CHLD} = sub { while (my $wait = wait) { alarm 0 if $wait == $pid; } }; # alarm here if you want force child to quit after TIMEOUT # alarm TIMEOUT; while (my $from_child = <$pipe>) { # alarm here if you want to interrupt child after inactivity # (no output lines) # alarm TIMEOUT; # ... }; alarm (0); close $pipe; } elsif (defined $pid) { # Child $pipe->writer; open STDOUT, '>&', $pipe; exec @cmd; } else { die "Fork failed: $!\n"; }

        where did you assigned value to $pid ? :-)

        oops! open returns it. I'll fix it.

        You can also miss some data, for example @cmd can handle TERM signal and print partial result.

        Change the die into a warn if you want to handle partial results.

        Actually, you're the one who misses data. <> is a buffered read, so you'll lose whatever's in the buffer if a timeout occurs. Or maybe it's recoverable by doing another read after the timeout occurs?

        @cmd also can block/not handle TERM signal (or in some case, cannot be KILLed).

        The killing mechanism can be expanded to add a kill KILL => $pid; if the process doesn't end after a certain time. That's easy, and it's outside of what the OP was asking about.

      The former is quite simple (alarm around a read),
      I would most likely opt for a 4-arg select in that case.
        That's what I'm using now for the other case. It would require twice as much code as needed, but it's definitely an option. I'm not fond of using alarm, so I might very well end up using select as well.
      I want to kill the process if it is not done in X amount of seconds.

        Isn't this just easier?

        #! perl -slw use strict; my $pid = open IN, '-|', qq[ perl -E"say scalar localtime, sleep 1 for 1.. $ARGV[0]" ] or die $!; eval { local $SIG{ ALRM } = sub { kill -9, $pid; warn 'timeout'; }; alarm 5; waitpid $pid, 0; alarm 0; }; while( <IN> ){ print; } __END__ c:\test>junk 3 Tue Jun 8 15:33:04 20101 Tue Jun 8 15:33:05 20101 Tue Jun 8 15:33:06 20101 c:\test>junk 4 Tue Jun 8 15:33:09 20101 Tue Jun 8 15:33:10 20101 Tue Jun 8 15:33:11 20101 Tue Jun 8 15:33:12 20101 c:\test>junk 5 timeout at C:\test\junk.pl line 11.
        ol>

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
      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";

        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
Node Status?
node history
Node Type: perlquestion [id://843610]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (15)
As of 2014-07-24 13:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (160 votes), past polls