#!/bin/perl
use POSIX;

$pid = fork();
defined $pid or die "$0: $!";

if ($pid == 0) {
	#Arrive here if I am the child.
	exit 0;   #Turn into a zombie immediately.
}

#Arrive here if I am the parent.
sleep 1;   #Give the child time to turn into a zombie.

print "My PID is $$ and my child's PID is $pid.\n",
	`ps -l -p $$ -p $pid`;   #minus lowercase L for "long"

$p = wait();   #Harvest the zombie.
if ($p != $pid) {
	warn "$0: my child is $pid, not $p.";
	exit 1;
}

if (!WIFEXITED($?)) {
	warn "$0: parent didn't receive exit status from child.";
	exit 2;
}

$status = WEXITSTATUS($?);
if ($status != 0) {
	warn "$0: parent received exit status $status instead of 0 from child.";
	exit 3;
}

exit 0;
