in reply to Reaping Zombies

It has been several years since i looked at this stuff. First, i recommend you have a look at Advanced Programming in the Unix Environment. It's in C, but the concepts apply to Perl as well. Having said that, back in 2002 i had to write a forking server. This is the reaper code that i came up with:

use POSIX qw(WNOHANG); our (%PID); sub reaper { while ((my $kid = waitpid(-1,WNOHANG)) > 0 ) { warn "Reaped child with PID $kid"; my $status = $? >> 8; $PID{$kid} = $status; } }
Very simple, simply keep reaping children and store their status in a hash so that the server which did the forking can return the status to the client.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^2: Reaping Zombies
by dsb (Chaplain) on Feb 15, 2006 at 19:00 UTC
    The reaper code I'm using is identical to what I posted...It seems to be expensive it terms of CPU usage. Did you have a similar issue?


    dsb
    This @ISA my( $cool ) %SIG
      It seems to be expensive it terms of CPU usage

      What do you mean exactly? have you found the waitpid system call to be expensive, or are you talking about the full process?

      What else are you doing inside the while loop besides calling the reaper? You should be calling sleep or select or any other thing that causes your program to do nothing for a while!

        If nothing else is done in the loop, just remove WNOHANG instead of sleeping.
        Well I'm thinking it's the call to waitpid().

        I've only just added the multi-threaded processing and the script was using a lot of CPU. With the reaper code, it running at 20-30% CPU usage...eesh


        dsb
        This @ISA my( $cool ) %SIG