Exiting threads via signal

Discussion in 'Perl Misc' started by Prince Al, Nov 11, 2009.

  1. Prince Al

    Prince Al Guest

    Hi,

    I am trying to write some code that creates threads to run data load
    jobs. There is also an extra thread created that watches for a
    specific file to be created. When this file is found, the watcher
    thread sends stop signals to all currently running threads. My code
    appears to exit correctly, but I cannot seem to get the subroutine of
    the SIG command to exit correctly.

    We are running HP-UX and the version of Perl we have is 5.8.8 (with
    thread support obviously). A cut down version of my code is below.

    Thanks in advance for any help :)

    Cheers

    Tim

    #!/opt/perl_5.8.8cu/bin/perl

    use strict;
    use warnings;
    use threads;

    my $system = "test";
    my $hard_stop_file = "/tmp/$system"."_hard.stop";

    if (-f $hard_stop_file) {
    unlink($hard_stop_file);
    }

    my $watcher_thread = threads->new(\&hard_stop_watcher);
    $watcher_thread->detach();

    foreach my $i (0..4) {
    threads->new(\&flock_test, $i);
    }

    while (scalar(threads->list()) > 0) {
    sleep 1;
    }

    sub flock_test {
    $SIG{'STOP'} = sub {
    print "I cannot get this to print!\n";
    threads->exit();
    };

    sleep 20;
    }

    sub hard_stop_watcher {
    while ( !-f $hard_stop_file) {
    sleep 1;
    }

    print "$hard_stop_file found...\n";

    foreach my $thr (threads->list) {
    print "killing $thr\n";
    $thr->kill('STOP')->detach();
    }

    threads->exit();
    }
    Prince Al, Nov 11, 2009
    #1
    1. Advertising

  2. Prince Al

    Guest

    On Wed, 11 Nov 2009 04:08:50 -0800 (PST), Prince Al <> wrote:

    >Hi,
    >
    >I am trying to write some code that creates threads to run data load
    >jobs. There is also an extra thread created that watches for a
    >specific file to be created. When this file is found, the watcher
    >thread sends stop signals to all currently running threads. My code
    >appears to exit correctly, but I cannot seem to get the subroutine of
    >the SIG command to exit correctly.
    >
    >We are running HP-UX and the version of Perl we have is 5.8.8 (with
    >thread support obviously). A cut down version of my code is below.
    >
    >Thanks in advance for any help :)
    >
    >Cheers
    >
    >Tim
    >


    It looks like detach() is being called before $SIG{'STOP'} sub,
    so try detaching in the handler itself.

    Of course after 20 seconds, the thread exits on its own, therefore
    the handler is invalid in that case.

    Also, detach will need to be called on the thread if it is no longer
    running. Put in a check for that in the watcher thread. This way, the
    body of flock_test() can exit after an arbitrary amount of time.


    Try this ...

    >#!/opt/perl_5.8.8cu/bin/perl
    >
    >use strict;
    >use warnings;
    >use threads;
    >
    >my $system = "test";
    >my $hard_stop_file = "/tmp/$system"."_hard.stop";
    >


    use strict;
    use warnings;
    use threads;

    my $system = "test";
    my $hard_stop_file = "$system"."_hard.stop";

    if (-f $hard_stop_file) {
    unlink($hard_stop_file);
    }

    my $watcher_thread = threads->new(\&hard_stop_watcher);
    $watcher_thread->detach();

    foreach my $i (0..4) {
    threads->new(\&flock_test, $i);
    }

    while (scalar(threads->list()) > 0) {
    sleep 1;
    }

    sub flock_test {
    my $id = $_[0];
    print "starting $id\n";
    $SIG{'STOP'} = sub {
    print "SIG_STOP recieved: $id\n";
    threads->detach();
    threads->exit();
    };
    sleep 1 while(1); # this will always have to finish, ie: sleep(1) in this case
    # but you can comment it out, is_running() below will detach it.
    }

    sub hard_stop_watcher {
    while ( !-f $hard_stop_file) {
    sleep 1;
    }

    print "$hard_stop_file found...\n";

    foreach my $thr (threads->list) {
    print "killing $thr\n";
    print " running? = '", $thr->is_running(),"'\n";

    if ($thr->is_running()) { # this is not atomic
    $thr->kill('STOP');
    } else {
    $thr->detach();
    }
    }

    threads->exit();
    }

    __END__

    -sln
    , Nov 11, 2009
    #2
    1. Advertising

  3. Prince Al

    Guest

    On Wed, 11 Nov 2009 12:50:14 -0800, wrote:

    >On Wed, 11 Nov 2009 04:08:50 -0800 (PST), Prince Al <> wrote:
    >
    >>Hi,
    >>
    >>I am trying to write some code that creates threads to run data load
    >>jobs. There is also an extra thread created that watches for a
    >>specific file to be created. When this file is found, the watcher
    >>thread sends stop signals to all currently running threads. My code
    >>appears to exit correctly, but I cannot seem to get the subroutine of
    >>the SIG command to exit correctly.
    >>
    >>We are running HP-UX and the version of Perl we have is 5.8.8 (with
    >>thread support obviously). A cut down version of my code is below.
    >>
    >>Thanks in advance for any help :)
    >>
    >>Cheers
    >>
    >>Tim
    >>

    >
    >It looks like detach() is being called before $SIG{'STOP'} sub,
    >so try detaching in the handler itself.
    >
    >Of course after 20 seconds, the thread exits on its own, therefore
    >the handler is invalid in that case.
    >
    >Also, detach will need to be called on the thread if it is no longer
    >running. Put in a check for that in the watcher thread. This way, the
    >body of flock_test() can exit after an arbitrary amount of time.
    >
    >
    >Try this ...
    >


    I'm actually shocked folks post code that *doesen't* work
    without trying different things and debug.

    Just like, 'here it is, fix it', .. blah, blah, blah. typical.
    -sln
    , Nov 12, 2009
    #3
  4. Prince Al

    Prince Al Guest

    On 12 Nov, 03:28, wrote:
    > I'm actually shocked folks post code that *doesen't* work
    > without trying different things and debug.
    >
    > Just like, 'here it is, fix it', .. blah, blah, blah. typical.
    > -sln


    Well if I had managed to get it to work, then why would I have
    bothered to post?! I have been at this for two days now, trying to get
    it to work, but to no avail... Thanks for taking the time to reply,
    but next time, please don't bother if that's your attitude.
    Prince Al, Nov 12, 2009
    #4
  5. Prince Al

    Prince Al Guest

    Ok, thanks for the replies - managed to fix my problem. The solution
    was to change 'STOP' to 'HUP' - not sure why tho!

    Cheers

    Tim
    Prince Al, Nov 12, 2009
    #5
  6. Prince Al

    sreservoir Guest

    Prince Al wrote:
    > Ok, thanks for the replies - managed to fix my problem. The solution
    > was to change 'STOP' to 'HUP' - not sure why tho!
    >
    > Cheers
    >
    > Tim


    sigstop is not blockable. the program stops, or lack of permissions.

    at least,on unix-like systems.

    --

    "Six by nine. Forty two."
    "That's it. That's all there is."
    "I always thought something was fundamentally wrong with the universe"
    sreservoir, Nov 12, 2009
    #6
  7. wrote:

    >
    > I'm actually shocked folks post code that *doesen't* work
    > without trying different things and debug.
    >
    > Just like, 'here it is, fix it', .. blah, blah, blah. typical.
    > -sln


    Are you replying to yourself about your own code you told the OP to use?
    Or are you accusing the OP of not trying to get it to work before they
    posted? If so, how would you know what they've tried or not? Why did
    you bother replying if you're going to get upset about taking the time
    to reply?
    --
    Not really a wanna-be, but I don't know everything.
    Wanna-Be Sys Admin, Nov 12, 2009
    #7
    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. Tony
    Replies:
    0
    Views:
    452
  2. Michael Pronath
    Replies:
    1
    Views:
    1,164
    Diez B. Roggisch
    Jan 3, 2005
  3. Jack Orenstein

    threading.Thread vs. signal.signal

    Jack Orenstein, Sep 18, 2005, in forum: Python
    Replies:
    0
    Views:
    462
    Jack Orenstein
    Sep 18, 2005
  4. Ouabaine

    Exiting threads

    Ouabaine, Mar 9, 2008, in forum: Java
    Replies:
    3
    Views:
    243
    Ouabaine
    Mar 10, 2008
  5. Exiting given via next

    , Apr 22, 2009, in forum: Perl Misc
    Replies:
    14
    Views:
    222
Loading...

Share This Page