sigset_t结构 sigpromask()函数

 /* A `sigset_t' has a bit for each signal.  */
 27
 28 # define _SIGSET_NWORDS (1024 / (8 * sizeof (unsigned long int)))
 29 typedef struct
 30   {
 31     unsigned long int __val[_SIGSET_NWORDS];
 32   } __sigset_t;
 33
 34 #endif
*****************************************************sigset_t***************************************************************************************

SIGPROCMASK(2)                                         Linux Programmer's Manual                                        SIGPROCMASK(2)

NAME
       sigprocmask - examine and change blocked signals

SYNOPSIS
       #include <signal.h>

       int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);

DESCRIPTION
       sigprocmask()  is  used  to  fetch  and/or change the signal mask of the calling thread.  The signal mask is the set of signals
       whose delivery is currently blocked for the caller (see also signal(7) for more details).

       The behavior of the call is dependent on the value of how, as follows.

       SIG_BLOCK
              The set of blocked signals is the union of the current set and the set argument.

       SIG_UNBLOCK
              The signals in set are removed from the current set of blocked signals.  It is permissible to attempt to unblock a  sig‐
              nal which is not blocked.

       SIG_SETMASK
              The set of blocked signals is set to the argument set.

       If oldset is non-NULL, the previous value of the signal mask is stored in oldset.

       If set is NULL, then the signal mask is unchanged (i.e., how is ignored), but the current value of the signal mask is neverthe‐
       less returned in oldset (if it is not NULL).

       The use of sigprocmask() is unspecified in a multithreaded process; see pthread_sigmask(3).

RETURN VALUE
       sigprocmask() returns 0 on success and -1 on error.

*********************************************************sigprocmask()**************************************************************************

举例代码:::

  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <unistd.h>   
  4. #include <stdbool.h>   
  5. #include <signal.h>   
  6. #include <sys/types.h>   
  7. #include <errno.h>   
  8. #include <string.h>   
  9.   
  10. int  flag =  true ;  
  11.   
  12. void  int_handler(  int  signum )  
  13. {  
  14.    printf("int handler %d/n" ,signum);  
  15. }  
  16.   
  17. void  quit_handler(  int  signum )  
  18. {  
  19.    flag = false ;  
  20.    printf("in quit handler/n" );  
  21. }  
  22.   
  23. int  main( int  argc,  char  **argv)  
  24. {  
  25.    char  choice;  
  26.    sigset_t mask;  
  27.   
  28.   
  29.    if  ( signal(SIGINT, int_handler) == SIG_ERR ){  
  30.       printf("sigaction failed!/n" );  
  31.       return  -1;  
  32.    }  
  33.   
  34.    if  ( signal(SIGQUIT, quit_handler) == SIG_ERR ){  
  35.       printf("sigaction failed!/n" );  
  36.       return  -1;  
  37.    }  
  38.   
  39.   
  40.    sigemptyset(&mask);  
  41.    if  ( -1 == sigaddset(&mask,SIGINT)){  
  42.       printf("add SIGINT to mask failed!/n" );  
  43.       return  -1;  
  44.    }  
  45.   
  46.    if  ( -1 == sigprocmask(SIG_BLOCK,&mask,NULL) ){  
  47.       printf("sigprocmask failed!/n" );  
  48.       return  -1;  
  49.    }  
  50.   
  51.    printf("SIGINT was masked, SIGQUIT was registered!/n" );  
  52.    while (flag){  
  53.       pause();  
  54.    }  
  55.   
  56.    if  ( -1 == sigprocmask(SIG_UNBLOCK,&mask,NULL) ){  
  57.       printf("sigprocmask failed!/n" );  
  58.       return  -1;  
  59.    }  
  60.   
  61.    printf("SIGINT was unmasked, pauseing ........./n" );  
  62.    pause();  
  63.   
  64.    return  0;  
  65. }  
  66. 这段代码运行:
  67. ***********************************************************
  68. SIGINT was mask , SIGQUIT was registered!
    ^/in quit handler
    SIGINT was unmasked , pauseing....
    ^/in quit handler
    ***********************************************************
  69. SIGINT was mask , SIGQUIT was registered!
    ^C^C^C^C^C
    ^/in quit handler
    int handler 2
    SIGINT was unmasked , pauseing....
    ^/in quit handler
    ****************************************************************
  70. SIGINT was mask , SIGQUIT was registered!
    ^C^C^C^/in quit handler
    int handler 2
    SIGINT was unmasked , pauseing....
    ^Cint handler 2
    ***********************************************************************
  71. 值得观察第一个结果和其他两个比较:
  72. 看出对信号SIGINT是阻塞,当UNBLOCK后SIGINT还会传回来作用

你可能感兴趣的:(sigset_t结构 sigpromask()函数)