http://qs1969.pair.com?node_id=144124

c has asked for the wisdom of the Perl Monks concerning the following question:

A while back, I wrote this asking about how to properly fork(). I'm still getting comfortable with the process, but I think I understand it better now than I did then. My new dilemna is that I would like to make fork, an option set by a getopts flag. I managed to get it to work, but it is, in my opinion, ug-ly. I'm also scratching my head about the proper position for wait() and where it should really sit within the script to operate correctly. I can summarize my code as follows:

## declare a pid for forking my $pid if ($fork); ## for my $node(@devices) { ## fork new process if child does not have pid if (($fork) && ($pid = fork)) { next; } elsif((defined($pid)) || (!$fork)) { ##do some exciting things ... ## explicitly exit the child process exit if ($fork); ## if forking error exit script } else { die "\n\nfound errors in forking\nError: $!\n\n"; ## end of if loop } ## wait on child processes to exit wait() if ($fork); ## end of for loop } ## exit the script exit;

use strict and -w are in there, i just wanted to give the general snippet of where my fork() takes place. i'm confused about the multiple exits. I'm not sure that I really need the one within the if loop, however it seemed like I had read an explicit exit should be given to child processes. And again, the position of the wait() statement is still questionable in my mind as to whether is placed correctly to avoid zombie children. And finally, the whole ($fork) flag just seems ugly to me. Perhaps it can be simplied?

thanks! -c