#!/usr/bin/perl -w $|++; print "Hello before fork\n"; #print first message $thiscount = 0; #counter for number of forks for($i = 0; $i < 2; $i++) #for loop to fork 2 procs { $thispid = phork(); #fork a proc, store the pid print "Just forked $thispid\n"; #tell us what you've done $thiscount++; #increment the fork count } $wpid = wait; #wait until children are finished print "i'm waiting on $wpid\n"; #tell us what you've done print "hello from after fork. I have forked $thiscount child processes.\n"; #final message from parent? sub phork() #subroutine for forking stuff { $cid = 0; #child id $count = 0; #number of times through. FORK: { print "i am about to start forking stuff. my id is $$, and i have done this $count times\n"; $count++; #increment the number of times through if($pid = fork()) #parent process { print "Parent phorking child. I am: $$, and my child is $pid\n"; $cid = $pid; } elsif(defined $pid) #child processs { print "Hello from in phork. I am $$, and my parent is: $pid\n"; return $$; } elsif($! =~ /No more process/) #error { sleep 5; redo FORK; } else #error { die "Cant phork: $!\n"; } } return $cid; #ship back the child id }