Transfer from http://blog.csdn.net/elbort/article/details/7594772
Sigprocmask function:
Function Description:
Sets the processing mode (blocking or non-blocking) of the signal within the signal shielding set.
Usage:
#include <signal.h>
int sigprocmask (int how, const sigset_t *set, sigset_t *oldset);
Note:if oldset is Non-null, the previous value of the signal mask is stored in oldset.
Parameters:
How: Used to specify the way the signal is modified, there may be a choice of three
Sig_block//Join signal to process mask.
Sig_unblock//Remove the signal from the process shield.
Sig_setmask//Sets the value of the set to the new process mask.
corresponding function command:
sigset_t Set
Sigemptyset (&set): emptying the block signal set variable
Sigfillset (&set): Add all signals to the block set variable
Sigaddset (&set,sigint): Adding a single signal to the block signal set variable
Sigdelset (&set,sigint): Removes a single signal from the block signal set variable
Sigismember (&set,int signum): This function tests whether the signal signum is contained in the set of signals, if it contains a return of 1, does not contain return 0, an error returns-1. The error code also has only one einval, which means that Signum is not a valid signal code.
Code Description: One of the simplest examples
#include <stdio.h>
#include <signal.h>
int main ()
{
sigset_t set; Define a variable that blocks a signal set
Sigemptyset (&set); Emptying the blocking signal of the variable set
Sigaddset (&set,sigint); Add the signal that will be blocked "SIGINT" to the blocking signal set variable
Sigdelset (&set,sigint); Remove blocking signal "SIGINT" from existing blocking signal set variables
Sigaddset (&set,sigquit); Add the signal that will be blocked "sigquit" to the blocking signal set variable
Sigprocmask (sig_setmask,&set,null);//sets the current block signal set variable set to the process signal block list
while (1); //
return 0;
}
The results show:
[Email protected] test1]$./test
^\^\
^c
[Email protected] test1]$
Description: The ^\ keyboard is the CTRL + \ corresponding Signal sigquit
The ^c keyboard is the CTRL + C corresponding signal SIGINT
The result shows that the test process masks the signal sigquit, without shielding the signal SIGINT.
sigpending function:
Function: Returns the collection of blocked signals received during blocking.
Code Description:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
int main (void)
{
sigset_t set, Oset, PSet;
Sigemptyset (&set);
Sigaddset (&set, SIGINT);
Sigaddset (&set,sigquit);
Sigaddset (&SET,SIGABRT);
sigprocmask (Sig_block, &set, &oset);
printf ("Old set is%8.8ld.\n", oset);
printf ("Set is%8.8ld.\n", set);
sigpending (&pset);
printf ("Pending set is%8.8ld.\n", PSet);
Kill (Getpid (), SIGINT);
sigpending (&pset);
printf ("Pending set is%8.8ld.\n", PSet);
Kill (Getpid (), sigquit);
sigpending (&pset);
printf ("Pending set is%8.8ld.\n", PSet);
Kill (Getpid (), SIGABRT);
sigpending (&pset);
printf ("Pending set is%8.8ld.\n", PSet);
// sigprocmask (Sig_unblock, &set, &oset );
if (sigismember (&pset,sigint) = = 1)
{
printf ("SIGINT was came.\n");
}
if (sigismember (&pset,sigquit) = = 1)
{
printf ("Sigquit was came.\n");
}
if (sigismember (&PSET,SIGABRT) = = 1)
{
printf ("SIGABRT was came.\n");
}
/* The program terminates with a SIGINT */
return (exit_success);
}
Run Result:
[Email protected] test1]$./test2
Old set is 00000000.
Set is 00000038.
Pending set is 00000000.
Pending set is 00000002.
Pending set is 00000006.
Pending set is 00000038.
SIGINT was came.
Sigquit was came.
SIGABRT was came.
[Email protected] test1]$
Note: The red sentence is removed//will get a completely different result. Why??
After testing, the discovery really was
SIGINT was came.
Sigquit was came.
SIGABRT was came. These three words did not print out, not very good understanding Ah, Sigprocmask (Sig_unblock, &set, &oset); This is the signal to remove the process set, then Sigismember (&pset,sigint) This &pset signal shielding is also empty? Because it's for the same process? This is the only explanation.
Sigsuspend function
Function: It has its own masking mask, which is capable of selectively receiving certain signals. Before a viable signal is received (that is, a signal that is not masked), it is suspended until it is run, somewhat like the pause () function. When a viable signal is accepted, it exits the suspension and executes the corresponding signal function. Received signal Source: 1. Before running the Sigprocmask () function to block the signal, 2. The signal received after suspension.
Code Description:
Example:sigsuspend (Correct-to-Wait for single Signal)
Sigset_tmaskall, Maskmost, Maskold;
Intsignum = SIGUSR1;
Sigfillset (&maskall);
Sigfillset (&maskmost);
Sigdelset (&maskmost,signum);
Sigprocmask (Sig_setmask,&maskall, &maskold);
Sigsuspend (&maskmost);
Sigprocmask (Sig_setmask,&maskold, NULL);
The entire atomic operation of the Sigsuspend is:
(1) Set a new mask to block the current process;
(2) received a signal to restore the original mask;
(3) Call the signal processing function set by the process;
(4) When the signal processing function returns, Sigsuspend returns.
Specific examples show:
#include <stdio.h>
#include <signal.h>
#include "Unistd.h"
void Fun_int ()
{
printf ("\nsigsuspend catch sigint\n");
printf ("The sigsuspend already restore the old mask for Sigprocmask. Now still in the signal handle,next would sleep 5 seconds can press Ctrl-c to test\n ");
Sleep (5);
Return
}
int main ()
{
sigset_t set;
Sigfillset (&set);
Signal (Sigint,fun_int);
Sigprocmask (Sig_setmask,&set,null);
Sigdelset (&set,sigint);
printf ("Here are the Sigprocmask signal Block,before use sigsuspend\n");
Sleep (5);
printf ("\nsleep Over,next is sigsuspend\n");
Sigsuspend (&set);
printf ("\ni has return from the handler. I'll sleep 5 seconds you can press Ctrl-c to send SIGINT to test the mask of SIGINT Restore from Sigsuspend for SIGPROCM Ask\n ");
Sleep (10);
printf ("\ntest finish\n");
return 0;
}
The results show:
1.sigsuspend received signal from Sigprocmask () block
[Email protected] test1]$./test
Here are the sigprocmask signal Block,before use Sigsuspend
^\^\^\
^c^c^c
Sleep Over,next is Sigsuspend
Sigsuspend Catch SIGINT
The sigsuspend already restore the old mask for Sigprocmask. Now still in the signal handle,next would sleep 5 seconds can press CTRL-C to test
^c^c^c
I have a return from the handler. I'll sleep 5 seconds you can press Ctrl-c to send SIGINT to test the mask of SIGINT Restore from Sigsuspend for SIGPROCM Ask
^c^c^c^\^\^\
Test Finish
[Email protected] test1]$
2.sigsuspend received signal from the thread to enter the signal received after the suspension
[Email protected] test1]$./test
Here are the sigprocmask signal Block,before use Sigsuspend
^\^\^\^\^\
Sleep Over,next is Sigsuspend
^\^\^\^\^\
^c
Sigsuspend Catch SIGINT
The sigsuspend already restore the old mask for Sigprocmask. Now still in the signal handle,next would sleep 5 seconds can press CTRL-C to test
^c^c^c^c^c
I have a return from the handler. I'll sleep 5 seconds you can press Ctrl-c to send SIGINT to test the mask of SIGINT Restore from Sigsuspend for SIGPROCM Ask
^c^c^c^c^c^\^\^\
Test Finish
[Email protected] test1]$
Sigprocmask, sigpending, and Sigsuspend functions