Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-12-02
8
Medium Priority
?
7,201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

719 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