10_6檢測子進程狀態變化的處理函數。所以確實有這種子進程 錯誤

因爲當已經進入信號處理函數裏,說明在子進程裏已經執行了_exit(0),在信號處理函數裏註冊信號SIGCLD的處理函數時,雖然會檢測是否有需要的子進程,可是結果沒有,因爲已經執行_exit(0);所以不會死循環。書中錯。

一、源代碼:

cat -n 10_6.c
     1  #include "apue.h"
     2  #include <sys/wait.h>
     3
     4  static void sig_cld(int);
     5
     6  int main()
     7  {
     8          pid_t pid;
     9          if ((pid = fork()) <0 ){
    10                  err_sys("fork error");
    11          }
    12          if (signal(SIGCHLD,sig_cld) == SIG_ERR){
    13                  err_sys("signal SIGCLD handler establish error");
    14          }
    15
    16          //if ((pid = fork()) <0 ){
    17          //      err_sys("fork error");
    18          //}
    19          if (pid == 0){
    20                  sleep(2);
    21                  _exit(0);
    22          }
    23
    24          pause();
    25          exit(0);
    26  }
    27
    28  static void sig_cld (int signalno)
    29  {
    30          pid_t pid;
    31          int status;
    32          printf("signal SIGCHLD received.\n");
    33          if (signal(SIGCHLD,sig_cld) == SIG_ERR){
    34                  err_sys("signla SIGCLD handler establish error.");
    35          }
    36
    37          if ((pid = wait(&status)) < 0){
    38                  err_sys("wait child error.");
    39          }
    40
    41          printf("pid = %d\n",pid);
    42
    43  }




二、編譯及執行結果

:gcc -Wall -gdb3 -o handler_sigcld 10_6.c
cc1: warning: `-gdb3' not supported by this configuration of GCC
In file included from apue.h:132,
                 from 10_6.c:1:
error.c: In function `err_doit':
error.c:121: warning: implicit declaration of function `vsnprintf'
error.c:123: warning: implicit declaration of function `snprintf'
10_6.c: In function `sig_cld':
10_6.c:41: warning: int format, pid_t arg (arg 2)


./handler_sigcld
signal SIGCHLD received.
pid = 24916



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章