in reply to Reaping Zombies (dont wanna wait)

Let's do this the easy way. There will be a wait() in the code below, but it's for a process that's already exited.
unless (fork) { # this is the child unless (fork) { # this is the grandchild # Do your request processing here ... my $exec = "/usr/local/bin/monster -i$id $filePath$file &"; qx/$exec/; exit 0; } # end grandchild # Child process just exits. exit 0; } # Parent reaps quick-exiting child. wait;
The grandchild has no parent (child exits immediately), so it gets switched over to be a subprocess of init by the scheduler.

The child exits immediately, so it doesn't need to wait for its child.

The parent waits for the child, so the zombie child is cleaned up right away.

You can add all the redo logic to this relatively easily; I've left it out so as to not obscure the simplicity of the mechanism. The mainline code does wait, but since the code it's waiting on runs extremely fast, it works more like relinquishing control to the child just long enough for it to exit.

As a funny aside, this is from the Perl 4 Programming Perl.

Replies are listed 'Best First'.
Re: Re: Reaping Zombies (dont wanna wait)
by Anonymous Monk on Jun 11, 2003 at 18:53 UTC
    Dear All,

    Thanks for your input, understanding forks better, I realised that since I didnt want control of the child, i used 'exec()':

    FORK: { $child = fork; if (defined $child) { if($child == 0){ exec("/usr/local/bin/monster", "-i$id", "$filePath$file") or die "couldn't exec monster: $!"; } }elsif ($! == EAGAIN) { sleep 5; redo FORK; }else { die "Can't fork: $!\n"; } }

    However, now the child refuses to exit, which is strange. It wont even 'die' and print the error message if any.

    I tried this very same thing with pemungkah's suggestion, and again it happened, the child won't exit.

    Repeated requests creates new children, but they are all hanging there...

    Anyone?

    Sam

      woops, forgot to sign in. This 'A.M.' is actually seaver.
      Try isolating the "must fork" code; trying to get the redo sorted out was driving me nuts:
      sub insistent_fork { my $pid = undef; FORK: { if (defined ($pid = fork())) { return $pid; } elsif ($! =~ /No more process/) { sleep 5; redo FORK; } else { die "Can't fork: $!\n"; } } }
      You can pair this with the original code I posted, substituting insistent_fork for fork. (Works on OS X.)
Re^2: Reaping Zombies (dont wanna wait)
by dragonchild (Archbishop) on Jul 09, 2004 at 15:48 UTC
    Has this ever been placed into a CPAN module? If so, which one? If not, which one should it be patched into (as an option?)?

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested