in reply to Fork child
Update: I added a second write in the second child, after closing STDOUT in the child. The output to STDERR is still written to the error log.
I was quite surprised that output from a child process, to both STDOUT and STDERR and long after the parent process had exited, was included in the response and written to the error log. This was on Apache on Linux (CentOS 5.2).
#!/usr/bin/perl use strict; use warnings; use CGI qw(:standard); print header, start_html(-title=>'fork test',); print "Parent start at " . localtime() . "<br>\n"; print STDERR "Parent start at " . localtime() . "\n"; if(my $pid = fork()) { sleep(5); print "Parent forking at " . localtime() . "<br>\n"; print STDERR "Parent forking at " . localtime() . "\n"; wait(); } else { print "First child at " . localtime() . "<br>\n"; print STDERR "First child at " . localtime() . "\n"; exit(0); } if(my $pid = fork()) { print "Parent forking again at " . localtime() . "<br>\n"; print STDERR "Parent forking again at " . localtime() . "\n"; } else { sleep(50); print "Second child at " . localtime() . "<br>\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() . "<br>\n"; print STDERR "Parent exiting at " . localtime() . "\n"; exit(0);
This produced the following in the browser:
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
And the following in the error log
[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 T +hu Dec 4 09:03:33 2008 [Thu Dec 04 09:03:38 2008] [error] [client 127.0.0.1] Parent forking a +t Thu Dec 4 09:03:38 2008 [Thu Dec 04 09:03:38 2008] [error] [client 127.0.0.1] Parent forking a +gain at Thu Dec 4 09:03:38 2008 [Thu Dec 04 09:03:38 2008] [error] [client 127.0.0.1] Parent exiting a +t 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 ex +ist: /var/www/html/favicon.ico, referer: http://localhost/cgi-bin/tes +t.pl [Thu Dec 04 09:05:18 2008] [error] [client 127.0.0.1] Second child aft +er STDOUT close at Thu Dec 4 09:05:18 2008
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Fork child
by gayathriAthreya (Novice) on Dec 03, 2008 at 20:31 UTC | |
by ig (Vicar) on Dec 03, 2008 at 21:09 UTC | |
by gayathriAthreya (Novice) on Dec 04, 2008 at 16:55 UTC |