Reaping child processes from Perl

EMiller picture EMiller · Jun 7, 2012 · Viewed 8.7k times · Source

I have a script that spawns a set of children. The parent must wait for each of the children to finish.

My script performs similar to the following perl script:

#! /usr/bin/perl
use strict;
use warnings;

print "I am the only process.\n";

my @children_pids;

for my $count (1..10){
        my $child_pid = fork();
        if ($child_pid) {  # If I have a child PID, then I must be the parent
                push @children_pids, $child_pid;
        }
        else { # I am the child
                my $wait_time = int(rand(30));
                sleep $wait_time;
                my $localtime = localtime;
                print "Child: Some child exited at $localtime\n";
                exit 0; # Exit the child
        }
}

foreach my $child (@children_pids) {
        print "Parent: Waiting on $child\n";
        waitpid($child, 0); 
        my $localtime = localtime;
        print "Parent: Child $child was reaped - $localtime.\n";
}

print "All done.\n";

Similar to the code I've provided above, each child may take a different time to finish.

The problem is when I try to reap the children by looping over the children PIDs, in that last foreach block, the parent waits for the children in the order that they are created.

Obviously the children do not finish in the order which they are spawned and so I'm left with a bunch of zombie processes for children that happen to finish early.

In my actual code, these children may finish days before one another and the number of zombie processes floating around can grow in the hundreds.

Is there a better way for me to reap a set of children?

Answer

Borodin picture Borodin · Jun 7, 2012

If your parent process doesn't need to be aware of its children's completion status then you can just set

$SIG{CHLD} = 'IGNORE';

which will automatically reap all children as they complete.

If you do need to be informed of the children completing, then the signal handler needs to be set to reap all possible processes

use POSIX ();

$SIG{CHLD} = sub {
  while () {
    my $child = waitpid -1, POSIX::WNOHANG;
    last if $child <= 0;
    my $localtime = localtime;
    print "Parent: Child $child was reaped - $localtime.\n";
  }
};