Re: Killing a process that takes too long
- From: jen.spinney@xxxxxxxxx (Jen Spinney)
- Date: Tue, 21 Nov 2006 16:06:33 -0500
On 11/21/06, Jen Spinney <jen.spinney@xxxxxxxxx> wrote:
On 11/21/06, Tom Phoenix <tom@xxxxxxxxxxxxxx> wrote:
> On 11/21/06, Jen Spinney <jen.spinney@xxxxxxxxx> wrote:
>
> > I want to make a system call, and then kill the process if it takes
> > too long.
>
> > So, if I do a ps -af, I can see that my perl script is a goner, but
> > the process spawned from the system call is still alive.
>
> Yes; if you use system() to start a sub-process, you're letting perl
> manage it; so there's no way to get the process-ID.
>
> You may instead use fork and exec; this lets you use the process-ID to
> manage the process directly. Be sure to use wait or waitpid to reap
> the completed child process, so as not to leave zombies.
>
> Is that what you needed? Hope this helps!
>
> --Tom Phoenix
> Stonehenge Perl Training
>
Thanks Tom!
I replaced the system call with fork and exec and it works just the
way I want it to:
use warnings;
use strict;
my $pid;
eval {
local $SIG{ALRM} = sub {
print "Timed out\n";
kill 'INT', $pid;
die 'alarm';
};
alarm 5;
if ($pid = fork)
{
waitpid ($pid, 0);
}
else
{
exec ('sleep 45');
}
alarm 0;
};
die if $@ && $@ !~ /alarm/;
print "Exited normally.\n";
__END__
For my actual program, I had to do a bit more work because I have
semicolons and other shell stuff in the command (which seems not to do
so well with exec?), but I figured out a workaround.
So, thanks again!
- Jen
Sorry for top-posting my last post. I ran into a bit of snag when
replacing system with exec. If you replace 'sleep 45' in my last post
with "perl -e 'while (1) {print 1}' | tee test.txt", the pipe really
messes things up. Can anyone give me guidance as to how I should set
up a pipe when using fork () and exec () to replace system ()? Do I
have to call pipe ()? I'm a beginner programmer, so this low-level
stuff is somewhat scary and foreign to me.
- Jen
.
- Follow-Ups:
- Re: Killing a process that takes too long
- From: D. Bolliger
- Re: Killing a process that takes too long
- References:
- Killing a process that takes too long
- From: Jen Spinney
- Re: Killing a process that takes too long
- From: Tom Phoenix
- Re: Killing a process that takes too long
- From: Jen Spinney
- Killing a process that takes too long
- Prev by Date: Re: Hello to Perl World
- Next by Date: RE: How to make a perl program to an exe file
- Previous by thread: Re: Killing a process that takes too long
- Next by thread: Re: Killing a process that takes too long
- Index(es):
Relevant Pages
|