Hi All, I am testing the fork feature in the Linux. In below code out of 3 child parent randomly check one child and wait for it to finish and moves to another. I was wondering is it possible to make parent to report the child which finish first and another child that finish second and the third?
use strict; use warnings; use POSIX ":sys_wait_h"; handler(); sub handler { my %process = ( A => 'sleep 10 && exit 0', B => 'sleep 15 && exit 0', C => 'sleep 25 && exit 1', ); my @child_pids; foreach my $process(keys %process) { my $pid = fork; if($pid) { #Parent. # print "Launched $process | $pid\n"; push(@child_pids, {PID => $pid, PROCES +S => $process{$process}}); } elsif($pid == 0) { exec $process{$process}; die "Can't exec $process"; } else { #Parent # Fork failed. die "cant fork $!\n"; } } foreach my $href (@child_pids) { print "Waiting $href->{PID} | $href->{ +PROCESS}\n"; while (waitpid($href->{PID}, WNOHANG) +== 0) { sleep 1; } print "Failed $href->{PROCESS}\n" unle +ss($? == 0); print "Done $href->{PID} | $href->{PRO +CESS}\n"; } }
In reply to fork report child finish by order child process finish. by tart
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |