Is there any way to build some time counter that enable parts of a script to run as long it ticks? For example, I have the following code:
for my $i (0 .. $QUOTA-1) {
build_dyna_file($i);
comp_simu_exe;
bin2txt2errormap($i);
}
Theoretically I want to run this loop for 3 minutes, even if the loop instructions haven't finished yet, it should still break out of the loop after exactly 3 minutes.
Actually the program opens a time counter window that works in parallel to part of the script (each time I call it).
Additionally, the sub call 'comp_simu_exe' run an outside simulator (in the shell) that when time out ends - this process must also killed (not suppose to return after a while).
sub comp_simu_exe{
system("simulator --shell");
}
Is there any connection between the dead coming problem to the system function call ?
Here's a second answer that deals with the case of timing-out a second process. Use this case to start your external program and make sure that it doesn't take too long:
The way you deal with timeouts like this in Perl is the
alarm
function. It will send the signalALRM
to your process after the number of seconds you pass into it. Be careful if the code you are trying to setup the timeout for callssleep
, as they do not mix well on many platforms. The basic structure looks like this:or written more compactly:
You can set an alarm that will break out of your code after a specified amount of seconds:
Or, if you really need the loop to run at least three times, no matter how long this might take: