Re: Timeout thread while it is in a system command

Discussion in 'Perl Misc' started by Rainer Weikusat, May 15, 2012.

  1. Kasper Middelboe Petersen <> 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).
    Rainer Weikusat, May 15, 2012
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Karl Seguin
    Replies:
    1
    Views:
    5,525
    Arun M
    Sep 4, 2007
  2. Ersin Gençtürk
    Replies:
    1
    Views:
    7,185
    jmohanty2006
    Sep 25, 2007
  3. boriq
    Replies:
    4
    Views:
    564
    Tim Golden
    Jun 12, 2008
  4. Mark Probert

    Timeout::timeout and Socket timeout

    Mark Probert, Oct 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    1,250
    Brian Candler
    Oct 6, 2004
  5. Charles Oliver Nutter
    Replies:
    43
    Views:
    588
    Roger Pack
    Mar 25, 2008
Loading...

Share This Page