waitpid waits for a particular child process to complete, returning 1 if the process is found and -1 on an error.
#!/usr/bin/perl -w
$pid = fork();
if ($pid == 0) {
print "In child.\n";
exit(0); # Terminate child.
} elsif (! defined $pid) {
# Not defined: means an error.
} else {
# Parent process.
sleep(5); # Wait
$status = waitpid($pid, 0);
print "Child is dead with PID $status.\n";
}
Related examples in the same category