Solved

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

Posted on 2003-12-02
8
7,088 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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
binary to char / hexadecimal 5 98
perl CPAN issue 3 90
perl to convert excel to csv 3 150
Regex rule to match two different url 5 37
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

13 Experts available now in Live!

Get 1:1 Help Now