c has asked for the wisdom of the Perl Monks concerning the following question:
## 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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: making fork() a flag, and wait()ing properly
by bluto (Curate) on Feb 08, 2002 at 16:46 UTC | |
|
Re: making fork() a flag, and wait()ing properly
by lestrrat (Deacon) on Feb 08, 2002 at 17:59 UTC | |
by c (Hermit) on Feb 08, 2002 at 19:09 UTC | |
by lestrrat (Deacon) on Feb 08, 2002 at 19:41 UTC |