Solved

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

Posted on 2003-12-02
8
7,122 Views
Last Modified: 2007-12-19
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
Comment
Question by:svgkraju
8 Comments
 
LVL 20

Expert Comment

by:jmcg
ID: 9859325
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
 

Author Comment

by:svgkraju
ID: 9859419
Is it possible to kill the process created by exec first and then kill the child process. If so, how to do it?
0
 
LVL 3

Expert Comment

by:terageek
ID: 9860471
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:svgkraju
ID: 9864438
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
 
LVL 20

Expert Comment

by:jmcg
ID: 9864572
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
 
LVL 20

Expert Comment

by:jmcg
ID: 9910074
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
 
LVL 20

Expert Comment

by:jmcg
ID: 10218618
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
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 10276570
PAQed, with points refunded (70)

Computer101
E-E Admin
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sort hash by values desc 2 193
Vb script to unzip a files and rename the files 5 89
combine multiple lines 2 59
Perl Untar File 1 41
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now