Chinaunix

标题: How to kill a zombie process in Linux? [打印本页]

作者: superfluous    时间: 2009-09-07 22:52
标题: How to kill a zombie process in Linux?
When parent cannot handle the exited child process, the child process becomes a zombie, also known as a defunct process. The defunct process uses a little resource. It cannot be killed by kill(). It exists in all the system life. Then how to kill a zombie process? There are several ways to kill a zombie process or avoid to generate a zombie process as follows:
1. Kill the parent to eliminate the zombie processes
2. Wait a child to exit through calling wait() to avoid to generate a zombile process
3. Ignore the SIGCHLD signal (in Linux 2.6 but 2.4; POSIX-2001) to avoid to generate a zombie process
It should be noted that a zombie process is defunct so that any kill() or related command cannot affect it.
example 1: Call signal() to avoid to generate a zombie process
           The main process(parent process) is not blocked
...
signal(SIGCHLD, SIG_IGN);
...
if ((pid = fork()) == -1){
  ...
}
else if (!pid) {
  ...
  exit(0);
}
...
$kill -9 {CHILD}
$ps x
/* child not ba a zombie */
example 2: Call wait() to avoid to generate a zombie process
           The main process(parent process) is blocked
...
if ((pid = fork()) == -1){
  ...
}
else if (!pid) {
  ...
  exit(0);
}
wait(&child_exit_status);
...
$kill -9 {CHILD}
$ps x
/* child not ba a zombie */
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/103132/showart_2047620.html




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2