Sigprocmask, sigpending and Sigsuspend functions __ functions

Source: Internet
Author: User
Sigprocmask function:

Function Description:
Sets the mode of processing (blocking or not blocking) the signal within the set of the signal shield.


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 was stored in Oldset.
Parameters:
How: To specify the way the signal is modified, there may be three kinds of choices

Sig_block//Add signal to process mask.
Sig_unblock//Removes the signal from the process shield.

Sig_setmask//Sets the value of set to a new process mask.

corresponding function command:

sigset_t Set

Sigemptyset (&set): Empty blocking Signal collection variable

Sigfillset (&set): Add all the signals to the blocking set variable

Sigaddset (&set,sigint): Add a single signal to the blocking signal set variable

Sigdelset (&set,sigint): Remove a single signal from a blocking signal collection variable

Sigismember (&set,int signum): This function tests whether the signal signum is contained in the set of the signal set, returns 1 if it contains a return of 1, does not contain a return of 0. 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; To define a variable that blocks a set of signals
Sigemptyset (&set); Empty the blocking signal of the variable set
Sigaddset (&set,sigint); Add the signal to be blocked "SIGINT" to the blocking signal set variable
Sigdelset (&set,sigint); Remove the blocking signal "SIGINT" from an existing blocking signal set variable
Sigaddset (&set,sigquit); Add the signal to be blocked "sigquit" to the blocking signal set variable
Sigprocmask (Sig_setmask,&set,null)//sets the current blocking signal collection variable set to the process signal blocking list
while (1); //
return 0;
}

The results show:

[Elbort@elbort test1]$./test
^\^\
^c
[Elbort@elbort test1]$

Description: ^\ keyboard is CTRL + \ corresponding Signal sigquit

The ^c keyboard is CTRL + C corresponds to the signal SIGINT

The result shows that the test process shields the signal sigquit, and there is no shielding signal SIGINT.


sigpending function:

Function: Returns a 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);
  & nbsp printf ("Pending set is%8.8ld.\n", PSet);

    Kill (Getpid (), sigquit);
    sigpending (&pset);
  & nbsp printf ("Pending set is%8.8ld.\n", PSet);

    Kill (Getpid (), SIGABRT);
    sigpending (&pset);
  & nbsp printf ("Pending set is%8.8ld.\n", PSet);


//    sigprocmask (Sig_unblock, &set, &oset);

    if (sigismember (&pset,sigint) = 1)
    {
    & nbsp;   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);
&nbs P;}

Run Results:

[Elbort@elbort 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.
[Elbort@elbort test1]$

Note: The red sentence gets rid of//will get a completely different result. Why..


Sigsuspend function

Function: It has a set of its own shielding signal mask, can selectively receive certain signals. Before you can get a viable signal (that is, a signal that is not shielded), it will hang until it is a bit similar to the pause () function. After receiving the feasible signal, it exits the suspension and executes the corresponding signal function. Received signal Source: 1. Previously running the blocking signal in the Sigprocmask () function, 2. Signal received after suspension.

Code Description:

Example:sigsuspend (correct Way to a 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 process for Sigsuspend is:
(1) Setting a new mask blocking the current process;
(2) receiving the signal to restore the original mask;
(3) Calling the signal processing function set by the process;
(4) After the signal processing function returned, Sigsuspend return.


Specific examples illustrate:

#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'll 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's the Sigprocmask signal Block,before use sigsuspend\n");
Sleep (5);
printf ("\nsleep Over,next is sigsuspend\n");
Sigsuspend (&set);
printf ("\ni have return to the handler.") I'll sleep 5 seconds your 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 source for Sigprocmask () block

[Elbort@elbort test1]$./test
This is 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'll sleep 5 seconds your can press Ctrl-c to test
^c^c^c
I have return to the handler. I'll sleep 5 seconds your can press Ctrl-c to send SIGINT to test the mask of SIGINT Restore from Sigsuspend for SIGPROCM Ask
^c^c^c^\^\^\
Test Finish
[Elbort@elbort test1]$


2.sigsuspend receive signal source for the thread to enter the signal received after the suspension

[Elbort@elbort test1]$./test
This is 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'll sleep 5 seconds your can press Ctrl-c to test
^c^c^c^c^c
I have return to the handler. I'll sleep 5 seconds your 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
[Elbort@elbort test1]$




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.