Re: Killing a process that takes too long



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
.



Relevant Pages

  • Re: Killing a process that takes too long
    ... I replaced the system call with fork and exec and it works just the ... kill 'INT', $pid; ... if you use systemto start a sub-process, you're letting perl ...
    (perl.beginners)
  • Re: Fork, exec - setsid?
    ... So I came up with the need to fork some processes away ... > of those or the spawned shell. ... The shell will have the pid that forkreturned to the parent. ... > How do I kill the forked away programms without knowing their PID? ...
    (comp.lang.perl.misc)
  • Killing process after time limit
    ... the problem of killing process after expired time limit ... check pid, say, every 10 seconds ... set KillingScript ... eval exec CmdLine ...
    (comp.lang.tcl)
  • Re: Problem with 3-arg open of pipe
    ... SK> If you're looking to avoid the shell, you can always fork and exec, ... SK> passing a LIST to the latter: ... SK> if ($pid) { ...
    (comp.lang.perl.misc)
  • Re: [PATCH 3/3] coredump: zap_threads() must skip kernel threads
    ... Otherwise we "kill" this thread erroneously (for example, it can not fork or ... exec after that), and the coredumping task stucks in the TASK_UNINTERRUPTIBLE ...
    (Linux-Kernel)