转-Zombie Processes in Linux Explained

from: http://www.brighthub.com/computing/linux/articles/79186.aspx
You might have seen zombie processes listed along with your Linux computer's other processes. But what are they and why cant they be killed like normal processes?
1.Why Cant I Kill Zombie Processes?
It's possible to end any Linux process using the "kill" command or by using your desktop's process monitor. In particular, the "kill -9" command instantly(立刻) terminates any specified process without giving it a chance to save its data properly and exit.
So, if any process can be killed, why can't a zombie process be killed? In short, zombie processes are already dead, hence the name. Zombie processes aren't really processes at all(僵尸进程根本就不是进程). Zombie processes are the leftover bits of dead processes that haven't been cleaned up properly by their parent process. A zombie process on a computer isn't a problem, takes up very little resources and can be ignored. However, if you have many zombie processes, this could indicate a software bug.
2.Causes of Zombie Processes
When a subprocess exits, its parent is supposed to use the "wait" system call and collect the process's exit information. The subprocess exists as a zombie process until this happens, which is usually immediately. However, if the parent process isn't programmed properly or has a bug and never calls "wait," the zombie process remains, eternally waiting for its information to be collected by its parent.
3.Killing Zombie Processes
Zombie processes persist until their parent process ends(结束), at which point they are adopted by the "init" system process and shortly cleaned up. However, there's no way to get rid of a zombie process without ending its parent process. If you have a lot of zombie processes, close and restart the parent process or service. Init adopts and cleans up the orphaned zombie processes.
If you can't close the parent process, don't worry, zombies won't affect the performance of your computer unless a very large amount are present. However, bear in mind that, if a process is creating a lot of zombies, it has a programming bug or error in its code and isn't working correctly.
4.Viewing Zombie Processes
click to enlargeExecute the "top" command in a Terminal window. The top command shows the number of zombie processes at the upper-right side of its output, in the Tasks: row. //top命令的右上角
You can also list running processes by executing the "ps aux" command. Zombie processes have a "z" listed in their Stat column in the output of the 'ps aux" command. //标Z的是僵尸进程
5.Risks of Zombie Processes
While zombie processes aren't a problem in and of themselves and take up very little resources, there is one concern. Linux systems have a maximum amount of processes and thus process ID numbers. If a computer has enough zombie processes, the maximum amount is reached and new processes can't be launched. //进程号是有限的
The maximum amount of processes can be listed by typing the "cat /proc/sys/kernel/pid_max" in a Terminal window and is usually 32768. Thus, zombie processes are usually not a concern.
However, if the parent process creating zombie processes is server software that isn't written properly, a large amount of zombies could be created under load. Or, zombies could gradually accumulate over long periods of time until the maximum process limit is reached. A few zombies are nothing to worry about, but if you need to kill a lot of zombie processes, your software needs to be fixed.

问:僵尸状态是每个子进程必经的状态吗?
答:是的。 任何一个子进程(init除外)在exit()之后,并非马上就消失掉,而是留下一个称为僵尸进程(Zombie)的数据结构,等待父进程处理。这是每个 子进程在结束时都要经过的阶段。如果子进程在exit()之后,父进程没有来得及处理,这时用ps命令就能看到子进程的状态是“Z”。如果父进程能及时 处理,可能用ps命令就来不及看到子进程的僵尸状态,但这并不等于子进程不经过僵尸状态。 * 如果父进程在子进程结束之前退出,则子进程将由init接管。init将会以父进程的身份对僵尸状态的子进程进行处理。

僵尸进程的避免:
1、父进程通过wait和waitpid等函数等待子进程结束,这会导致父进程挂起。
2. 如果父进程很忙,那么可以用signal函数为SIGCHLD安装handler,因为子进程结束后, 父进程会收到该信号,可以在handler中调用wait回收。
3. 如果父进程不关心子进程什么时候结束,那么可以用signal(SIGCHLD, SIG_IGN)通知内核,自己对子进程的结束不感兴趣,那么子进程结束后,内核会回收, 并不再给父进程发送信号。
4. 还有一些技巧,就是fork两次,父进程fork一个子进程,然后继续工作,子进程fork一 个孙进程后退出,那么孙进程被init接管,孙进程结束后,init会回收。不过子进程的回收 还要自己做。

 

ps axu 显示进程的详细状态。
ps axf 看进程树,以树形方式现实进程列表。
ps axm 会把线程列出来,在linux下进程和线程是统一的,是轻量级进程的两种方式。

你可能感兴趣的:(转-Zombie Processes in Linux Explained)