10.11 Signal Sets

Source: Internet
Author: User


We need to use a data type to store multiple signals, which are called signal sets, and we will use them in functions like function Sigprocmask (in the next section) to tell the kernel not to allow the signals in the collection to appear, as we mentioned earlier, The number of different signals may exceed the number of bits in an shaping variable, so it is generally not possible to use each bit of the shaping variable to store each signal. POSIX.1 defines a data structure sigset_t with a stored signal set, and allows the following five functions to manipulate it:

  
 
  1. #include <signal.h>
  2. int sigemptyset(sigset_t *set);
  3. int sigfillset(sigset_t *set);
  4. int sigaddset(sigset_t *set, int signo);
  5. int sigdelset(sigset_t *set, int signo);
  6. All four return:0 if OK, -1 on error.
  7. int sigismember(const sigset_t *set, int signo);
  8. Returns:1 if true, 0 if false, -1 on error.

The function sigemptyset is used to initialize set signal sets so that all signals in it are excluded. The function sigfillset is used to initialize set signal sets so that all signals are included. All applications should call Sigemptyset or Sigfillset once before using the signal set, because we cannot make any assumptions about the external or static variables that the C program initializes.
Once we have initialized a set of signals, we can add or remove signals specified in the signal set, the function Sigaddset is used to add a signal to a signal set, and the function sigdelset is used to remove a signal from a set, In all functions that use a signal set as a parameter, we always pass a pointer to the signal set as a parameter.

Implementation


If the implementation has fewer signal types than the number of bits in the shaping variable, then the signal set may be implemented using each bit of the shaping, which is conveniently considered in this section, assuming that an implementation has 31 different signals, At the same time, the shaping variable has 32 bit. function Sigemptyset 0 reshape the variable, and the function sigfillset all bits, the two functions can be implemented in the header file as follows:

  
 
  1. #define sigemptyset(ptr) (*(ptr) = 0)
  2. #define sigfillset(ptr) (*(ptr) = ~(sigset_t)0, 0)


Note that the Sigfillset function must return 0, so we use the good operator in C to implement an expression with a return value of 0.
With this implementation, the function Sigaddset will place a bit, and the function Sigdelset will close a bit, and the function Sigismember will test a specified bit. Since there is no signal value of 0, a 32bit variable can store only 31 signals.



From for notes (Wiz)

10.11 Signal Sets

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.