# This version uses subs instead of classes. use strict; use warnings; my $pid1 = fork(); if ($pid1) # parent #1 { my $pid2 = fork(); if ($pid2) # parent #2 { # This is just to illustrate that waitpid($pid1) works in parent #2 # Wait for child #2 to finish so we're not interfering with its waitpid() call. waitpid($pid2, 0); my $waitpid1 = waitpid($pid1, 0); print "waitpid1 (in parent #2) = '$waitpid1'\n"; } elsif ($pid2 == 0) # child #2 { print "I'm child #2\n"; #******* waitpid($pid1) inside child #2 will return -1. #******* It seems to not know about $pid1. How can I make it work? my $waitpid1 = waitpid($pid1, 0); print "waitpid1 (in child #2) = '$waitpid1'\n"; } } elsif ($pid1 == 0) # child #1 { print "I'm child #1\n"; }