Using a Double-Fork in CGIC to Avoid Timeouts

C

chad.vice

I have a CGIC program that generates reporting output on-the-fly. At
times this generation can take a long time, and session timeouts occur
while users wait for the page results to be displayed.

To alleviate this problem, I would like to use fork(), so that a parent
process notifies the user that the report will be e-mailed, while a
detached child process generates and sends the report.

However, it appears that even without a "wait" command, my Apache web
server keeps the parent process alive until the child finishes running.
I'd like to know how to stop the parent process from waiting for the
child to finish. I've read quite a bit about using a "double-fork"
solution for this, but I've yet to find a clear example.

Here is a simplified version of the code as I currently have it:

pid_t pid, sid;

pid = fork();

/* Child Process */
if ( pid == 0 ) {
sid = setsid();

if ( sid < 0 )
exit(-1); /* Exit with failure */

/* Code for Report Generation Goes Here */

exit(0);

/* Parent Process: prints HTML notification with CGIC library */
} else if ( pid > 0 ) {
cgiHeaderContentType("text/html");
fprintf(cgiOut, "<html><body>\n");
fprintf(cgiOut, "<p>The report will be e-mailed.</p>\n");
fprintf(cgiOut, "</body></html>\n");

fclose(cgiOut);
_exit(0);
}

Any help or resource suggestions would be greatly appreciated.

Thanks in advance,
Chad Vice


--
PLEASE NOTE: comp.infosystems.www.authoring.cgi is a
SELF-MODERATED newsgroup. aa.net and boutell.com are
NOT the originators of the articles and are NOT responsible
for their content.

HOW TO POST to comp.infosystems.www.authoring.cgi:
http://www.thinkspot.net/ciwac/howtopost.html
 
C

Chris Davies

However, it appears that even without a "wait" command, my Apache web
server keeps the parent process alive until the child finishes running.

You need to close stdin, stdout, and stderr for the child.
Chris

--
PLEASE NOTE: comp.infosystems.www.authoring.cgi is a
SELF-MODERATED newsgroup. aa.net and boutell.com are
NOT the originators of the articles and are NOT responsible
for their content.

HOW TO POST to comp.infosystems.www.authoring.cgi:
http://www.thinkspot.net/ciwac/howtopost.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top