Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: code explanation

by tmharish (Friar)
on Mar 06, 2013 at 14:46 UTC ( #1022015=note: print w/replies, xml ) Need Help??

in reply to code explanation

First off please use code tags so your code is readable - I have done this for you this time around.

my $pid = fork; if ($pid > 0){ eval{ local $SIG{ALRM} = sub {kill 9, -$PID; die "TIMEOUT!"}; alarm $num_secs_to_timeout; waitpid($pid, 0); alarm 0; }; } elsif($pid == 0){ setpgrp(0,0); exec('echo blahblah | program_of_interest'); exit(0); }

Documentation for fork will show you that the fork call returns the pid of the forked process to the parent and 0 to the child. This is used above to run the parent through the if and the child through the else above.

The following should help you understand the rest:

  1. setpgrp
  2. exec
  3. Piped redirection of input and output
  4. How to use Alarm to timeout

This code essentially creates a process which executes a non-perl program on a timeout.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1022015]
[LanX]: talking about politics. ..
[LanX]: yet another reason to miss yapc na this year
[shmem]: missing is an euphemism
[Discipulus]: a piano et¨de can be harmfull, anyway
[shmem]: grandola vila morena *sing*

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2017-04-29 22:33 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (534 votes). Check out past polls.