Exiting threads via signal

P

Prince Al

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();
}
 
S

sln

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
 
S

sln

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
 
P

Prince Al

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.
 
P

Prince Al

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

Cheers

Tim
 
S

sreservoir

Prince said:
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.
 
W

Wanna-Be Sys Admin

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

File locking using threads 14
thread problem 1
Perl threads 6
Define alarm in threads 1
Memory leak with threads 16
Threads and Directory Handles 2
Many threads working constantly 1
threads / freeing memory 1

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top