stu
stu

Reputation: 8805

How can I get the PID of a child process that died, in the parent process in perl?

I'm using proc::queue to manage a pile of child processes, and I fire them off like this:

if(Proc::Queue::running_now() < $kids)
  {
    logger("Starting another server process...");
    my $newprocessid = Proc::Queue::run_back(\&serverprocess);
    logger("New child process id: $newprocessid");
    $childprocessids{$newprocessid} = 1;
  }

So now I have a hash of the child process IDs that I can kill when the parent process is sent a kill SIGTERM.

But what if the child process is killed externally? The parent process knows to fire up another child to make up for the lost one, and the new child PID ends up in my hash, but how can the parent find out the PID of the child that died to remove it from the hash so I don't try and kill it later?

I can set $SIG{CHLD} but I don't see how to get the child PID to remove it from my hash.

Upvotes: 1

Views: 1767

Answers (2)

Greg Bacon
Greg Bacon

Reputation: 139701

You might place the parent and its children in their own process group and kill the whole family by sending a signal to the parent.

Depending on the nature of your problem, you may be willing to kill away (Mr. McManus!) and live with the failures due to each attempted kill on a child processes that is already dead.

If the parent process does nothing but track the kids, a simple waitpid loop is all you need.

while ((my $kid = waitpid -1, 0) > 0) {
  warn "$0: [$$] reaped $kid\n";
  delete $kid{$kid};
}

If you have other processing in the parent process, setting the WNOHANG bit in the second argument to waitpid tells the system call not to block. This allows you to periodically reap zombie children and then go back to other processing.

For demonstration purposes, say we start up a bunch of sleepy children.

#! /usr/bin/env perl

use strict;
use warnings;

use 5.10.0;  # for defined-or

my %kid;

for (1 .. 5) {
  my $pid = fork // die "$0: fork: $!";  # / fix SO hilighting
  if ($pid == 0) {
    warn "$0: [$$] sleeping...\n";
    sleep 10_000;
    exit 0;
  }
  else {
    $kid{$pid} = 1;
    warn "$0: [$$] forked $pid\n";
  }
}

Then to simulate kills from outside, we fork another child to pick off the rest of its siblings at random.

my $pid = fork // die "$0: fork: $!";
if ($pid == 0) {
  warn "$0: [$$] The killer awoke before dawn.\n";
  while (keys %kid) {
    my $pid = (keys %kid)[rand keys %kid];
    warn "$0: [$$] killing $pid...\n";
    kill TERM => $pid or warn "$0: [$$] kill $pid: $!";
    delete $kid{$pid};
    sleep 1;
  }
  exit 0;
}

Now the loop from above reads the obituaries.

while ((my $kid = waitpid -1, 0) > 0) {
  warn "$0: [$$] reaped $kid\n";
  delete $kid{$kid};
}

Double-check that no one made it out alive.

if (keys %kid) {
  my $es = keys %kid == 1 ? "" : "es";
  die "$0: unkilled process$es:\n",
      map "  - $_\n", keys %kid;
}

Output:

./waitpid-demo: [1948] forked 7976
./waitpid-demo: [7976] sleeping...
./waitpid-demo: [1948] forked 7244
./waitpid-demo: [7244] sleeping...
./waitpid-demo: [1948] forked 4776
./waitpid-demo: [4776] sleeping...
./waitpid-demo: [1948] forked 4304
./waitpid-demo: [4304] sleeping...
./waitpid-demo: [1948] forked 7908
./waitpid-demo: [7908] sleeping...
./waitpid-demo: [5144] The killer awoke before dawn.
./waitpid-demo: [5144] killing 7908...
./waitpid-demo: [1948] reaped 7908
./waitpid-demo: [5144] killing 7976...
./waitpid-demo: [1948] reaped 7976
./waitpid-demo: [5144] killing 4776...
./waitpid-demo: [1948] reaped 4776
./waitpid-demo: [5144] killing 4304...
./waitpid-demo: [1948] reaped 4304
./waitpid-demo: [5144] killing 7244...
./waitpid-demo: [1948] reaped 7244
./waitpid-demo: [1948] reaped 5144

Upvotes: 4

ArtMat
ArtMat

Reputation: 2150

If the only issue is "remove it from the hash so I don't try and kill it later", you could try the following:

# probably you should put this code in a loop for all your process IDs
$alive = kill 0, $childprocessids{$processId};

if ( $alive ) {
    # process is still alive, do whatever you want with it
}
else {
    # process is dead, probably killed externally
    # do whatever you need for this scenario or just delete your hash key
}

Upvotes: 0

Related Questions