I Am forking child processes. I am waiting for them to finish in parent. If you see my code What I want to parent is not to move forward until it get info from each child. Next portion of the code is dependent on the status of the child. I want to do kind of wait in a loop in parent and keep checking for the child's response. Here is my problem I am not able to do it last logic. Please help me with suggestions.
#!/usr/local/bin/perl -w use strict; use warnings; use Net::Telnet; use IO::Socket::INET; my $IsParent = 1; my $ListenPort = 5000; my $PPID = $$; print "I am the only process.\n"; my @children_pids; for my $count (1..3){ my $child_pid = fork(); if ($child_pid) { # If I have a child PID, then I must be the + parent push @children_pids, $child_pid; } else { # I am the child # close the sides of the pipes not used by the child my $end; local $SIG{HUP} = sub { $end = 1 }; my $Child_PID = $$; print "\n============================== \n"; print "CHILD's PID is: $Child_PID \n"; my $db1 = ConnectToServer('127.0.0.1',$ListenPort,60); local $SIG{HUP} = sub { $end = 1 }; #Do something and then write to DB as done WriteValueToDB('done',$db1,$Child_PID . '_Status'); until ($end) { #print "Sleep 1\n"; sleep 1; } print "============================== \n"; function2(); } } foreach my $child (@children_pids) { print "Parent: Waiting on $child\n"; sleep 5; waitpid($child, 0); } # HERE I WANT TO GET THE STATUS OF ALL THE KIDS BECAUSE FROM HERE IT D +EPENDS ON EACH RESULT FURTHER PROCESSING. WAIT IN A LOOP OR SOMTETHIN +G # NOT GETTING IT HOW TO DO IT. I DON'T WANT TO CONTINUE UNLESS I GET A +LL KIDS STATUS. DON'T WANT TO GO TO END DB MAIN UNLESS I GET ALL CHIL +D PROCESSES STATUS. # my $localtime = localtime; # my $child_stat = ReadValue($child . '_Status',$dbmain); # accoding to the result in DB decide to kill it #print "$child CHILD STAT: $child_stat \n"; #kill HUP => $child; print "END DB MAIN: $dbmain \n"; KillServer($dbmain); print "All done.\n";
In reply to How to make parent wait for all the child processes. by gjoshi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |