今天看了unix網(wǎng)絡(luò)編程中的關(guān)于wait和waitpid的區(qū)別,它采用的驗(yàn)證例子是客戶/服務(wù)器的連接問題
1.當(dāng)子進(jìn)程結(jié)束的時(shí)候,父進(jìn)程會(huì)收到SIGCHLD通知
2.進(jìn)程可以調(diào)用wait/waitpid等待此Signal
a.當(dāng)所有子進(jìn)程都在執(zhí)行的時(shí)候,會(huì)block
b.當(dāng)某個(gè)子進(jìn)程中止,成為zombie的時(shí)候,立刻返回
c.如果沒有任何子進(jìn)程,則返回錯(cuò)誤
以前曾經(jīng)學(xué)過這兩函數(shù)的使用,但是沒有什么例子可以驗(yàn)證,今天巧遇網(wǎng)絡(luò)編程,就把這兩個(gè)函數(shù)重新溫習(xí)一下:
pid_t wait(int *stat_loc);
pid_t waitpid(pid_t pid, int *stat_loc, int options);
上面就是兩個(gè)函數(shù)的聲明
stat_loc返回的是子進(jìn)程的終止?fàn)顟B(tài),是個(gè)整型值,可以通過一些宏定義來檢測(cè)終止的狀態(tài),暫時(shí)不介紹了
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在網(wǎng)絡(luò)編程中使用的區(qū)別:
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;
}

上面是兩段不同的信號(hào)處理函數(shù),它們的結(jié)果可能相差很多的,下面我們就來看看有哪些區(qū)別:
首先我們看看用wait函數(shù)產(chǎn)生的效果:
1、下面的輸出是在啟動(dòng)客戶端和服務(wù)器端程序后的ps輸出
備注:在客戶端程序中,我們連續(xù)產(chǎn)生5個(gè)服務(wù)器連接進(jìn)程,所以一共6個(gè)waitsrv進(jìn)程 
2、下面是在客戶端輸入ctrl+d后的服務(wù)器端輸出和ps查詢后的結(jié)果


我們從上面可以看出利用wait的一個(gè)致命的缺點(diǎn)就是只能回收一個(gè)子進(jìn)程,其他的進(jìn)程由于沒有得到回收而變成僵尸進(jìn)程
下面我們來看看利用waitpid函數(shù)的輸出結(jié)果:
1、啟動(dòng)服務(wù)器端和客戶端后的ps輸出

2、在客戶端輸入ctrl+d后的服務(wù)器端輸出和ps的結(jié)果


我們可以發(fā)現(xiàn)所有服務(wù)器的子進(jìn)程在接受到客戶端輸入的EOF后,都被回收了!
由此,我們可以發(fā)現(xiàn),為了減少僵尸進(jìn)程的產(chǎn)生,我們可以采用waitpid而減少wait的使用,雖然僵尸進(jìn)程可以由init回收
以上的例子程序是采用的unix網(wǎng)絡(luò)編程中的例子,在此代碼就不再詳細(xì)列出了!