All pastes #31603 Raw Edit

Anonymous

public text v1 · immutable
#31603 ·published 2005-11-28 22:47 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) {                        # fork successful, do stuff
                print "> doing thing\n";        # inform of success
                print "new child: $forkcount\n";# number children
                sub_to_do();                    # stuff to do
                exit 0;                         # kills child
        } else {                                # parent
	waitpid($pid, 0);
        }
} while (1) { sleep 1; }           # waits for all children to die 

sub sub_to_do {
print " fork number " . $forkcount;
}