Linux under C Programming: Sigprocmask blocking process

Source: Internet
Author: User
Tags exit new set printf semaphore sleep linux

1, sometimes do not want to receive the signal immediately stop the current execution, to deal with the signal, but also do not want to ignore the signal, but delay a period of time to call the signal processing function. This situation is achieved by blocking the signal.

2, signal blocking and ignoring the difference between signals.

The concept of blocking is different from ignoring the signal. The operating system does not speak the signal before it is unblocked by the process, and the blocked signal does not affect the behavior of the process, and the signal is temporarily blocked from passing. When a process ignores a signal, the signal is passed out but the process discards the signal.

3, signal blocking system calls, they all play a blocking role, they are not collaborative use.

#include <signal.h>     
         
int sigprocmask (ubt how,const sigset_t*set,sigset_t *oldset);     
         
int sigsuspend (const sigset_t*sigmask);

The sigprocmask sets the signal handling mode (blocking or not blocking) on the signal shielding set.

Parameters:

How: To specify the way the signal is modified, there may be three kinds of choices

Sig_block//adds the signal contained in the signal set that the set points to the current signal mask. The signal mask and set signal set are performed or manipulated.

sig_unblock//removes the signal contained in the signal set that the set points to from the current signal mask. That is, the signal mask and set are performed and manipulated.

Sig_setmask//Sets the value of set to a new process signal mask. That is, the set carries out assignment operations on the signal mask.

Set: A pointer to a set of signals that refers specifically to a new set of signals, which you can set to NULL if you only want to read the current masked value.

Oldset: It is also a pointer to the signal set, where the original signal set is stored. can be used to detect what signals are present in the signal mask.

Return Description:

When successfully executed, returns 0. The failure return -1,errno is set to Einval.

Sigprocmask Sample (Demo add signal mask):

 #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 (demonstrating that some code is not disturbed by a signal):

 #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 to SIGINT, jump to function pointer func place executes Sigemptyset (&blockset); Initialization of the signal volume set Sigaddset (&BLOCKSET,SIGTSTP); Add the SIGTSTP to the semaphore set Sigaddset (&blockset,sigint);//Add SIGINT to the semaphore set Sigprocmask (sig_setmask,&blocks Et,&oldblockset);     
    Block out the SIGINT,SIGTSTP in the Blockset, and save the current signal screen Word/* Execute the following procedure, will not be interrupted by the signal * * * checkset ();     
     Sleep (5); Sigpending (&pendmask); The check signal is pending if (Sigismember (&pendmask,sigint))//sigint is pending.     

     The so-called unresolved, refers to the Sigquit is blocked has not been processed printf ("sigintpending\n"); /* Free of Interruption end/Sigprocmask (sig_setmask,&oldblockset,null);     
     Restore the shielded signal 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"); }

View a full set of articles: Http://www.bianceng.cn/Programming/C/201212/34807.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.