This series of articles written by muge0913, reproduced please note the Source: http://blog.csdn.net/muge0913/article/details/7334771
1. Sometimes you do not want to stop the current execution immediately when receiving a signal to process the signal, or ignore the signal, but call the signal processing function for a period of time. This is achieved through blocking signals.
2. Differences between signal blocking and ignoring signals.
The concept of blocking is different from that of ignoring signals. The operating system will not talk about signal transmission before the signal is blocked by the process. The blocked signal will not affect the process. The signal is only temporarily blocked. When a process ignores a signal, the signal is transmitted, but the process discards the signal.
3. Signal blocking system calls all play a blocking role. They are not used in collaboration.
[CPP]
View plaincopyprint?
- # Include <signal. h>
- Int sigprocmask (UBT how, const sigset_t * Set, sigset_t * oldset );
- Int sigsuspend (const sigset_t * sigmask );
#include <signal.h>int sigprocmask(ubt how,const sigset_t*set,sigset_t *oldset);int sigsuspend(const sigset_t*sigmask);
Sigprocmask sets the signal processing method (blocking or not blocking) in the signal shield set ).
Parameters:
How: used to specify the signal modification method. Three methods may be selected.
Sig_block // Add the signals in the Set signal set to the current signal mask. That is, the signal mask and the Set signal set are used or operated.
Sig_unblock // Delete the signal contained in the signal set to which the set points from the current signal mask. That is, the signal mask and set operations.
Sig_setmask // set the set value to the new process signal mask. That is, set assigns values to the signal mask.
Set: indicates the pointer to the signal set. This refers to the new signal set. If you only want to read the current shielding value, you can set it to null.
Oldset: a pointer to the signal set, where the original signal set is stored. It can be used to detect the signal in the signal mask.
Return description:
0 is returned when execution is successful. -1 is returned for failure, and errno is set to einval.
Sigprocmask example (demonstrate adding a signal mask ):
[CPP]
View plaincopyprint?
- # Include <stdio. h>
- # Include <signal. h>
- Void checkset ();
- Void main ()
- {
- Sigset_tblockset;
- Sigemptyset (& blockset );
- Sigaddset (& blockset, SIGINT );
- Sigaddset (& blockset, sigtstp );
- Checkset ();
- Sigprocmask (sig_setmask, & blockset, null );
- Checkset ();
- Sigaddset (& blockset, sigterm );
- Sigprocmask (sig_block, & blockset, null );
- Checkset ();
- Sigdelset (& blockset, sigterm );
- Sigprocmask (sig_unblock, & blockset, null );
- Checkset ();
- }
- Void checkset ()
- {
- Sigset_tset set;
- Printf ("checksetstart: \ n ");
- If (sigprocmask (0, null, & set) <0)
- {
- Printf ("checksetsigprocmask Error !! \ N ");
- Exit (0 );
- }
- If (sigismember (& set, SIGINT ))
- Printf ("SIGINT \ n ");
- If (sigismember (& set, sigtstp ))
- Printf ("sigtstp \ n ");
- If (sigismember (& set, sigterm ))
- Printf ("sigterm \ n ");
- Printf ("checksetend \ n ");
- }
#include <stdio.h>#include <signal.h>void checkset();void main(){ sigset_tblockset; sigemptyset(&blockset); sigaddset(&blockset,SIGINT); sigaddset(&blockset,SIGTSTP); checkset(); sigprocmask(SIG_SETMASK,&blockset,NULL); checkset(); sigaddset(&blockset,SIGTERM); sigprocmask(SIG_BLOCK,&blockset,NULL); checkset(); sigdelset(&blockset,SIGTERM); sigprocmask(SIG_UNBLOCK,&blockset,NULL); checkset();}void checkset(){ sigset_tset set; printf("checksetstart:\n"); if(sigprocmask(0,NULL,&set)<0) { printf("checksetsigprocmask error!!\n"); exit(0); } if(sigismember(&set,SIGINT)) printf("sigint\n"); if(sigismember(&set,SIGTSTP)) printf("sigtstp\n"); if(sigismember(&set,SIGTERM)) printf("sigterm\n"); printf("checksetend\n");}
Sigprocmask example (to demonstrate that some code is not disturbed by signals ):
[CPP]
View plaincopyprint?
- # Include <stdio. h>
- # Include <signal. h>
- Void checkset ();
- Void func ();
- Void main ()
- {
- Sigset_tblockset, oldblockset, pendmask;
- Printf ("PID: % LD \ n", (long) getpid ());
- Signal (SIGINT, func); // semaphore capture function, capture SIGINT, jump to function pointer func to execute
- Sigemptyset (& blockset); // initializes the semaphore set
- Sigaddset (& blockset, sigtstp); // Add sigtstp to the semaphore set
- Sigaddset (& blockset, SIGINT); // Add SIGINT to the semaphore set
- Sigprocmask (sig_setmask, & blockset, & oldblockset); // block SIGINT and sigtstp in blockset, and save the current signal shielding characters
- /* The following program will not be disturbed by signals */
- Checkset ();
- Sleep (5 );
- Sigpending (& pendmask); // check the signal is pending
- If (sigismember (& pendmask, SIGINT) // SIGINT is pending. Pending means that sigquit is blocked and not processed
- Printf ("sigintpending \ n ");
- /* End without disturbing */
- Sigprocmask (sig_setmask, & oldblockset, null); // restores blocked SIGINT sigtstp
- Printf ("sigintunblocked \ n ");
- Sleep (6 );
- }
- Void checkset ()
- {
- Sigset_tset;
- Printf ("checksetstart: \ n ");
- If (sigprocmask (0, null, & set) <0)
- {
- Printf ("checksetsigprocmask Error !! \ N ");
- Exit (0 );
- }
- If (sigismember (& set, SIGINT ))
- Printf ("SIGINT \ n ");
- If (sigismember (& set, sigtstp ))
- Printf ("sigtstp \ n ");
- If (sigismember (& set, sigterm ))
- Printf ("sigterm \ n ");
- Printf ("checksetend \ n ");
- }
- Void func ()
- {
- Printf ("hellofunc \ n ");
- }