說說Linux中的訊號處理和殭屍進程的避免

來源:互聯網
上載者:User

 

什麼殭屍進程

 

這裡簡單說一下,詳細的到網上搜一下就知道了:

殭屍進程就是指子進程退出了,而父進程尚未退出,並且沒有對子進程進行wait,致使子進程的資源得不到釋放,依然佔據在記憶體中,從而變成了像"殭屍"一樣的進程(殭屍不能動,卻佔據著身體;進程不能執行了,卻佔據著記憶體等資源)。

這種進程因為不再活動了,不會對訊號進行處理,使用kill向它發送訊號是沒有用的,也就是它變成了殺不死的進程。

 

 

wait是幹什麼的

 

wait不是來打醬油的,看一下man手冊的描述:

All of these system calls are used to wait for state changes in a child of the calling process, and obtain information about the child whose state has changed. A state change is considered to be: the child terminated; the child was stopped by a signal; or the child was resumed by a signal. In the case of a terminated child, performing a wait allows the system to release the resources associated with the child; if a wait is not performed, then the terminated child remains in a "zombie" state (see NOTES below).

這段話的意思是說:wait是當前進程等待它的子進程的狀態變化,並且能夠取得子進程狀態變化的一些資訊。這種狀態變化通常指的是子進程的結束或者恢複。當子進程結束的時候,wait函數將會告知系統釋放該子進程的資源,如果沒有使用wait函數,那麼結束的子進程將會變成"殭屍"狀態。

這種殭屍狀態會一直保持,直到父進程退出,被過繼到老祖宗init(pid為1)進程,由init進程負責釋放它們的資源。

 

殭屍進程的避免

 

網上搜一搜,基本上就是這樣的三種方式:

  1. signal(SIGCHLD, SIG_IGN),忽略SIGCHLD訊號,這樣子進程結束後,就不需要父進程來wait和釋放資源
  2. fork兩次,第一次fork的子進程在fork完成後直接退出,這樣第二次fork得到的子進程就沒有爸爸了(真可憐。。。),它會被自動過繼給老祖宗init進程,init會負責釋放它的資源,這樣就不會由"殭屍"產生了
  3. 對子進程進行wait,釋放它們的資源。但是父進程一般沒工夫在那裡守著,等著子進程的退出,所以,一般使用訊號的方式來處理,在收到SIGCHLD訊號的時候,再臨時用wait操作來釋放它們的資源。

 

從個人角度,簡單評價一下這三種方式:

  1. 老爸不管兒子死活。父進程無法知曉子進程的退出情況。
  2. 兒子自殺了,爺爺不管孫子。跟1一樣,父進程無法知曉子進程的退出情況。
  3. 老爸算是盡職了,兒子死了會給它火化(釋放資源)。父進程可以知曉子進程的退出情況,不過處理比1和2麻煩。

 

個人推薦第3種解決方案,這也引出了下面這樣的一個問題。

在《UNIX環境進階編程》10.8章節中有這樣一段話:

What happens if a blocked signal is generated more than once before the process unblocks the signal? POSIX.1 allows the system to deliver the signal either once or more than once. If the system delivers the signal more than once, we say that the signals are queued. Most UNIX systems, however, do not queue signals unless they support the real-time extensions to POSIX.1. Instead, the UNIX kernel simply delivers the signal once.

這段話的意思是說:如果相同的訊號在被在該進程解除對它的阻塞之前發生了多次,多數UNIX系統並不對該訊號進行排隊處理,也就是說,該訊號將會被只遞交一次。

 

這就是Linux中訊號處理有一個特點,那就是,同一個訊號被遞交多次,如果第一個訊號還在處理,那麼後面的訊號都會被丟棄,而不會進入隊列中等待處理。如果我們只是簡單的對這個訊號處理,必然會丟失對後面相同訊號的處理。

 

這個問題是在我寫一個File Server的時候遇到的。這個檔案伺服器每接收一個Client請求,就fork一個進程對它進行處理。為了對這個Server進行壓力測試,我在Client端產生了非常多的檔案傳輸請求,同時為了測試Server的容錯能力,我在這些請求產生後,按下Ctrl+C鍵中斷了這些檔案傳輸,發現每次執行這樣的操作,Server端都會有大量的殭屍進程產生。研究了一整天,查閱了很多資料,並測試了多次,終於發現,原來這些殭屍進程的SIGCHLD訊號被父進程給忽略了!沒有對他們進行wait,從而導致了他們變成了殭屍進程!

 

下面舉個簡單的例子:

/*<br /> * main.cpp<br /> *<br /> * Created on: Jun 17, 2011<br /> * Author: boyce<br /> */<br />#include <stdio.h><br />#include <stdlib.h><br />#include <unistd.h><br />#include <signal.h><br />#include <wait.h><br />#include <errno.h><br />int num_clients = 0;<br />int dead_clients = 0;<br />void sig_chld_handler(int sig) {<br />pid_t pid;<br />if (sig == SIGCHLD) {<br />pid = wait(NULL);<br />printf("A child dead, current child number: %d, id: %d/n", ++dead_clients, pid);<br />}<br />}<br />int main(int argc, char **argv) {<br />pid_t pid;<br />signal(SIGCHLD, sig_chld_handler);<br />for (int i = 0; i < 30; i++) {<br />if ((pid = fork()) == 0) {<br />exit(0);<br />} else if (pid > 0) {<br />printf("A child created, current child number: %d, id: %d/n", ++num_clients, pid);<br />}<br />}<br />sleep(10);<br />return 0;<br />}<br /> 

 

這段代碼將同時建立30個進程,這些進程將幾乎在同一時間退出,退出之時都會向父進程發送SIGCHLD訊號,按照前面說的,如果上一個訊號尚且在處理中,那麼當前訊號就會被丟棄。如果來一個訊號,我們等待一個子進程,那麼程式運行時,將會發現,總會有那麼一些進程沒有被父進程等到而變成殭屍進程。

就像所示,30個進程裡面居然有9個進程變成了殭屍進程(defunct進程)!這些進程佔據著系統資源不肯釋放,連kill都不能殺死他們,天殺的,這樣都殺不死你!

 

 

不能捕捉到所有的訊號,是不是就表示我們不能等到所有的子進程退出呢?當然不是了,通過man查詢wait函數手冊,我們看到這樣一段話:

RETURN VALUE

wait(): on success, returns the process ID of the terminated child; on error, -1 is returned.

…

ERRORS

ECHILD (for wait()) The calling process does not have any unwaited-for children.

 

Wait函數在失敗的時候將返回-1,這個都是廢話。在失敗的時候,如果錯誤碼為ECHILD,對於wait函數而言,這說明當前進程沒有需要等待的子進程,這說明什嗎?這說明我們可以不用管來了多少個訊號,那都是浮雲,訊號來了,只能說明一件事:那就是有子進程退出了。至於是哪個子進程要退出了,有多少個,這個等了就知道。於是乎,就有了下面這樣一段代碼。

/*<br /> * main.cpp<br /> *<br /> * Created on: Jun 17, 2011<br /> * Author: boyce<br /> */<br />#include <stdio.h><br />#include <stdlib.h><br />#include <unistd.h><br />#include <signal.h><br />#include <wait.h><br />#include <errno.h><br />int num_clients = 0;<br />int dead_clients = 0;<br />void sig_chld_handler(int sig) {<br />pid_t pid;<br />if (sig == SIGCHLD) {<br />while (1) {<br />pid = wait(NULL);<br />if (pid < 0 && errno == ECHILD) {<br />break;<br />}<br />printf("A child dead, current child number: %d, id: %d/n", ++dead_clients, pid);<br />}<br />}<br />}<br />int main(int argc, char **argv) {<br />pid_t pid;<br />signal(SIGCHLD, sig_chld_handler);<br />for (int i = 0; i < 30; i++) {<br />if ((pid = fork()) == 0) {<br />exit(0);<br />} else if (pid > 0) {<br />printf("A child created, current child number: %d, id: %d/n", ++num_clients, pid);<br />}<br />}<br />sleep(10);<br />return 0;<br />}<br /> 

 

好,來看看測試結果:

 

哈哈,看見了吧,兒子全死了,老爸開心了(o(╯□╰)o)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.