Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: ModPerl: ModPerl

fork/CGI/Apache problem

 

 

ModPerl modperl RSS feed   Index | Next | Previous | View Threaded


marco.kleefman at cmg

Nov 2, 2001, 6:30 AM

Post #1 of 3 (1865 views)
Permalink
fork/CGI/Apache problem

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);
sleep(60); # pause for 1 minute
exit; # terminate the child process
}
else
{ # this is the branch for the parent
warn "parent: pid=$$ (child=$pid)\n";
...print some HTML code....
exit; # terminate the parent process
}

Best regards,

Marco
The Netherlands

PS I have searched 30 pages of old messages related to forking, but no luck
there! Still I have the feeling that my problem is something trivial... :(


rlandrum at capitoladvantage

Nov 2, 2001, 7:24 AM

Post #2 of 3 (1780 views)
Permalink
Re: fork/CGI/Apache problem [In reply to]

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.

Rob

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


Charles.Day at frontstep

Nov 2, 2001, 7:53 AM

Post #3 of 3 (1775 views)
Permalink
RE: fork/CGI/Apache problem [In reply to]

You script only printed once for me.

Here's a shortened version I use:

....parent stuff above

defined($pid = fork) or die "cannot fork: $!\n";
exit if $pid;
close (STDOUT);

...child stuff below





-----Original Message-----
From: Marco Kleefman [mailto:marco.kleefman[at]cmg.nl]
Sent: Friday, November 02, 2001 8:30 AM
To: modperl[at]apache.org
Subject: fork/CGI/Apache problem


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);
sleep(60); # pause for 1 minute
exit; # terminate the child process
}
else
{ # this is the branch for the parent
warn "parent: pid=$$ (child=$pid)\n";
...print some HTML code....
exit; # terminate the parent process
}

Best regards,

Marco
The Netherlands

PS I have searched 30 pages of old messages related to forking, but no luck
there! Still I have the feeling that my problem is something trivial... :(

ModPerl modperl RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.