Hi Below is my script where alarm is generated after 15 sec and changes the global variable in parent from 0 to 1. Is there any mistake in my multiprocess approach with time-based stop ?
#!/usr/bin/perl use strict; use warnings; use Parallel::ForkManager; my $MAX_PROC=2; $|++; my $stopFlag=0; main(); sub main { preprocess(@ARGV); multiproc($MAX_PROC, @ARGV); } sub multiproc { my $proc=shift; my $argu = $ARGV[0]; open(USRFILE, "$argu") or die "cant open $!"; my $pmgr = Parallel::ForkManager->new($proc); $pmgr->run_on_finish( sub { my ($pid, $exit_code, $ident) = @_; print "** $ident just got out of the pool ** with PID $pid and parent pid as $$ exit code: $exit_code\n"; } ); $pmgr-> run_on_start( sub { my ($pid,$ident)=@_; print "** $ident started, pid: $pid **\n"; } ); $SIG{ALRM} = sub { $stopFlag = 1; }; alarm(16); while(my $user = <USRFILE>) { chomp($user); print "value of Stop Flag in parent $0 is $stopFlag\n"; if($stopFlag == 1) { print "stop flag has been set\n"; last; } my $id = $pmgr->start($user) and next; print "value of Stop Flag in child $id is $stopFlag\n"; sleep(7); $pmgr->finish($user); } print "Waiting for all children to exit\n"; $pmgr->wait_all_children(); alarm(0); print "All children completed\n"; } This is what I get output : - perl mprocess.pl /tmp/list1 value of Stop Flag in parent mprocess.pl is 0 value of Stop Flag in child 0 is 0 ** te...@test.com started, pid: 21777 ** value of Stop Flag in parent mprocess.pl is 0 value of Stop Flag in child 0 is 0 ** te...@test.com started, pid: 21778 ** value of Stop Flag in parent mprocess.pl is 0 ** te...@test.com just got out of the pool ** with PID 21777 and parent pid as 21776 exit code: 0 ** te...@test.com just got out of the pool ** with PID 21778 and parent pid as 21776 exit code: 0 value of Stop Flag in child 0 is 0 ** te...@test.com started, pid: 21811 ** value of Stop Flag in parent mprocess.pl is 0 value of Stop Flag in child 0 is 0 ** te...@test.com started, pid: 21812 ** value of Stop Flag in parent mprocess.pl is 0 ** te...@test.com just got out of the pool ** with PID 21811 and parent pid as 21776 exit code: 0 ** te...@test.com just got out of the pool ** with PID 21812 and parent pid as 21776 exit code: 0 value of Stop Flag in child 0 is 0 ** te...@test.com started, pid: 21832 ** value of Stop Flag in parent mprocess.pl is 0 value of Stop Flag in child 0 is 0 ** te...@test.com started, pid: 21833 ** value of Stop Flag in parent mprocess.pl is 0 ** te...@test.com just got out of the pool ** with PID 21832 and parent pid as 21776 exit code: 0 ** te...@test.com just got out of the pool ** with PID 21833 and parent pid as 21776 exit code: 0 value of Stop Flag in child 0 is 1 ** te...@test.com started, pid: 22030 ** value of Stop Flag in parent mprocess.pl is 1 stop flag has been set Waiting for all children to exit ** te...@test.com just got out of the pool ** with PID 22030 and parent pid as 21776 exit code: 0 All children completed The only concern here is I see value of stopflag as 1 for child before the parent which is a bit wierd:- value of Stop Flag in child 0 is 1 ** te...@test.com started, pid: 22030 ** value of Stop Flag in parent mprocess.pl is 1 Thanks and Regards.