Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using a Double-Fork in CGIC to Avoid Timeouts

42 views
Skip to first unread message

chad...@gmail.com

unread,
Aug 8, 2006, 10:37:35 PM8/8/06
to
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

Chris Davies

unread,
Aug 9, 2006, 11:09:21 AM8/9/06
to
chad...@gmail.com wrote:
> 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

0 new messages