Linux interprocess communication--signal set functions Sigemptyset (), Sigprocmask (), sigpending (), Sigsuspend ()

Source: Internet
Author: User
Tags exit in set set sigint signal

We already know that we can terminate the process by signal, or through the signal to communicate between processes, the program can also specify the signal's associated processing function to change the signal's default processing mode, you can also block some signals, so that it cannot be passed to the process. So how do we set the signal we need to deal with, and what signals do we need to deal with? The signal set function is to help us solve these problems.

For more information about using signal communication between Linux processes, you can refer to my other article, Linux interprocess communication-semaphore function signal (), sigaction ()

Here is the set of signal functions:

1, int sigemptyset(sigset_t *set);

The function is to initialize the signal set to NULL.

2, int sigfillset(sigset_t *set);

The function is to initialize the signal set to include all the defined signals.

3, int sigaddset(sigset_t *set, int signo);

The function is to add the signal signo to the signal set set, return 0 on success, and 1 on failure.

4, int sigdelset(sigset_t *set, int signo);

The function is to remove the signal signo from the set of signals, return 0 on success, and 1 on failure.

5, int sigismember(sigset_t *set, int signo);

The function is to determine whether a given signal signo is a member of the signal set, if it is returned 1, if not, returns 0 if the given signal is invalid, returns-1;

6, int sigpromask(int how, const sigset_t *set, sigset_t *oset);

The function can modify the signal screen word of the process according to the method specified by the parameter. The new signal-shielding word is specified by the parameter set (NOT NULL), and the original signal-shielding word is saved in Oset (non-empty). If set is null, how is meaningless, but at this point the function is called and if Oset is not empty, the current signal mask word is saved to oset.

The different values and operations of how are as follows:

If Sigpromask completes successfully returns 0, if the How value is invalid, return-1, and set errno to Einval.

Note : Call this function to change the screen word of the process, the previous function is to change the value of a variable, does not really affect the screen word of the process.

7, int sigpending(sigset_t *set);

The function is to put the blocked signal in the pending state of a set of signals to write to the parameter set point to the signal set, the successful call returned 0, otherwise return 1, and set errno to indicate the cause of the error.

8, int sigsuspend(const sigset_t *sigmask);

The function suspends the execution of the process by replacing the screen word of the process with the set of signals given by the parameter sigmask. Note that the sequence of operations is to replace the execution of the pending program first. The program will continue to execute after the signal processing function has finished executing. If the received signal terminates the program, Sigsuspend () does not return, and if the received signal does not terminate the program, Sigsuspend () returns 1, and the errno is set to Eintr.

Special Reminder : If a signal is blocked by the process, it will not be passed to the process, but will stay in the pending state, when the process is de-processing signal blocking, the pending signal will be processed immediately.

Here is an example to illustrate the use of the above function, the source file is sigset.c, the code is as follows:

#include <stdio.h> #include <signal.h> #include <unistd.h>void handler (int sig) {printf ("Handle the Signal%d\n ", SIG);} int main (int argc, char **argv) {sigset_t sigset;//used to record the mask word sigset_t ign;//used to record blocked (masked) signal sets struct sigaction act;//clear signal set sig Emptyset (&sigset); Sigemptyset (&ign);//Add Sigintsigaddset (&sigset, SIGINT) to signal set,//Set processing function and signal set Act.sa_ Handler = Handler;sigemptyset (&act.sa_mask); act.sa_flags = 0;sigaction (SIGINT, &act, 0);p rintf ("Wait the Signal signal...\n ");p ause ();//Set the process screen word, in this case the Mask sigintsigprocmask (sig_setmask, &sigset, 0);p rintf (" Please press Ctrl + C in seconds...\n "), Sleep (10),//test whether SIGINT is masked sigpending (&ign), if (Sigismember (&ign, SIGINT)) {printf ("The SIGINT signal has ignored\n");} Remove signal sigintsigdelset from signal set (&sigset, SIGINT);p rintf ("Wait for the Signal sigint...\n")//Reset the screen character of the process, that is, remove the mask for SIGINT/ /and Suspend process Sigsuspend (&sigset);p rintf ("The app would exit in 5 secondes!\n"); sleep (5); return 0;}

The results of the operation are as follows:

First, we can cross the Sigaction () function to change the default behavior of the SIGINT signal so that it executes the specified function handler, so the statement is output: Handle the signal 2. Then, through the Sigprocmask () set the process signal shielding word, the SIGINT signal shielding, so after 10 seconds, with the sigpending () function to obtain the blocked signal set, detected the blocked signal SIGINT, output the SIGINT Signal has ignored. Finally, using the function Sigdelset () function to remove the previously added Sigaddset () function on the sigset signal SIGINT, and then call the function sigsuspend (), Modify the screen word of the process again to sigset (not including SIGINT) and suspend the process. Because the previous SIGINT signal stays in the pending state, and now the process is no longer blocking the signal, so the process immediately processing the signal, so that at the end, you do not have to input CTRL + C will also appear after the processing of the statement (see the above special reminder), and finally after 5 seconds the program successfully exited.

Reference:

http://blog.csdn.net/ljianhui/article/details/10130539

Linux interprocess communication--signal set functions Sigemptyset (), Sigprocmask (), sigpending (), Sigsuspend ()

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.