今天看了unix網絡編程中的關于wait和waitpid的區別,它采用的驗證例子是客戶/服務器的連接問題
1.當子進程結束的時候,父進程會收到SIGCHLD通知
2.進程可以調用wait/waitpid等待此Signal
a.當所有子進程都在執行的時候,會block
b.當某個子進程中止,成為zombie的時候,立刻返回
c.如果沒有任何子進程,則返回錯誤
以前曾經學過這兩函數的使用,但是沒有什么例子可以驗證,今天巧遇網絡編程,就把這兩個函數重新溫習一下:
pid_t wait(int *stat_loc);
pid_t waitpid(pid_t pid, int *stat_loc, int options);
上面就是兩個函數的聲明
stat_loc返回的是子進程的終止狀態,是個整型值,可以通過一些宏定義來檢測終止的狀態,暫時不介紹了
The pid argument specifies a set of child processes for which status is requested.
The waitpid() function shall only return the status of a child process from this set:
If pid is equal to (pid_t)-1, status is requested for any child process. In this respect, waitpid() is then equivalent to wait().
If pid is greater than 0, it specifies the process ID of a single child process for which status is requested.
If pid is 0, status is requested for any child process whose process group ID is equal to that of the calling process.
If pid is less than (pid_t)-1, status is requested for any child process whose process group ID is equal to the absolute value of pid.
The options argument is constructed from the bitwise-inclusive OR of zero or more of the following flags, defined in the <sys/wait.h> header:
[WCONTINUED]
The waitpid() function shall report the status of any continued child process specified by pid whose status has not been reported
since it continued from a job control stop.
[WNOHANG]
The waitpid() function shall not suspend execution of the calling thread if status is not immediately available for one of the child processes specified by pid
[WUNTRACED]
The status of any child processes specified by pid that are stopped, and whose status has not yet been reported since they stopped,
shall also be reported to the requesting process
下面我們來具體看看wait和waitpid在網絡編程中使用的區別:
void sig_chld(int signo)


{
pid_t pid;
int stat;
pid = wait(&stat);
printf("child %d terminated \n",pid);
return;
}

void sig_chld(int signo)


{
pid_t pid;
int stat;
while((pid = waitpid(-1,&stat,WNOHANG))>0)
printf("child %d terminated \n",pid);
return;
}

上面是兩段不同的信號處理函數,它們的結果可能相差很多的,下面我們就來看看有哪些區別:
首先我們看看用wait函數產生的效果:
1、下面的輸出是在啟動客戶端和服務器端程序后的ps輸出
備注:在客戶端程序中,我們連續產生5個服務器連接進程,所以一共6個waitsrv進程 
2、下面是在客戶端輸入ctrl+d后的服務器端輸出和ps查詢后的結果


我們從上面可以看出利用wait的一個致命的缺點就是只能回收一個子進程,其他的進程由于沒有得到回收而變成僵尸進程
下面我們來看看利用waitpid函數的輸出結果:
1、啟動服務器端和客戶端后的ps輸出

2、在客戶端輸入ctrl+d后的服務器端輸出和ps的結果


我們可以發現所有服務器的子進程在接受到客戶端輸入的EOF后,都被回收了!
由此,我們可以發現,為了減少僵尸進程的產生,我們可以采用waitpid而減少wait的使用,雖然僵尸進程可以由init回收
以上的例子程序是采用的unix網絡編程中的例子,在此代碼就不再詳細列出了!
posted on 2007-09-04 09:21
LG 閱讀(2086)
評論(0) 編輯 收藏 引用 所屬分類:
UnixProgram