#!/usr/bin/perl use strict; use warnings; # number of times to fork # DO NOT RAISE ABOVE 10. my $maxfork = 10; # do not edit the below variables my $forkcount = 1; my $count = 0; my %settings; my $pid; $SIG{CHLD} = \&sub_to_do; for ($forkcount = 0; $forkcount < $maxfork; $forkcount++) { sleep 1; # so we don't overload ourselves if (!defined(my $pid = fork())) { # fork die "cannot fork: $!"; # die fork if fork fails } elsif ($pid == 0) { # fork successful, do stuff print "new child: $forkcount\n";# number children sub_to_do(); # stuff to do exit 0; # kills child } else { # parent waitpid($pid, 0); $SIG{CHLD} = \&sub_to_do; } } while(1) { sleep 1; } # waits for all children to die sub sub_to_do { print "in sub, fork number " . $forkcount . "\n"; }