Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Getting PID of process forked by backticks

by paulski (Beadle)
on Jun 08, 2005 at 04:30 UTC ( [id://464552]=note: print w/replies, xml ) Need Help??


in reply to Re: Getting PID of process forked by backticks
in thread Getting PID of process forked by backticks

What happens if the forked process blocks?

From PERL's point of view, backticks block until the process returns. But from the OS point of view processes are running concurrently.

If I know the PID (returned by open), I can kill the process after a certain amount of time has elasped by catching an ALRM signal. The signal will interrupt the control flow and the signal handler can kill the "timed out" process.

  • Comment on Re^2: Getting PID of process forked by backticks

Replies are listed 'Best First'.
Re^3: Getting PID of process forked by backticks
by eyepopslikeamosquito (Archbishop) on Jun 08, 2005 at 09:33 UTC

    If that's what you want, I'd recommend using fork. I did something similar a while back, using fork, as described in Timing and timing out Unix commands. If you get it working without using fork, I'd be interested in seeing your code.

      I had a similar requirement, and came up with this:

      my %p; sub killchildren { my $ppid = shift; chomp(my $out = `ps -o pid --no-headers --ppid $ppid`); for (split /\n/, $out) { killchildren($_); kill 9, $_; } } sub homicide { killchildren($$); $p{fg}{timeout} = 1; } sub fg { my ($cmd, $timeout) = @_; $p{fg}{timeout} = 0; my $out; eval { local $SIG{ALRM} = q(homicide); alarm $timeout; $out = `$cmd`; alarm 0; }; return $out unless $p{fg}{timeout}; return q(TIMEOUT); } # # TEST # my $out = fg("echo Hi; sleep 5", 1); print "Out = $out\n"; $out = fg("echo Bye; sleep 5", 10); print "Out = $out\n";

      The test example had the following output:

      ~/src$ ./a.pl
      Out = TIMEOUT
      Out = Bye
      

Log In?
Username:
Password:

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

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

    No recent polls found