Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hey rjt ! thank you for your fast answers ! I though I wouldn't need to do so , but I have to expose my code, maybe I'll be clearer. I want to thank you first for your help , I know you're trying :) First I want to say that my code can't have any timeout, cause it can be running for days or even weeks, so timeout for the threads is not useful here. I tried to add an alarm trigger for the children , but still - same problem. Now , for the code , there is alot of code so I'll try to show what is relevant for our case and cut some of it. Father :
sub init { my ($logger_input,$xmlFile_input,$ini_file_input,$run_all_input,$g +ui_sync_file_input,$dump_input) = @_; #Setting and reading params from run_me readParamsFromRunme($logger_input,$xmlFile_input,$ini_file_input,$ +run_all_input,$gui_sync_file_input,$dump_input); #Handle Ctrl+c if not running via GUI $SIG{INT}=\&ctrl_c_handler if (!$gui_sync_file); ##kill -10 from UI $SIG{USR1}=\&usr_kill_handler; } # FUNCTION NAME: ctrl_c_handler # WRITTEN BY: Sagi # DATE: 1.11.2011 # PURPOSE: Exit the test when user press Ctrl+c # IN PARAMS: None # RETURNED VALUES: None # Sample call: sub ctrl_c_handler { #Wait for to end $SIG{'INT'}='IGNORE'; my @procs = keys (%{fatherProcess::framework_child_procs}); kill ALRM => @procs; kill SIGUSR1 => @procs; foreach my $child (@procs) { my $tmp = waitpid($child, 0); } $father_self->finishRun("user stoped"); } sub usr_kill_handler { my $signame = shift; printer::Logger_screen_message("info","got signal $signame",0,$log +ger); $father_self->finishRun("user stoped"); }
Start forking the childs from the father :
sub start_fork_run{ my $self = shift; my ($xml_ojects_ref,$night_run_flag) = @_; my @childs = (); #Get father PID my $father_pid=$$; #Get father SN my $fatherSN = $self->getSN(); if (!$fatherSN){ printer::Logger_screen_message("error","can't get father's SN from + DB",0,$logger); exit 1; } #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,$gui_sync_fil +e,$father_pid,$night_run_flag); exit 0; }else { printer::Logger_screen_message("error","couldnt fork: $!",0,$l +ogger); exit 1; } } #Wait for to end foreach my $child (@childs) { my $tmp = waitpid($child, 0); delete $framework_child_procs{$child}; printer::Logger_screen_message("info","done with pid $tmp",0,$logg +er); } #End the whole topology run $self->finishRun("finish"); }
a child object :
package testProcess; #use strict; use warnings; use Data::Dumper; use Getopt::Long; use File::Basename; use File::Temp; use Storable ('dclone'); use logger; use reporter; my $logger; our $local_xml_obj; my $gui_sync_file; my $night_run; my $packageReporter; sub new { my $class = shift; my ($xml_obj,$fatherSN,$gui_sync_file_input,$father_pid,$night_run +_input) = @_; #kill -10 from UI $SIG{'USR1'}=\&prc_kill_handler; #Reseting signals so only father will catch them $SIG{'INT'}='IGNORE'; $SIG{'ALRM'}=sub {foreach my $thr (@parallel::threads){ $thr->kill('ALRM'); } }; } sub prc_kill_handler { my $signame = shift; printer::colorized_msg("\n got to prc_kill_handler \n","green"); $logger->info("usr_kill_handler: got signal $signame line: ".__LIN +E__); testProcess::finishPackage("user stoped"); testProcess::exit_run(); }
now each child can call this parallel function (which create threads as many as he wishes) , and here is the problem (that it catches the signal only after the threads end) :
sub Functions_Run_in_threads_windows { my (%hash)=@_; printer::Logger_screen_message ("Info", "Start", "", $logger); my @temp_file; if(!%hash) { printer::Logger_screen_message("Error","Functions_Run_in_threads +: The hash is empty",1,$logger); return %hash; } my ($functions_name,@temp_params,@parameters,%hash_results); @threads = (); for( my $i=0 ; $hash{"params_name_$i"} ; $i++) { @temp_params=$hash{"params_name_$i"}; $functions_name=$hash{"function_name_$i"}; @parameters=(); #push (@parameters,$functions_name); if(!@{$temp_params[0]}) #check input if not reference return fai +l { printer::Logger_screen_message("Error","Please send parameter +s array as referens",1,$logger); return %hash; } #parse hash table and run the function for( my $index=0 ; $index<@{$temp_params[0]} ; $index++) { if($temp_params[0][$index]) { $tmp=$temp_params[0][$index]; push(@parameters,$tmp); } else { push(@parameters,""); } } #Start new thread my $t = threads->create (sub{ $SIG{ALRM} = sub { die("Timeout\n"); }; \&$functions_name(@parameters); }); 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++; } return %hash_results; }
Hope now you better understand it (and the problem). Thanks again.

In reply to Re^4: killing threads inside forks by mojo2405
in thread killing threads inside forks by mojo2405

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-18 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found