This snippet shows how to (properly) fork() in Perl. Don't forget to use wait() to collect your children after they're done...
Update:: Thanks to Coruscate for catching the runaway $
use strict; use warnings; use constant FORK_WAIT => 2; use constant MAX_ATTEMPTS => 10; our $attempt = 0; my $pid = undef; while (not defined ($pid = fork())) { die "Too many failed fork() attempts: $!\n" if ++$attempt > MAX_ATTEMPTS; warn "fork() failed: $!\n"; sleep FORK_WAIT; } if ($pid) { # I'm the father } else { # I'm the son }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Safe fork() construct
by rob_au (Abbot) on Feb 09, 2003 at 21:23 UTC |