Sigprocmask blocks the process.

Source: Internet
Author: User

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?
  1. # Include <signal. h>
  2. Int sigprocmask (UBT how, const sigset_t * Set, sigset_t * oldset );
  3. 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?
  1. # Include <stdio. h>
  2. # Include <signal. h>
  3. Void checkset ();
  4. Void main ()
  5. {
  6. Sigset_tblockset;
  7. Sigemptyset (& blockset );
  8. Sigaddset (& blockset, SIGINT );
  9. Sigaddset (& blockset, sigtstp );
  10. Checkset ();
  11. Sigprocmask (sig_setmask, & blockset, null );
  12. Checkset ();
  13. Sigaddset (& blockset, sigterm );
  14. Sigprocmask (sig_block, & blockset, null );
  15. Checkset ();
  16. Sigdelset (& blockset, sigterm );
  17. Sigprocmask (sig_unblock, & blockset, null );
  18. Checkset ();
  19. }
  20. Void checkset ()
  21. {
  22. Sigset_tset set;
  23. Printf ("checksetstart: \ n ");
  24. If (sigprocmask (0, null, & set) <0)
  25. {
  26. Printf ("checksetsigprocmask Error !! \ N ");
  27. Exit (0 );
  28. }
  29. If (sigismember (& set, SIGINT ))
  30. Printf ("SIGINT \ n ");
  31. If (sigismember (& set, sigtstp ))
  32. Printf ("sigtstp \ n ");
  33. If (sigismember (& set, sigterm ))
  34. Printf ("sigterm \ n ");
  35. Printf ("checksetend \ n ");
  36. }
#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?
  1. # Include <stdio. h>
  2. # Include <signal. h>
  3. Void checkset ();
  4. Void func ();
  5. Void main ()
  6. {
  7. Sigset_tblockset, oldblockset, pendmask;
  8. Printf ("PID: % LD \ n", (long) getpid ());
  9. Signal (SIGINT, func); // semaphore capture function, capture SIGINT, jump to function pointer func to execute
  10. Sigemptyset (& blockset); // initializes the semaphore set
  11. Sigaddset (& blockset, sigtstp); // Add sigtstp to the semaphore set
  12. Sigaddset (& blockset, SIGINT); // Add SIGINT to the semaphore set
  13. Sigprocmask (sig_setmask, & blockset, & oldblockset); // block SIGINT and sigtstp in blockset, and save the current signal shielding characters
  14. /* The following program will not be disturbed by signals */
  15. Checkset ();
  16. Sleep (5 );
  17. Sigpending (& pendmask); // check the signal is pending
  18. If (sigismember (& pendmask, SIGINT) // SIGINT is pending. Pending means that sigquit is blocked and not processed
  19. Printf ("sigintpending \ n ");
  20. /* End without disturbing */
  21. Sigprocmask (sig_setmask, & oldblockset, null); // restores blocked SIGINT sigtstp
  22. Printf ("sigintunblocked \ n ");
  23. Sleep (6 );
  24. }
  25. Void checkset ()
  26. {
  27. Sigset_tset;
  28. Printf ("checksetstart: \ n ");
  29. If (sigprocmask (0, null, & set) <0)
  30. {
  31. Printf ("checksetsigprocmask Error !! \ N ");
  32. Exit (0 );
  33. }
  34. If (sigismember (& set, SIGINT ))
  35. Printf ("SIGINT \ n ");
  36. If (sigismember (& set, sigtstp ))
  37. Printf ("sigtstp \ n ");
  38. If (sigismember (& set, sigterm ))
  39. Printf ("sigterm \ n ");
  40. Printf ("checksetend \ n ");
  41. }
  42. Void func ()
  43. {
  44. Printf ("hellofunc \ n ");
  45. }

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.