信號實現父子進程之間的同步--sigsuspend的作用

函數原型:

  #include <signal.h>
  int sigsuspend(const sigset_t *mask);

作用:

  用於在接收到某個信號之前,臨時用mask替換進程的信號掩碼,並暫停進程執行,直到收到信號爲止。
  The sigsuspend() function replaces the current signal mask of the calling thread with the set of signals pointed to by sigmask and then suspends the thread until delivery of a signal whose action is either to execute a signal-catching function or to terminate the process. This will not cause any other signals that may have been pending on the process to become pending on the thread.
  If the action is to terminate the process then sigsuspend() will never return. If the action is to execute a signal-catching function, thensigsuspend() will return after the signal-catching function returns, with the signal mask restored to the set that existed prior to thesigsuspend() call.
  It is not possible to block signals that cannot be ignored. This is enforced by the system without causing an error to be indicated.
  也就是說,sigsuspend後,進程就掛在那裏,等待着開放的信號的喚醒。系統在接收到信號後,馬上就把現在的信號集還原爲原來的,然後調用處理函數。

返回值:
  sigsuspend返回後將恢復調用之前的的信號掩碼。信號處理函數完成後,進程將繼續執行。該系統調用始終返回-1,並將errno設置爲EINTR.
  Since sigsuspend() suspends process execution indefinitely, there is no successful completion return value. If a return occurs, -1 is returned and errno is set to indicate the error.
  The sigsuspend() function will fail if:
  [EINTR]
  A signal is caught by the calling process and control is returned from the signal-catching function.

也就是說,sigsuspend後,進程就掛在那裏,等待着開放的信號的喚醒。系統在接受到信號後,馬上就把現在的信號集還原爲原來的,然後調用處理函數。


Stevens在《Unix環境高級編程》一書中是如是回答的“If a signal is caught and if the signal handler returns, then sigsuspend returns and the signal mask of the process is set to its value before the call to sigsuspend.”,由於sigsuspend是原子操作,所以這句給人的感覺就是先調用signal handler先返回,然後sigsuspend再返回。

  1. int main(void) {    
  2.    sigset_t   newmask, oldmask, zeromask;    
  3.     
  4.    if (signal(SIGINT, sig_int) == SIG_ERR)    
  5.       err_sys("signal(SIGINT) error");    
  6.     
  7.    sigemptyset(&zeromask);    
  8.     
  9.    sigemptyset(&newmask);    
  10.    sigaddset(&newmask, SIGINT);    
  11.    /* block SIGINT and save current signal mask */    
  12.    if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)    
  13.       err_sys("SIG_BLOCK error");    
  14.     
  15.    /* critical region of code */    
  16.    pr_mask("in critical region: ");    
  17.     
  18.    /* allow all signals and pause */    
  19.    if (sigsuspend(&zeromask) != -1)    
  20.       err_sys("sigsuspend error");    
  21.    pr_mask("after return from sigsuspend: ");    
  22.     
  23.    /* reset signal mask which unblocks SIGINT */    
  24.    if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)    
  25.       err_sys("SIG_SETMASK error");    
  26.     
  27.    /* and continue processing ... */    
  28.    exit(0);    
  29. }    
  30.     
  31. static void sig_int(int signo) {    
  32.    pr_mask("\nin sig_int: ");    
  33.    return;    
  34. }    
結果:

  1. $a.out    
  2. in critical region: SIGINT    
  3. ^C    
  4. in sig_int: SIGINT    
  5. after return from sigsuspend: SIGINT    

如果按照sig_handler先返回,那麼SIGINT是不該被打印出來的,因爲那時屏蔽字還沒有恢復,所有信號都是不阻塞的。那麼是Stevens說錯了麼?當然沒有,只是Stevens沒有說請在sigsuspend的原子操作中到底做了什麼?
sigsuspend的整個原子操作過程爲:
(1) 設置新的mask阻塞當前進程;
(2) 收到信號,恢復原先mask;
(3) 調用該進程設置的信號處理函數;
(4) 待信號處理函數返回後,sigsuspend返回。


  1. #include <stdio.h>    
  2. #include <signal.h>    
  3.     
  4. void checkset();    
  5. void func();    
  6. void main()    
  7. {    
  8.      sigset_tblockset,oldblockset,zeroset,pendmask;    
  9.      printf("pid:%ld\n",(long)getpid());    
  10.      signal(SIGINT,func);    
  11.     
  12.      sigemptyset(&blockset);    
  13.      sigemptyset(&zeroset);    
  14.      sigaddset(&blockset,SIGINT);    
  15.     
  16.      sigprocmask(SIG_SETMASK,&blockset,&oldblockset);    
  17.      checkset();    
  18.      sigpending(&pendmask);    
  19.     
  20.      if(sigismember(&pendmask,SIGINT))    
  21.          printf("SIGINTpending\n");    
  22.     
  23.      if(sigsuspend(&zeroset)!= -1)    
  24.      {    
  25.      printf("sigsuspenderror\n");    
  26.      exit(0);    
  27.      }    
  28.     
  29.      printf("afterreturn\n");    
  30.      sigprocmask(SIG_SETMASK,&oldblockset,NULL);    
  31.     
  32.      printf("SIGINTunblocked\n");    
  33. }    
  34.     
  35. void checkset()    
  36. {    sigset_tset;    
  37.      printf("checksetstart:\n");    
  38.      if(sigprocmask(0,NULL,&set)<0)    
  39.      {    
  40.      printf("checksetsigprocmask error!!\n");    
  41.      exit(0);    
  42.      }    
  43.     
  44.      if(sigismember(&set,SIGINT))    
  45.      printf("sigint\n");    
  46.     
  47.      if(sigismember(&set,SIGTSTP))    
  48.      printf("sigtstp\n");    
  49.     
  50.      if(sigismember(&set,SIGTERM))    
  51.      printf("sigterm\n");    
  52.      printf("checksetend\n");    
  53. }    
  54.     
  55. void func()    
  56. {    
  57.      printf("hellofunc\n");    
  58. }    

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