use warnings; use strict; $|=1; my $cmd = q{ perl -e 'print "child: id after exec: $$\n"; print "child: sees processes...\n " , grep { m/perl/ } `ps ax` , "\n"; ' }; print "parent: pre-fork id $$\n"; if ( my $pid = fork ) { # this is parent code print "parent: post-fork id $$\n"; print "parent: return from fork is: $pid\n"; sleep 3; print "parent: still here, with id $$\n"; print "parent: sees processes...\n " , grep { m/perl/ } `ps ax` , "\n"; } else { # this is child code die "cannot fork: $!" unless defined $pid; print "child believes it is: $$\n"; exec( $cmd ) # replaces child with a new perl: pid shouldn't change, right? } #### parent: pre-fork id 27516 child believes it is: 27517 child: id after exec: 27518 parent: post-fork id 27516 parent: return from fork is: 27517 child: sees processes... 27516 pts/1 S+ 0:00 /usr/bin/perl /home/doom/bin/testes-forkitude 27517 pts/1 S+ 0:00 sh -c perl -e 'print "child: id after exec: $$\n";? print "child: se 27518 pts/1 R+ 0:00 perl -e print "child: id after exec: $$\n";? print "child: sees proc parent: still here, with id 27516 parent: sees processes... 27516 pts/1 R+ 0:00 /usr/bin/perl /home/doom/bin/testes-forkitude #### parent: pre-fork id 7253 child believes it is: 7254 parent: post-fork id 7253 parent: return from fork is: 7254 child: id after exec: 7254 child: sees processes... 7253 pts/3 S+ 0:00 /usr/bin/perl /home/doom/bin/testes-forkitude 7254 pts/3 R+ 0:00 perl -e print "child: id after exec: $$\n";? print " parent: still here, with id 7253 parent: sees processes... 7253 pts/3 S+ 0:00 /usr/bin/perl /home/doom/bin/testes-forkitude 7254 pts/3 Z+ 0:00 [perl]