I have some Perl code that generates a number of child processes. The basic code is as follows:
my $forked = 0;
my $err = 0;
my $Processes_To_Use_After_Calc=10;
print "Parent ($$) has started\n";
for my $ispawn (1 .. $Processes_To_Use_After_Calc){
my $child_pid = fork();
die "Cannot fork: $!" if !defined $child_pid; # system overload
if(defined $child_pid && $child_pid > 0) {
## Parent
$forked++;
} elsif(defined $child_pid){
#
# Here some calculations are performed
#
print "Child $$ has finished (number $ispawn) \n";
}
}
for(1..$forked) {
my $child_pid = wait();
}
All fairly standard stuff. Now, I would like to know if any of the child processes crash (failed to terminate correctly). Clearly if the last statement of each child process is not printed I could assume there was a problem. However I would like another method that would exit the parent program completely and close all child processes that were still open if one of the child processes crashes. Is this possible?