Signal Processing (ii)

Source: Internet
Author: User

1.1. Sigprocmask Signal Blocking

The set of blocked signals in the function sigaction is only for signals to be processed, such as

struct Sigaction Act;

Sigemptyset (&act.sa_mask);

Sigaddset (&act.sa_mask,sigquit);

Sigaction (Sigint,&act,null);

Indicates that the signal sigquit is blocked only when the signal SIGINT is processed;

Function Sigprocmask is the whole block, after setting up the blocking set in Sigprocmask, the blocked signal will no longer be captured by the signal processing function until the block signal set is reset.

The prototypes are:

#include <signal.h>

int sigprocmask (int how, const sigset_t *set, sigset_t *oldset);

The parameter how value is one of the following 3:

A:sig_block, adding a signal set of parameter 2 to the existing block signal set of the process

B:sig_unblock, removing the signal contained in parameter 2 from the original block signal set of the process

C:sig_setmask, reset the signal set for the blocking signal set of the process to parameter 2

Parameter set is a block signal set

Parameter oldset is an outgoing parameter that holds the original signal set of the process, usually null

Example: Adding a full block

#include <stdio.h>

#include <string.h>

#include <unistd.h>

#include <signal.h>

int g_iseq=0;

void signhandlernew (int isignno,siginfo_t *pinfo,void *preserved)

{

int iseq=g_iseq++;

printf ("%d Enter signhandlernew,signo:%d\n", Iseq,isignno);

Sleep (3);

printf ("%d Leave signhandlernew,signo:%d\n", Iseq,isignno);

}

int main ()

{

Char Szbuf[8];

int iRet;

// Shield off Sigquit Signal

sigset_t Sigset;

Sigemptyset (&sigset);

Sigaddset (&sigset,sigquit);

Sigprocmask (Sig_block,&sigset,null);

struct Sigaction Act;

Act.sa_sigaction=signhandlernew;

Act.sa_flags=sa_siginfo;

Sigemptyset (&act.sa_mask);

Sigaction (Sigint,&act,null);

while (1);

return 0;

}

Example: Cancels the entire block of the specified signal.

#include <stdio.h>

#include <string.h>

#include <unistd.h>

#include <signal.h>

int g_iseq=0;

void signhandlernew (int isignno,siginfo_t *pinfo,void *preserved)

{

int iseq=g_iseq++;

printf ("%d Enter signhandlernew,signo:%d\n", Iseq,isignno);

Sleep (3);

printf ("%d Leave signhandlernew,signo:%d\n", Iseq,isignno);

}

int main ()

{

Shielding off SIGINT and sigquit signals, sighandlernew will no longer be able to capture SIGINT and Sigquit

sigset_t Sigset;

Sigemptyset (&sigset);

Sigaddset (&sigset,sigint);

Sigaddset (&sigset,sigquit);

Sigprocmask (sig_block,&sigset,null);//Will SIGINT, Sigquit Shield

struct Sigaction Act;

Act.sa_sigaction=signhandlernew;

Act.sa_flags=sa_siginfo;

Sigemptyset (&act.sa_mask);

Sigaction (Sigint,&act,null);

Sigaction (Sigquit,&act,null);

int iCount = 0;

while (1)

{

if (ICount > 3)

{

sigset_t SigSet2;

Sigemptyset (&sigset2);

Sigaddset (&sigset2, SIGINT);

Sigprocmask (Sig_unblock, &sigset2, NULL);

}

ICount + +;

Sleep (2);

}

return 0;

}

Example3: Using sigpending to test the signal and adopt the model

#include <signal.h>

#include <unistd.h>

void handler (int signum, siginfo_t* pInfo, void* preversed)

{

printf ("Receive signal%d \ n", Signum);

}

int main ()

{

sigset_t New_mask, Old_mask, Pending_mask;

Sigemptyset (&new_mask);

Sigaddset (&new_mask, SIGINT);

if (Sigprocmask (Sig_block, &new_mask, &old_mask))

printf ("Block signal SIGINT error\n");

struct Sigaction Act;

Sigemptyset (&act.sa_mask);

Act.sa_flags = Sa_siginfo;

Act.sa_sigaction = handler;

if (Sigaction (SIGINT, &act, NULL))

printf ("Install signal SIGINT error\n");

Sleep (10);

printf ("Now begin to get pending mask and unblock sigint\n");

if (sigpending (&pending_mask) < 0)//Add the blocking signal to the pending_mask signal set and return

printf ("Get Pending Mask error\n");

if (Sigismember (&pending_mask, SIGINT))

printf ("Signal SIGINT is pending\n");

if (Sigprocmask (Sig_setmask, &old_mask, NULL) < 0)

printf ("Unblock signal error\n");

printf ("Signal unblocked\n");

Sleep (10);

return 0;

}

Signal Processing (ii)

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.