天天看點

Linux 僵屍程序清除

僵屍程序概念

僵屍程序(zombie process)通俗來說指那些雖然已經終止的程序,但仍然保留一些資訊,等待其父程序為其收屍.

書面形式一點:一個程序結束了,但是他的父程序沒有等待(調用wait /

waitpid)他,那麼他将變成一個僵屍程序。通過ps指令檢視其帶有defunct的标志。僵屍程序是一個早已死亡的程序,但在程序表

(processs table)中仍占了一個位置(slot)。

 

 但是如果該程序的父程序已經先結束了,那麼該程序就不會變成僵屍程序。因為每個程序結束的時候,系統都會掃描目前系統中所運作的所有程序,看看有沒有哪

個程序是剛剛結束的這個程序的子程序,如果是的話,就由init程序來接管他,成為他的父程序,進而保證每個程序都會有一個父程序。而init程序會自動

wait其子程序,是以被init接管的所有程序都不會變成僵屍程序

    與zombie對應的程序狀态還有running(正在運作或等待運作狀态),uninterruptable(不可中斷阻塞狀态),interruptable(可中斷阻塞狀态),stopped(挂起狀态)等。

關于僵屍程序的維基百科介紹:

on

unix and unix-like computer operating systems, a zombie process or

defunct process is a process that has completed execution (via the exit

system call) but still has an entry in the process table: it is a

process in the "terminated state". this occurs for child processes,

where the entry is still needed to allow the parent process to read its

child's exit status: once the exit status is read via the wait system

call, the zombie's entry is removed from the process table and it is

said to be "reaped". a child process always first becomes a zombie

before being removed from the resource table. in most cases, under

normal system operation zombies are immediately waited on by their

parent and then reaped by the system – processes that stay zombies for a

long time are generally an error and cause a resource leak.

the

term zombie process derives from the common definition of zombie — an

undead person. in the term's metaphor, the child process has "died" but

has not yet been "reaped". also, unlike normal processes, the kill

command has no effect on a zombie process.

zombie

processes should not be confused with orphan processes: an orphan

process is a process that is still executing, but whose parent has died.

these do not remain as zombie processes; instead, (like all orphaned

processes) they are adopted by init (process id 1), which waits on its

children. the result is that a process that is both a zombie and an

orphan will be reaped automatically.

僵屍程序檢視

檢視系統裡面有那些僵屍程序,有很多方法,例如top指令,ps指令等

Linux 僵屍程式清除

另外,使用ps和grep指令結合也能檢視僵屍程序,當然有非常多的形式,如下所。

檢視僵屍程序的個數指令

僵屍程序清除

僵屍程序的清除有時候是一個頭痛的問題,僵屍程序有時候很殺不掉,有時候還不能亂殺。

Linux 僵屍程式清除

要殺掉僵屍程序,一般有兩個方法:

1:找到該defunct僵屍程序的父程序,将該程序的父程序殺掉,則此defunct程序将自動消失

2:重新開機伺服器。

檢視僵屍程序并殺掉

一般情況下,不建議莽撞的kill掉這些僵屍程序,還是檢查一下具體原因後,根據具體情況再做清除,如下所示。

檢視僵屍程序的父程序,發現是對應的是oracle裡面pspo程序,關于這個程序,我也沒有把握是否可以kill掉。是以選擇重新開機伺服器比較保險一點。

Linux 僵屍程式清除

繼續閱讀