in reply to automatically killing all child processes created in a script

I may be mis-understanding something here, but normal way would be to install handler for $SIG{CHLD} in the parent, like:
#kill zombie children $SIG{CHLD} = 'reaper'; while (1) { server code to accept new clients and fork()... } sub reaper { my ($kidpid); while (($kidpid = waitpid(-1,WNOHANG))>0 ) { print "Reaped $kidpid\n"; } close $client; }
Update: I think: use POSIX qw(sys_wait_h); is needed too.

Replies are listed 'Best First'.
Re^2: automatically killing all child processes created in a script
by JavaFan (Canon) on Aug 21, 2009 at 23:08 UTC
    That waits for all the children to finish - clearly quite the opposite of what the OP wants.
      This does not wait for all children to finish. It processes multiple children from the same SIG CHLD signal if they are "ready". If the parent "dies", then you as a child eventually get this CHLD signal and you get removed from the process table. This is a non-blocking situation, that's what WNOHANG does - it doesn't "hang up" waiting for anybody. If say 3 of 20 children have CHLD signal at the same time, then you have to process those 3 children at once. After that, then the other 17 is a different story. So no, this will not "hang" and wait for all children to finish. This just says, "hey at least one and maybe more than one children" are in CHLD signal state". "At least one" does not mean "all", and it doesn't mean wait for all children to be in CHLD signal state!

      Ok, then if you are launching servers that spawn other child processes, then you should keep track of the PID's or names of those things. If you have permission level that allows you to kill that server, then its children will also be killed if you have right signal handling installed.

        Ok, it doesn't wait for children to die. But the code you present is code that runs when children die. That's not what the OP asked. The OP asked to *kill* the children. And its children.
      Correct - the traditional reaper is the opposite of what I want. The processes are persistent servers and will never die on their own, so I'd be waiting forever.