Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7218
  • Last Modified:

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
0
svgkraju
Asked:
svgkraju
1 Solution
 
jmcgOwnerCommented:
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.
0
 
svgkrajuAuthor Commented:
Is it possible to kill the process created by exec first and then kill the child process. If so, how to do it?
0
 
terageekCommented:
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
svgkrajuAuthor Commented:
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");
}
0
 
jmcgOwnerCommented:
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.

0
 
jmcgOwnerCommented:
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.
0
 
jmcgOwnerCommented:
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
0
 
Computer101Commented:
PAQed, with points refunded (70)

Computer101
E-E Admin
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now