#!/usr/bin/perl #use Errno qw(EAGAIN); # Gets you the EAGAIN symbol for looping on fork soft errors. use POSIX ":sys_wait_h"; # Gets you the WNOHANG symbol, used in waitpid call. $|=1; # Force all writing to STDOUT to be immediately auto-flushed. $pid1 = fork; # print "\nAfter the first fork, PID $$ shows \$pid1 =>$pid1<\n"; if (! defined $pid1) {die "Bizarre fork error.\n"} if ($pid1==0) { # First child process exit; } else { # Parent process $pid2 = fork; if (! defined $pid2) {die "Bizarre fork error.\n"} if ($pid2==0) { # Second child process exit; } else { # Parent process do { $kid = waitpid(-1,&WNOHANG); # print "In reap loop, \$kid =>$kid< with exit status of >$?<\n" if $kid != 0; # waitpid returns 0 if the child is still running # -1 if the child is no longer around (i.e. it's been reaped) # the $pid the first time we waitpid after the child dies. } until $kid == -1; } } print "\nHere when all children have been reaped.\n";