[Learning note] signal blocking and not up

Source: Internet
Author: User
Tags sigint signal

The representation of the signal in the kernel

Q The processing action of the signal is called signal recursion (Delivery), the signal from the generation to the state between the recursion, called Signal pending (Pending). A process can choose to block (block) a signal. When the blocked signal is generated, it remains in the pending state until the process has unblocked the signal and executes the recursive action.

Q Note that blocking and ignoring are different, as long as the signal is blocked will not be recursive, and ignore is a recursive after the optional processing action. The signal's representation in the kernel can be seen as:

Description 1) PCB Process Control block in the structure of the Signal block state word (block), the signal pending status word (pending) and whether to ignore the flag;

Description 2) Signal shielding status word (block), 1 for blocking, 0 for non-blocking, signal pending status Word (Pending) 1 is pending (indicating there is no signal), 0 means the signal can be reached;

Note 3) Send SIGINT to the process, the kernel first determine whether the signal screen status word is blocked, the signal pending status word (pending corresponding bits made 1; If the blocking is lifted, the signal pending status word (pending) corresponding bits made 0; signal can be reached.

Description 4) block status word, pending status word 64bit;//socket Select

Description 5) The Block status Word user can read and write, pending status Word user can only reading; This is the signal design mechanism.

Think 1: The state word is 64bit, when programming, how to represent the state word that?

Think about how the 2:block status word information Gets or operates? What APIs?

Think about how the 3:pending status word information Gets or operates? What APIs?

Signal Set operation function (state Word representation)

Q #include <signal.h>

Q int Sigemptyset (sigset_t *set); Empty the signal set 64bit/8=8 bytes

Q int Sigfillset (sigset_t *set); Put the signal set to 1.

Q int Sigaddset (sigset_t *set, int signo); According to Signo, the corresponding bit in the signal set is set to 1

Q int Sigdelset (sigset_t *set, int signo); According to Signo, the corresponding bit in the signal set is set to 0

Q int sigismember (const sigset_t *set, int signo);//Determine if Signo is in the signal set

Sigprocmask read or change the signal mask status word (block) of a process

Q #include <signal.h>

Q int sigprocmask (int how, const sigset_t *set, sigset_t *oset);

Q function: Read or change the signal screen word of the process.

Q return Value: 0 if successful, 1 if an error occurs

Q If the Oset is a non-null pointer, the current signal screen word of the read process is passed through the Oset parameter. If the set is a non-null pointer, the signal mask word for the process is changed, and the parameter how indicates how to change it. If both the Oset and the set are non-null pointers, the original signal shielding word is backed up into the oset, and the signal screen word is changed according to the set and how parameters. Assuming that the current signal mask is mask, the following table describes the optional values for the How parameter.

how meaning

Description: Sig_block, which tells the set of signal sets added to the process BLOCK status Word.

Sigpending get signal Pending status Word (pending) information

NAME

Sigpending-examine Pending signals

Synopsis

#include <signal.h>

int sigpending (sigset_t *set);

DESCRIPTION

Sigpending () returns the set of signals that is pending for delivery to the calling thread (i.e., the signals which has  been raised while blocked). The mask of pending signals is returned in set.

Signal blocking not up to programming practice

Comprehensive experiment,

SIGINT signal not set blocking, view pending keywords

SIGINT signal Settings Blocking to view pending keywords

SIGINT signal unblocked, view pending keyword (unblock via ctrl+\ sigquit)

Demonstrate the entire process of signaling from generation to arrival

A comprehensive experiment on blocking and unblocking of signals

Set signal blocking and non-blocking, set ctl+q to unblock signal

void handler (int sig)

{

if (sig = = SIGINT)

printf ("Recv a sig=%d\n", SIG);

else if (sig = = Sigquit)

{

sigset_t Uset;

Sigemptyset (&uset);

Sigaddset (&uset, SIGINT);

Ctr + \ Used to contact SIGINT signal

Unblocking

Sigprocmask (Sig_unblock, &uset, NULL);

}

}

void Printsigset (sigset_t *set)

{

int i;

for (I=1; i<nsig; ++i)

{

if (Sigismember (set, i))

Putchar (' 1 ');

Else

Putchar (' 0 ');

}

printf ("\ n");

}

3 consecutive Press CTRL + C keyboard, although sent a plurality of SIGINT signals, but because the signal is not stable, only one reserved.

Queuing is not supported

int main (int argc, char *argv[])

{

sigset_t PSet; The set of signals used to print

sigset_t bset; Used to set the blocked signal set

Sigemptyset (&bset);

Sigaddset (&bset, SIGINT);

if (signal (SIGINT, handler) = = Sig_err)

Err_exit ("Signal error");

if (signal (sigquit, handler) = = Sig_err)

Err_exit ("Signal error");

Read or change the signal screen word of a process here to block the CTRL + C signal

The CTRL + C signal is set to block, even if the user presses the Ctl+c keyboard and does not arrive

Sigprocmask (Sig_block, &bset, NULL);

for (;;)

{

Get pending word Information

Sigpending (&pset);

Print signal pending sigset_t Word

Printsigset (&pset);

Sleep (1);

}

return 0;

}

Copy to Google TranslateTranslation Results

[Learning note] signal blocking and not up

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.