僵进程

| 评论(4) | 暂无引用通告

-->
  对于僵进程,wikipedi上是这样描述的:

On Unix and Unix-like computer operating systems, a zombie process or defunct process is a process that has completed execution but still has an entry in the process table, this entry being still needed to allow the process that started the zombie process to read its exit status. The term zombie process derives from the common definition of zombie--an undead person. In the term's colorful metaphor, the child process has died but has not yet been reaped.

When a process ends, all of the memory and resources associated with it are deallocated so they can be used by other processes. However, the process's entry in the process table remains. The parent can read the child's exit status by executing the wait system call, at which stage the zombie is removed. The wait call may be executed in sequential code, but it is commonly executed in a handler for the SIGCHLD signal, which the parent is sent whenever a child has died.

After the zombie is removed, its process ID and entry in the process table can then be reused. However, if a parent fails to call wait, the zombie will be left in the process table. In some situations this may be desirable, for example if the parent creates another child process it ensures that it will not be allocated the same process ID. As a special case, under Linux, if the parent explicitly ignores the SIGCHLD (sets the handler to SIG_IGN, rather than simply ignoring the signal by default), all child exit status information will be discarded and no zombie processes will be left.

A zombie process is not the same as an orphan process. An orphan process is a process that is still executing, but whose parent has died. They don't become zombie processes; instead, they are adopted by init (process ID 1), which waits on its children.

Zombies can be identified in the output from the Unix ps command by the presence of a "Z" in the STAT column. Zombies that exist for more than a short period of time typically indicate a bug in the parent program. As with other leaks, the presence of a few zombies isn't worrisome in itself, but may indicate a problem that would grow serious under heavier loads. Since there is no memory allocated to zombie processes except for the process table entry itself, the primary concern with many zombies is not running out of memory, but rather running out of process ID numbers.

To remove zombies from a system, the SIGCHLD signal can be sent to the parent manually, using the kill command. If the parent process still refuses to reap the zombie, the next step would be to remove the parent process. When a process loses its parent, init becomes its new parent. Init periodically executes the wait system call to reap any zombies with init as parent.

  以前,我一直认为使用kill -9杀死进程时,因其未等待子进程,而会使其子进程变为僵进程,而其子进程被init收养后,将会被一直存留在系统中直到系统重启,现在看来大错特错,因为一旦进程被init收养,init被定期wait子进程,僵进程也就不会驻留在系统中了,对僵进程是指子进程已死,释入了大部分资源(保留了进程号及退出状态及其他信息)而没有被父进程wait。

杀死僵进程的方法:

   发送SGICHLD信号给其父进程,通知其wait子进程,如果父进程仍无行动可以杀死父进程,子进程被init收养,init将周期性执行wait。

  通过top的进程统计信息来了解僵进程的数量:

 14:55:32  up 277 days,  4:59,  1 user,  load average: 0.13, 0.14, 0.10
220 processes: 193 sleeping, 1 running, 26 zombie, 0 stopped
CPU states:  cpu    user    nice  system    irq  softirq  iowait    idle
           total    2.9%    0.0%    1.9%   0.0%     0.0%    0.0%   95.0%
Mem:  3082448k av, 3064196k used,   18252k free,       0k shrd,   85172k buff
                   1803992k actv,  544844k in_d,   59084k in_c
Swap: 1024056k av,     496k used, 1023560k free                 2048520k cached

  使用grep、awk过滤ps的输出来获取僵进程进程号(ps输出的STAT列中标为Z的即为僵进程):
[root@mail root]# ps aux | awk '{ print $8 " " $2 }' | grep -w Z
Z 7522
Z 7523
Z 15590
Z 15670
Z 12029
Z 18311
Z 19289


另外,可以参考:
理解Zombie和Daemon Process
What are these zombie processes that show up in ps? I kill them but they don't go away! by David Newall.

暂无引用通告

发送引用通告网址: http://supersun.info/mt/mt-tb.cgi/822
如果您想引用这篇日记到您的Blog,请复制上面的链接,放置到您发表文章时的相应界面中。

评论(4)

Supersun:
圣诞节快到了,Merry Chirstmas!

圣诞快乐,呵呵!

supersun:
偶的博客标题已经改成SINO BLOG啦,麻烦你修改一下友情链接哦~~

已更新

发表评论

最新资源

  • IMG_1437.JPG
  • line.png
  • bar.png
  • perl_calander.jpg

关于此日记

此日记由 supersun 发表于 2007年12月21日 15:00

此Blog上的上一篇日记进程查看器htop

此Blog上的下一篇日记获取http的连接数

首页归档页可以看到最新的日记和所有日记。