Link to home
Start Free TrialLog in
Avatar of svgkraju
svgkrajuFlag for India

asked on

How to kill all children of a child process from parent process.

The program is as follows:

# file name : killChild.pl

use POSIX ":sys_wait_h";
use strict;

my $etime = time + 1;
my $pid;

sub sigChild {
      print "in the signal handler\n";
}

if ($pid = fork()) {
      my $child;
      do {
            $child = waitpid($pid, &POSIX::WNOHANG);
      } while time < $etime;
      kill "KILL", $pid;
} else {
      exec ("perl t.pl");
}

# file name: t.pl

use strict;

while() {
      print "i am here\n";
}

Actual scenario is, sometimes t.pl (not in this program) is getting hanged and I am forced to kill it from the parent process. When I am killing the child process, the exec i.e. perl t.pl is becoming a zombie process.

How do I kill the zombie process also when I kill the child process? i.e. when I kill the child process all its children must also be killed.

How to do it? Any idea?

regds
-raju
Avatar of jmcg
jmcg
Flag of United States of America image

Zombie processes cannot be killed. They are already dead. All that is left for them to do is hand their status back to a parent.

If you send a signal to a child process with the expectation that it will terminate, you can use the 'waitpid' call  to collect its status so that it will not hang around as a zombie.
Avatar of svgkraju

ASKER

Is it possible to kill the process created by exec first and then kill the child process. If so, how to do it?
Avatar of terageek
terageek

When I tried your code, t.pl terminated with the kill.  I assume that the problem is that t.pl spawns children which become zombies.  One thing that you can try to do is send a different, trappable signal (TERM) to the child.  Put a signal handler in the t.pl which will kill it's children and exit gracefully.
I found the answer. Send a kill signal to the process group. Change killChild.pl as follows:

# file name : killChild.pl

use POSIX ":sys_wait_h";
use strict;

my $etime = time + 1;
my $pid;

if ($pid = fork()) {
     my $child;
     do {
          $child = waitpid($pid, &POSIX::WNOHANG);
     } while time < $etime;
     kill -1, $pid; # Use -1 to kill process group
} else {
     setpgrp 0, $$; # make child itself as group id.
     exec ("perl t.pl");
}
Why the busy loop on waitpid?

The length of time it takes for the loop condition to be true could be a tiny fraction of a second, if you hit things just right. The value returned by "time" is quantized in seconds, but the underlying clock ticks may be somewhere between 16 msec all the way down to a few microseconds. If you calculate $etime at a point just a few of these tiny ticks away from the next second, your child process will not have much of a chance to even get started before you're sending it a SIGHUP signal.

I suggest doing the following in the parent:

  sleep 2;
  kill -1, $pid;
  sleep 1;
  $child = waitpid( $pid, &POSIX::WNOHANG);

as a better way to prevent zombies.

If you like your answer and don't consider any of what we've said here as contributing to your solution, you can go to Community Support and request that a moderator PAQ this question and refund your points.
Nothing has happened on this question in more than 7 weeks. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
PAQ, refund points (asker posted a solution).

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial