10.7 SIGCLD semantics
SIGCLD是在system v的系列版本中使用的一個信號。SIGCHLD是BSD系列版本中使用的信號,他們的意義有所不同,其中SIGCLD應該被禁用。其原因如下:
(一) system 的SIGCLD
SVR4是從system v發展出來的,它使用SIGCLD的方法(如下都以signal和sigset函數作為signal安裝函數) :
1.被推薦的使用SIGCLD的方法
設置SIGCLD為SIG_IGN,這樣生成的孩子process在退出后不會生成zombia。
2.不被推薦的使用方法
就是默認的對SIGCLD的處理方法,即SIG_DFL,子進程結束后不處理,如果父親wait的話,會等所有的孩子進程結束后才返回-1,errno=ECHLD。這不是推薦的。
可見在System v系列里,對SIGCLD的使用推薦使用SIG_IGN。
3.system v的SIGCLD的一個被詬病的地方
就是當用signal安裝SIGCLD的handler的時候,他會立馬檢查當前是否有孩子進程可以被waited,如果有,就立馬調用handler。這會造成stack overflow,例如:我們用signal安裝handler的時候,經常在signal handler里面首先將handler reinstall, 好,當有一個子進程結束了,發送了SIGCLD信號,該handler被調用,reinstall 該handler, 立馬檢查是否有可以waited的進程,有,立馬調用handler, ….如此循環,不久死掉了么.
如下就是例子代碼:
Figure 10.6. System V SIGCLD handler that doesn't work
#include "apue.h"
#include <sys/wait.h>
static void sig_cld(int);
int
main()
{
pid_t pid;
if (signal(SIGCLD, sig_cld) == SIG_ERR)
perror("signal error");
if ((pid = fork()) < 0) {
perror("fork error");
} else if (pid == 0) { /* child */
sleep(2);
_exit(0);
}
pause(); /* parent */
exit(0);
}
static void
sig_cld(int signo) /* interrupts pause() */
{
pid_t pid;
int status;
printf("SIGCLD received\n");
if (signal(SIGCLD, sig_cld) == SIG_ERR) /* reestablish handler */
perror("signal error");
if ((pid = wait(&status)) < 0) /* fetch child status */
perror("wait error");
printf("pid = %d\n", pid);
}
可見system v系列不能給SIGCLD安裝handler,那么就只能采用推薦的方法SIG_IGN了。使其子進程壓根不產生zombia.
(二)SIGCHLD
在linux里,用signal安裝SIGCHLD,且將handler置為SIG_IGN,也會達到是子進程退出后不產生zombia的好處。而默認的我們不處理該信號,就會產生zombia。BSD系列就是這樣。
如下是摘自linux suse man page的一段話:
POSIX.1-1990 disallowed setting the action for SIGCHLD to SIG_IGN. POSIX.1-2001 allows this possibility, so that ignoring SIGCHLD can be used to prevent the creation of zombies (see wait(2)). Nevertheless, the historical BSD and System V behaviors for ignoring SIGCHLD differ, so that the only completely portable method of ensuring that terminated children do not become zombies is to catch the SIGCHLD signal and perform a wait(2) or similar.
鑒于不同平臺的對SIGCHLD的設置成SIG_IGN的意義的不同,唯一的更portable的避免zombia的方法是在SIGCHLD的handler里面調用wait。
(三)使不產生zombia孩子的另一個方法
使用sigaction并設置SA_NOCLDWAIT標記。
(四)在包括linux在內的一些平臺,SIGCLD被宏定為SIGCHLD,放心使用