All pastes #31614 Raw Edit

Miscellany

public text v1 · immutable
#31614 ·published 2005-11-28 23:14 UTC
rendered paste body
#!/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
            while (1) {
	      sub_to_do();
	    }
        } 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";
}