Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

kill process after time-out

Status
Not open for further replies.

phyllisdillon

Programmer
Joined
Jan 22, 2007
Messages
4
Location
AT
Hello,
I want to start a program from my perlscript and put the result in a database. The following code is run in a loop, changing param1 and param2:

Code:
my @args=("program.exe", "--param1", "1", "--param2", "2");
system( @args );  
my $exitcode=$?/256;

It works fine, but sometimes "program.exe" hangs, which causes the whole script to hang. What I need is to kill "program.exe" when a certain time has passed. Any tip on how I can achieve this?
 
Any idea why it hangs? It's usually better to treat the problem, rather than the symptom...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
No, it is not my program, so I cannot influence that... I have to accept the fact that it will sometimes hang and try to deal with it somehow.
 
Hey Phyllis,

I think you need to look at the fork() function.

This will enable you to:

[li]Start a background process[/li]
[li]Watch that background process from your parent script[/li]
[li]Stop the background process with kill() if need be[/li]



Mike

The options are: fast, cheap and right - pick any two.. [orientalbow] & [anakin]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884
 
Hey!
Thanks for the help! One question about fork() & kill(). I have tried it this way now, giving program.exe 2 minutes to finish:

Code:
my $child_PID = fork();
	if ( $child_PID ) {
		sleep 60;
		if( kill 0, 0 )
		{
			sleep 60;			
			print "process killed, timeout" if( kill 1, 0 ); 
		}
	} elsif ( $child_PID == 0 ) {
		($s_start, $usec_start) = gettimeofday();
		my @args=("program.exe", "--param1", "1", "--param2", "2");
		system( @args );
		$exitcode = $?/256;
		($s_end, $usec_end) = gettimeofday();
		exit(0);
	} else {
		die "couldn’t fork: $!\n";
	}

and it seems to work, most of the times. However sometimes I get an errormessage from program.exe that an instance is already running... So my question is, when I use fork() and start program.exe, does it run in the same process space or does program.exe get a PID of its own? Does (kill 1, 0) also kill program.exe correctly?
 
MillerH might well be right, an alarm is worth looking at.

But to answer your question - program.exe is a process of its own, or can be treated as such anyway.

Instead of waiting for n seconds, have a look at the waitpid() or wait() functions.

I'm not sure why you're calling kill() with those arguments - am I missing something? I would have expected you to kill() the PID you started.

Mike

The options are: fast, cheap and right - pick any two.. [orientalbow] & [anakin]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884
 
Alarm seems to be working yet better. This code is running without problems (somehow ` ` seems to be working better than system()...):

eval {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm 90;
$? = `program.exe --param1 1 --param2 2`;
alarm 0;
};
if ($@) { # timed out
die unless $@ eq "alarm\n";
}
else { # didn't timeout
print "finished program.exe";
}

But I still don't get the whole fork()-kill() thing... PID 0 is the PID for the child I get with fork() and the PID of the parent is available with getppid(), right? But how do I get the PID for the process where program.exe is running?
By killing PID 0 do I also kill the process where program.exe is running?
 
Hi Phyllis,

When you call fork() your script splits into two - so your code is then running twice.

So that you can tell which copy you're running fork() returns a value.

In one copy, the parent copy, it returns the PID of the child process that's just been started.

In the other copy, the child copy, it returns 0.

if($CPID=fork()){
# I'm the daddy
print "Woo hoo\n";
# do something, wait for the child process maybe
} else {
# I'm the child
print "That's *so* unfair\n"
}

(this particular child happens to be a teenager)

Mike

The options are: fast, cheap and right - pick any two.. [orientalbow] & [anakin]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top