Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Re: Timeout thread while it is in a system command

Thread Tools

Re: Timeout thread while it is in a system command

Rainer Weikusat
Posts: n/a
Kasper Middelboe Petersen <(E-Mail Removed)> writes:
> I have a seemingly simple problem. I need to execute a series of
> system commands (using `<cmd>`) in parallel.
> The code below has been stripped of anything meaningful besides
> demonstrating my problem:
> #!/usr/bin/perl -w
> use strict;
> use threads;
> use POSIX;


> POSIX::sigaction(SIGALRM, POSIX::SigAction->new(\&timeout));
> alarm(2);
> sub threadsub {
> sub handletimeout {
> print "KILL\n";
> threads->exit(1);
> }
> POSIX::sigaction(SIGALRM, POSIX::SigAction->new(\&handletimeout));
> # while(1) { sleep(1); }
> return `sleep 10`;
> }


> Now, the problem is the ALRM signal sent to the threads are never
> caught when the thread is blocked in the system call. If you uncomment
> the while loop the signal are caught as intended.
> How do I make this work so I'm able to timeout my threads even if
> they're stuck in the system command?

SIGALRM is a so-called asynchronous signal and this means that it may
be handled by any thread not currently blocking it, IOW, this can't
work: There will be one SIGALRM generated for the process once the
alarm expires, no matter how many threads were started and this will
interrupt one (arbitrarily selected) thread (your signal handler is
also not 'safe', meaning, should be process be doing something more
complicated than executing an inifinite loop or being blocked in a
system call, all kinds of weird things can result).
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
suppress opening command window after using os.system command boriq Python 4 06-12-2008 10:42 AM
Thread#raise, Thread#kill, and timeout.rb are unsafe Charles Oliver Nutter Ruby 43 03-25-2008 02:31 PM
System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. Ersin Gençtürk ASP .Net 1 09-25-2007 07:33 AM
Re: System.Data.SqlClient.SqlException: Timeout expired. The timeout p Karl Seguin ASP .Net 1 09-04-2007 11:34 AM
Timeout::timeout and Socket timeout Mark Probert Ruby 1 10-06-2004 09:30 AM