Linux signal (6): signal shielding

Source: Internet
Author: User

1. concept:

The blocked signal set is the blocked signal set in the process. These signals cannot be sent to the process, and they are "blocked" in the process. as we will mention later, they are actually blocked.

2. signal shielding function:

# Include <signal. h>

Int sigprocmask (INT how, const sigset_t * restrict set, sigset_t * restrict oset );

0 is returned for success, and-1 is returned for error.

The sigprocmask function has three parameters:

  • How: Modify the signal shielding method.
  • Set: set this signal set to the new current signal shielding character. If it is null, it will not change.
  • Oset: saves the old signal shielding word of the process. If it is null, It is not saved.

The how parameter can have three values:

How parameter in sigprocmask
How Description
Sig_block After modification, the new signal shielding word of the process is the union of the current shielding word and the signal set pointed to by the set.
Sig_unblock After modification, the new signal shielding word of the process is the intersection of the current shielding word and the signal set pointed to by the set.
Sig_setmask After modification, the new signal shielding word of the process will be replaced by the value of the signal set to which the set points

In addition, sigprocmask is defined only for a single thread, and pthread_sigmask must be used in multiple threads.

3. unprocessed signals:

After calling the function related to signal shielding, the blocked signal is blocked for the calling process and cannot be sent to the calling process. Therefore, it is pending. to obtain these blocked signal sets, you can call the sigpending function.

# Include <signal. h>

Int sigpending (sigset_t * Set );

0 is returned for success, and-1 is returned for error.

4. instance:

The following uses a simple example to describe the two functions mentioned in this article.

# Include <signal. h>
# Include <unistd. h>
# Include <stdlib. h>
# Include <stdio. h>

/* Sigquit handler */
Static void sig_quit (INT signo)
{
Printf ("sigquit is caught ");
}

Int main ()
{
Sigset_t new, old, pend;

/* Handle sigquit */
If (signal (sigquit, sig_quit) = sig_err)
{
Perror ("signal ");
Exit (1 );
}

/* Add sigquit to sigset */
If (sigemptyset (& New) <0)
Perror ("sigemptyset ");
If (sigaddset (& New, sigquit) <0)
Perror ("sigaddset ");

/* Mask sigquit */
If (sigprocmask (sig_setmask, & New, & old) <0)
{
Perror ("sigprocmask ");
Exit (1 );
}

Printf ("sigquit is blocked ");
Printf ("Now Try Ctrl /");

Sleep (5);/* sigquit will pending */

/* Get pending */
If (sigpending (& pend) <0)
Perror ("sigpending ");

If (sigismember (& pend, sigquit ))
Printf ("sigquit pending ");

/* Restore signal mask */
If (sigprocmask (sig_setmask, & old, null) <0)
{
Perror ("sigprocmask ");
Exit (1 );
}

Printf ("sigquit unblocked ");
Printf ("Now Try Ctrl /");

Sleep (5 );

Return 0;
}

At the beginning, this program blocked sigquit (CTRL +/trigger) with sigprocmask. The signal triggered within five seconds can be obtained from sigpending; then the program unblocks sigquit (restores the previously blocked word), and then triggers this signal to call the sig_quit signal processing 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.