#!/usr/bin/perl
use strict;
use warnings;
use CGI qw(:standard);
print header, start_html(-title=>'fork test',);
print "Parent start at " . localtime() . "
\n";
print STDERR "Parent start at " . localtime() . "\n";
if(my $pid = fork()) {
sleep(5);
print "Parent forking at " . localtime() . "
\n";
print STDERR "Parent forking at " . localtime() . "\n";
wait();
} else {
print "First child at " . localtime() . "
\n";
print STDERR "First child at " . localtime() . "\n";
exit(0);
}
if(my $pid = fork()) {
print "Parent forking again at " . localtime() . "
\n";
print STDERR "Parent forking again at " . localtime() . "\n";
} else {
sleep(50);
print "Second child at " . localtime() . "
\n";
print STDERR "Second child at " . localtime() . "\n";
close(STDOUT);
sleep(50);
print STDERR "Second child after STDOUT close at " . localtime() . "\n";
exit(0);
}
print end_html;
print "Parent exiting at " . localtime() . "
\n";
print STDERR "Parent exiting at " . localtime() . "\n";
exit(0);
####
Parent start at Thu Dec 4 09:03:33 2008
First child at Thu Dec 4 09:03:33 2008
Parent forking at Thu Dec 4 09:03:38 2008
Parent forking again at Thu Dec 4 09:03:38 2008
Parent exiting at Thu Dec 4 09:03:38 2008
Second child at Thu Dec 4 09:04:28 2008
####
[Thu Dec 04 09:03:33 2008] [error] [client 127.0.0.1] Parent start at Thu Dec 4 09:03:33 2008
[Thu Dec 04 09:03:33 2008] [error] [client 127.0.0.1] First child at Thu Dec 4 09:03:33 2008
[Thu Dec 04 09:03:38 2008] [error] [client 127.0.0.1] Parent forking at Thu Dec 4 09:03:38 2008
[Thu Dec 04 09:03:38 2008] [error] [client 127.0.0.1] Parent forking again at Thu Dec 4 09:03:38 2008
[Thu Dec 04 09:03:38 2008] [error] [client 127.0.0.1] Parent exiting at Thu Dec 4 09:03:38 2008
[Thu Dec 04 09:04:28 2008] [error] [client 127.0.0.1] Second child at Thu Dec 4 09:04:28 2008
[Thu Dec 04 09:04:28 2008] [error] [client 127.0.0.1] File does not exist: /var/www/html/favicon.ico, referer: http://localhost/cgi-bin/test.pl
[Thu Dec 04 09:05:18 2008] [error] [client 127.0.0.1] Second child after STDOUT close at Thu Dec 4 09:05:18 2008