Santa's a busy guy with a lot on his mind, and can never quite remember how to invoke fork…"RTFM!" you say? Did I mention he was a busy guy? Instead, this cookie-thieving B&E artist—just kidding Nick, honest!—relies upon a little bit of syntactic sugar known as ForkBlock, which actually makes things a bit clearer too:
1 use ForkBlock ':all'; 2 3 #'Dumb' so that the parent can carry-on afterwards & show us another way 4 DumbFork { 5 Parent \&manager, 6 Child \&worker 7 }; 8 9 #OR 10 Fork { 11 Parent { 12 my $kid = $_[0]; #Not meant to condone infanticide 13 local $SIG{ALRM} = sub { kill(9, $kid); sleep 2; exit }; 14 alarm(2); 15 16 print "Work work work\n" while(1); 17 } 18 Child { 19 print "I want toys!\n" while(1); 20 } 21 }; 22 23 24 sub manager{ 25 my $employee = $_[0]; 26 local $SIG{ALRM} = sub { kill(9, $employee); sleep 2; die }; 27 alarm(2); 28 29 #Eval required to trap die, so signal handler can punt us, 30 #and we can fall out to second invocation form. 31 eval{ print "Work harder not smarter\n" while(1) }; 32 } 33 34 sub worker{ 35 print "Work work work\n" while(1); 36 }
Published 2008-12-24