10.12 Sigprocmask function

Source: Internet
Author: User


In section 10.8 We mention that the mask of the process is a set of signals that the current blocking signal is sent to the process. A process can view its signal mask, change its signal mask, or perform both operations at the same time, by invoking the function Sigprocmask to achieve the above requirements.

  
 
  1. #include <signal.h>
  2. int sigprocmask(int how, const sigset_t *restrict set, sigset_t *restrict oset);
  3. return:0 if OK, -1 on error.

First, if Oset is a non-null pointer, the current process's signal mask will be returned through Oset.
Second, if set is a non-null pointer, the parameter how shows how the current signal mask is modified, and figure 10.13 describes the possible values for the parameter how. Sig_block is one or operation, while Sig_setmask is an assignment operation, note that Sigkill and Sigstop cannot be blocked.

How
Description
Sig_block The new signal mask is a combination of the current signal mask and the set of signals specified by the sets. That is, the set contains the extra signal we want to block.
Sig_unblock The new signal mask is the intersection of the current signal mask and the complement set of the set signal set, that is, the set contains the signals we want to contact with blocking.
Sig_setmask The new signal will be replaced by a set of incoming signal sets.

Figure 10.13 How to change the current signal mask using the function Sigprocmask

If set is a null pointer, the process's signal mask is not changed and the parameter how is ignored.
After calling the function Sigprocmask, if any of the signals that are not blocked are suspended, at least one of these signals is sent to the process before the Sigprocmask is returned.

The function sigprocmask is defined only for single-threaded processes, and for multithreaded processes the system provides another function to manipulate the signal mask of the thread, which we will discuss in 12.8.

Example


The function in Figure 10.14 is used to print the name in the signal mask of the calling process, and we will call the function in the program in Figure 10.20 and 10.22.

  
 
  1. #include "apue.h"
  2. #include <errno.h>
  3. void pr_mask(const char *str)
  4. {
  5. sigset_t sigset;
  6. int errno_save;
  7. errno_save = errno; /*we can be called by signal handlers*/
  8. if(sigprocmask(0, NULL, &sigset)<0)
  9. {
  10. err_ret("sigprocmask error");
  11. }
  12. else
  13. {
  14. printf("%s", str);
  15. if(sigismember(&sigset, SIGINT))
  16. printf(" SIGINT");
  17. if(sigismember(&sigset, SIGQUIT))
  18. printf(" SIGQUIT");
  19. if(sigismember(&sigset, SIGUSR1))
  20. printf(" SIHUSR1");
  21. if(sigismember(&sigset, SIGALRM))
  22. printf(" SIGALRM");
  23. /*remaining signals can go here */
  24. printf("\n");
  25. }
  26. errno = errno_save; /*restore errno*/
  27. }



From for notes (Wiz)

10.12 Sigprocmask function

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.