CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:


New User

Jul 23, 2009, 2:15 AM

Post #1 of 2 (1113 views)
ALARM vs SYSTEM Can't Post

Hello friends,

How can I limit the time spent in a specific section of a Perl script?

I have the following code:

eval { my $guard = scope_guard {
alarm (10);

for my $i (0 .. $QUOTA-1) {

alarm (0);};

Theoretically I want to run this loop for 10 sec, even if the loop instructions haven't finished yet, it should still break out of the loop after exactly 10 sec.

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 - what actually happen).

sub comp_simu_exe{

system("simulator --shell");

Is there any connection between the dead coming problem to the system function call ?


Jul 24, 2009, 9:19 AM

Post #2 of 2 (1102 views)
Re: [yoDar] ALARM vs SYSTEM [In reply to] Can't Post

Hi yoDar,

The problem of dispatching/killing/reaping child processes while avoiding zombies, and the problem of breaking out of a blocking call using an alarm are two separate problems. I recommend a study of the perlipc perldoc. It discusses both issues in detail.


I was probably too glib in my initial reply. The real answer is, "maybe". Normally, the alarm is set in an eval {} block: you execute a "die" to break out of the block when the alarm goes off. The die will kill the system command, but whether the program running inside system() will a) die; and b) get reaped is probably OS/command specific. I will say that I'm using alarm/system/die blocks in some of my code and I haven't had any problems with leftover zombies on Fedora Linux. If you are, you may have to check/kill them yourself if you get a timeout or go the fork/exec/wait route and set your alarm on the wait.



(This post was edited by 1arryb on Jul 27, 2009, 7:52 AM)


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives