Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Sending signals to threads that trying to join

by mojo2405 (Acolyte)
on Aug 22, 2013 at 14:49 UTC ( #1050536=perlquestion: print w/replies, xml ) Need Help??
mojo2405 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, So, I'm attaching my code. The code create 3 sons (with fork). Then each son create 3 grand sons (with thread). What I'm failing to do is to send to a son (started with fork) a USR1 signal , which will make all its grand sons to exit immediatly (and not wait to the grand sons to exit after they finish their work). I want to send the USR1 signal from outside to the son PID. I know there is a limitation and I might can not do that, but do other here another clean way to exit threads of the son immediatly when son gets USR1 signal ? Thanks ! Here is the code , the goal is to make the threads to exit while sleep function starting :
#!/usr/bin/env perl use threads; ##kill -10 handle $SIG{USR1}=\&usr_kill_handler; my @sons = ("Son1","Son2","Son3"); my @childs = (); #Start fork foreach my $son (@sons){ my $pid = fork(); if ($pid) { # parent push(@childs, $pid); }elsif($pid == 0) { # child my $results = new testProcess($son); exit 0; }else { print "\nerror : couldnt fork: $!\n"; exit 1; } } #Wait for to end foreach my $child (@childs) { my $tmp = waitpid($child, 0); print "\ninfo : done with pid $tmp\n"; } sub usr_kill_handler{ #Send SIGUSR1 to all childs (started with fork) kill SIGUSR1 => @childs; print "\n\n Stoped due to user request!\n\n"; #Wait to all childs to exit foreach my $child (@childs) { my $tmp = waitpid($child, 0); print "\ndone with pid $tmp\n"; } # After all child ended kill father exit 1; } package testProcess; sub new{ my $class = shift; my ($son) = @_; ##kill -10 handle $SIG{USR1}=sub { print "\n Got signal to son $son with pid ".$$." \n"; threads->exit; }; print "\nSon $son with PID ".$$." Started\n"; my %hash_results = (); my @grand_sons = ("Grand son 1","Grand son 2","Grand son 3"); my @threads = (); #Start new thread foreach my $grand_son (@grand_sons){ my $t = threads->create (\&start_threads,$son,$grand_son); push(@threads,$t); } my $index=0; foreach my $thread (@threads) #wait for all threads untill the end + and insert results to hash { $hash_results{$index}=$thread->join; $index++; } print "\nSon $son with PID ".$$." Finished\n"; } sub start_threads{ my ($son,$grand_son) = @_; print "\nSon $son grand son $grand_son started\n"; my $random_number = int(rand(100)); my $msg = "Son $son grand son $grand_son returns random number $ra +ndom_number"; print "\n$msg\n"; sleep 100; return $random_number; } 1;

Replies are listed 'Best First'.
Re: Sending signals to threads that trying to join
by vsespb (Chaplain) on Aug 22, 2013 at 19:26 UTC
    Seems problem that join() call blocks signals (i.e. signal will arrive after join() finished).

    some related links:
Re: Sending signals to threads that trying to join
by afoken (Abbot) on Aug 24, 2013 at 08:43 UTC

    Threads and signals don't mix well. It highly depends on the operating system which thread will receive signals. In the worst case, signals are delivered to a randomly choosen thread. Use the search function to see some other discussions.


    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1050536]
Approved by kcott
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2017-07-23 17:10 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (347 votes). Check out past polls.