Linux Signal Set

Source: Internet
Author: User
Tags sigint signal

Linux Signal Set
1. Concept of Signal Set
A signal set is a data type that can represent multiple signals. sigset_t set; set is a signal set.
Since it is a set, you need to add/Delete the set.
Int sigemptyset (sigset_t * set); empty the set
Int sigfillset (sigset_t * set); add all signals to the set
Int sigaddset (sigset_t * set, int signo); Add the signo signal to the set
Int sigdelset (sigset_t * set, int signo); remove the signo signal from the set
Int sigismember (const sigset_t * set, int signo); signo determines whether the signal exists in the set
Code example:
Include <stdio. h>
# Include <stdlib. h>
# Include <sys/types. h>
# Include <sys/stat. h>
# Include <signal. h>
Int main ()
{
Sigset_t sigset;
Sigfillset (& sigset);/* fill all signals */
If (sigismember (& sigset, SIGINT)/* Judge SIGINT */
Printf ("SIGINT exist in signal_set! ");
If (sigismember (& sigset, SIGTERM ))
Printf ("SIGTERM exist in signal_set! ");
If (sigismember (& sigset, SIGABRT ))
Printf ("SIGABRT exist in signal_set! ");
If (sigdelset (& sigset, SIGINT) <0)/* remove SIGINT */
Perror ("del error ");
Else
Printf ("SIGINT have been removed! ");
If (sigismember (& sigset, SIGINT)/* judge again */
Printf ("SIGINT exist in signal_set! ");
Else
Printf ("SIGINT not exist in signal_set! ");
}
Output:
$./Sigset
SIGINT exist in signal_set!
SIGTERM exist in signal_set!
SIGABRT exist in signal_set!
SIGINT have been removed!
SIGINT not exist in signal_set!
2. Use of Signal Set
Define Signal Set-> set signal shielding bit-> define signal processing function-> detect signal
<1> use the function in 1 to define the signal set.
<2> set the signal shielding bit
The function is to set the signal to be blocked by a process.
Int sigprocmask (int how, const sigset_t * set, sigset_t * oset );
Parameters
How indicates How to modify the blocked Signal
When Set is a non-null pointer, modify the shielding signal according to how
When Oset is a non-null pointer, it stores the current blocked Signal Set
If set is NULL and the signal shielding word of the process is not changed, how is meaningless.
How values:
S I G B L O C K
The new signal shielding word of this process is the union of the current signal shielding word and s e t pointing to the signal set. S e t contains the additional signal we want to block
S I G U N B L O C K
The new signal shielding word of this process is the intersection of the current signal shielding word and the signal set pointed by s e t. S e t contains the signal we want to unblocking
S I G S E T M A S K
The new signal shielding of this process is the value pointed to by s e t.
[NextPage]
Example
# Include <stdio. h>
# Include <stdlib. h>
# Include <sys/types. h>
# Include <sys/stat. h>
# Include <signal. h>
/* Use of sigprocmsk */
Void msg (int signo)
{
If (signo = SIGINT)
Printf ("Get SIGINT! ");
Else
Printf ("Get SIGQUIT! ");
}
Int main ()
{
Sigset_t sigset, oset;/* sigset stores blocked signals, and oset stores current blocked signals */
Sigemptyset (& sigset);/* clear signal set */
Sigaddset (& sigset, SIGINT);/* Add SIGINT signal, only SIGINT */
Sigprocmask (SIG_BLOCK, & sigset, & oset);/* Add shielded signal */
Signal (SIGINT, msg );
Signal (SIGQUIT, msg );
Sleep (2 );
Raise (SIGINT);/* Send SIGINT signal */
Raise (SIGQUIT); 30 .}
Output
Get SIGQUIT!
<3> define signal processing functions
The s I g a c t I o n function is used to check or modify (or both) the processing action associated with the specified signal. This function replaces the s I g N a l function used in earlier versions of U n I X.
Int sigaction (int signo, const struct sigaction * act, struct sigaction * oact );
Parameter: semaphore for which the signo action is to be detected or modified
When Act is not empty, it indicates the action to be modified
When Oact is not empty, the original action to process the signal is returned.
The structure sigaction is as follows:
Struct sigaction {
Void (* sa_handler) ();/* Handler function or SIG_IGN (ignore) or SIG_DFL (default )*/
Sigset_t sa_mask;/* blocked during function processing */
Int sa_flags;/* flag, signal process processing option */
};
Example
# Include <stdio. h>
# Include <stdlib. h>
# Include <sys/types. h>
# Include <sys/stat. h>
# Include <signal. h>
Void msg (int signo)
{
If (signo = SIGINT)
Printf ("Get SIGINT! ");
}
Int main ()
{
Sigset_t sigset, oset;/* sigset stores blocked signals, and oset stores current blocked signals */
Struct sigaction action1, action2;/* Signal Processing */
Action1.sa _ handler = msg;
Sigaction (SIGINT, & action1, & action2 );
Sleep (2 );
Raise (SIGINT);/* Send SIGINT signal */
}
Output:
Get SIGINT
<4> detect shelved Signals
Int sigpending (sigset_t * set );
Returns the signal set that cannot be delivered or is currently pending when the calling process is blocked. This signal set is returned through the s e t parameter.

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.