CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
killing threads inside forks

 



mojo2405
New User

Jul 31, 2013, 5:02 AM

Post #1 of 2 (736 views)
killing threads inside forks Can't Post

Hi, I have a code now that starts fork (with few childs). Each child runs few threads. What I want to do is to - catch a ctrl + c from user by the parent (this part is working for me) , send a signal to the children that kills immediatly the threads and do some more code (write to DB, etc..) - this part is half working for me. the part that working is the children actually catches the signal ( I'm using USR1 signal ) , and hte part that doesnt work for me - us that it catches it only when the threads end. if not threads run in the fork children - the form catches the signal immediatly. Any ideas guys ? this is how I send the USR1 to children :


Code
kill USR1 => (keys %framework_child_procs);  

Code
thanks ! 


mojo2405
New User

Jul 31, 2013, 7:46 AM

Post #2 of 2 (733 views)
Re: [mojo2405] killing threads inside forks [In reply to] Can't Post

I better be more clearer and show some code..

So this is the father starting his children :
<code>

#Start fork
foreach my $xml_obj (@{$xml_ojects_ref}){
my $pid = fork();
if ($pid) {
# parent
push(@childs, $pid);
$framework_child_procs{$pid} = 1;
}elsif($pid == 0) {
# child
my $results = new testProcess($xml_obj,$fatherSN); #Test process is a new run
exit 0;
}else {
print ,"couldnt fork: $!";
exit 1;
}
}

#Wait for to end
foreach my $child (@childs) {
my $tmp = waitpid($child, 0);
print "done with pid $tmp";
}

</code>

This is the code (inside the father class) , which handles the CTRL+C:

<code>
$SIG{INT}=\&ctrl_c_handler;

sub ctrl_c_handler
{
$SIG{'INT'}='IGNORE';
my @procs = keys (%{fatherProcess::framework_child_procs});
kill SIGUSR1 => @procs;


foreach my $child (@procs) {
my $tmp = waitpid($child, 0);
}
}
</code>

This is a code inside a child (this is how I start the threads inside a child actually):

<code>
my $t = threads->new (sub{
local $SIG{'USR1'}=sub {threads->exit();};
\&$functions_name(@parameters);
});

my $result = $t->join();
</code>

Now the signal (CTRL+C) comes from the user / CLI , then it catched by the father (going to ctrl_c_handler function) , and then ctrl_c_handler function suppose to send the USR1 signal to the threads - but the thread doesn't catch it.

In addition - in the child (testProcess), before starting the thread , I have another USR1 handler , which caught - only after the thread is ending.

I think your solution is not suitable here..

please help :)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives