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 }
In reply to Safe fork() construct by fokat
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |