how to TIMEOUT a system()?

W

Weirong Zhu

A beginner question!
I have a piece of code

#!/usr/bin/perl -w

print "$$ \n";


eval{
local $SIG{ALRM} = sub { die "Timeout Alarm" };
alarm 5;
$rt = system("/home/user/tmp/long");
alarm 0;
};
if ($@ = ~/Timeout Alarm/) {
print "**** Time Out\n";
}

blahblah......

----------------------------

when it finishes, the child process invoked by system() does not
terminated. But I want to kill it when it's timed out. How to do this?
The reason that I want to use the system(), because I want to get the
return value when it's not timed out.
 
B

Ben Morrow

eval{
local $SIG{ALRM} = sub { die "Timeout Alarm" };
alarm 5;
$rt = system("/home/user/tmp/long");
alarm 0;
};
if ($@ = ~/Timeout Alarm/) {
print "**** Time Out\n";
}

when it finishes, the child process invoked by system() does not
terminated. But I want to kill it when it's timed out. How to do this?
The reason that I want to use the system(), because I want to get the
return value when it's not timed out.

You want to use the technique in 'Safe Pipe Opens' in perlipc to get
the pid of the child process, and then kill it in the signal handler.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top