[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: using fork in cgi & perl

Ricardo Kleemann wrote:
> Hi,
> I'm having a hard time getting fork to work correctly in a cgi script.
> Basically I have a time consuming process which I want to be done in the
> background.
> I have something like this:
> unless (fork) {
> # print out result html page
> exit 0;
> }
> else {
> # do time-intensive stuff
> }
> What's happening is that the child process executes (and becomes zombie)
> and the parent process continues to chug along. The cgi result html only
> comes up after the long process finishes...
> I've tried switching the parent & child to see if it makes a difference,
> but it doesn't. In every case, the html output goes into zombie state, and
> the result page is only displayed after the entire process is complete.
> Almost as if the fork isn't working as intended...
> Any ideas?
> Thanks,
> Ricardo

Well, the child is a zombie because the process which called fork
did not wait() on the child process. This can be accomplished 
asynchronously by handling the SIGCHLD signal. The following (somewhat
modified to remove unecessary SysV stuff) example comes from the
perlipc manpage:

    sub REAPER { 
        $waitedpid = wait;

    $SIG{CHLD} = \&REAPER;
    # now do something that forks...

That code will make sure the child doesn't sit there as a zombie.
As for why the result doesn't come up quickly I can't speculate 
since I don't know what you're doing.

Jens B. Jorgensen

TO UNSUBSCRIBE FROM THIS MAILING LIST: e-mail the word "unsubscribe" to
debian-user-REQUEST@lists.debian.org . Trouble? e-mail to Bruce@Pixar.com

Reply to: