perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Robert Landrum <>
Subject Re: fork/CGI/Apache problem
Date Fri, 02 Nov 2001 14:24:06 GMT
At 2:30 PM +0100 11/2/01, Marco Kleefman wrote:
>Hello everybody,
>I am new to the list. I have a question I hope you can help me with. It is
>not a modperl question, just a normal perl question related to Apache/CGI.
>I have this script which forks off a process which does something. I already
>encountered the problem of my browser waiting for the child to be finished.
>I solved it by closing STDIN/OUT/ERR. But I still have another problem...
>Somehow all print statements in my script which occur before the fork-part
>of the script are printed twice! I have tried to unbuffer STDOUT, but no
>luck until now...
>Here's my script:
>#! /usr/bin/perl
>print "start: pid=$$\n"; # this line gets printed twice!
>print "Content-type: text/html\n\n"; # this line gets printed twice!
>if (!defined ($pid = fork))
>    die "Unable to fork: $!\n";
>elsif (! $pid)
>    warn "child: pid=$$\n";
>    # this is the branch for the child process
>    close(STDIN); close(STDOUT); close(STDERR);

You need to close these before you fork.  Right now, you're printing 
your headers out to the buffer.  When you call close, it 
automatically flushes the buffer, and since there are now two buffers 
(you've forked at this point) it prints twice.


"Only two things are infinite: The universe, and human stupidity. And I'm not
sure about the former." --Albert Einstein

View raw message