linux interprocess communication (IPC) -- signal set and signal blocking set

Signal set overview

A user process often needs to process multiple signals. In order to facilitate the processing of multiple signals, signal set is introduced into linux system. Signal set is used to represent the data type of multiple signals

Signal set data type

sigset_t

Define path

/usr/include/x86_64-linux-gnu/bits/sigset.h(Ubuntu 16.04)

Signal set related operations mainly include the following functions

sigemptyset
sigfillset
sigismember
sigaddset
sigdelset

sigemptyset function

Function: initialize an empty signal set

#include <signal.h>
int sigemptyset(sigset_t * set)

function
Initialize the signal set pointed by set, and clear all the signals, that is, initialize an empty signal set
parameter
Set: the address identified by the signal set. You can operate the signal set later
Return value
0 is returned for success and - 1 is returned for failure

sigfillset function

Function: initialize a full signal set

#include <signal.h>
int sigfillset(sigset_t * set);

function
Initialize the signal set and set the signal set as the set of all signals
parameter
The address identified by the signal set. You can operate this signal set and set later
Return value
0 is returned for success and - 1 is returned for failure

sigismember function

Determine whether there is a signal in a set

#include <signal.h>
int sigismember(const sigset_t * set, int signum);

function
Query whether the signal identified by signum is in the signal set
parameter
set: address of the semaphore identifier 4
signum: signal number
Return value
Returns - 1 in the signal set, not 0 in the signal set
Error, return - 1

sigaddset function

Adds a signal to a collection

#include <signal.h>
int sigaddset(sigset_t * set , int signum);

Function:
Add the signal signum to the signal set
Parameters:
Set: address identified by the signal set
signum; Signal number
Return value:
0 is returned for success and - 1 is returned for failure

sigdelset function

Deletes a signal from a signal set

#include <signal.h>
int sigdelset(sigset_t * set, int signum);

function
Delete the signal identified by signum from the signal set
parameter
Set: address of the semaphore set identifier
signum: signal number
Return value
Success: return 0
Failed: Return - 1

case

#include <stdio.h>
#include <signal.h>
int main()
{
		//Create a signal set
        sigset_t set;
        int ret = 0;
		//Initialize an empty signal set
        sigemptyset(&set);
		//Judge whether the SIGINT signal is in the signal set
        ret = sigismember(&set,SIGINT);
        if(ret == 0)
        {
                printf("SIGINT is not a member of sigprocmask \n ret = %d\n", ret);

        }
		//Adds the specified signal to the signal set
        sigaddset(&set, SIGINT);
        sigaddset(&set, SIGQUIT);
		//Determine whether SIGINT is in the signal set
        ret = sigismember(&set, SIGINT);
        if(ret == 1)
        {
                printf("SIGINT is a member of sigprocmask \nret = %d\n", ret);
        }
        return 0;
}       

Signal blocking set (mask set, mask)

Each process has a blocking set, which is used to describe which signals are blocked when they are delivered to the process (remember it when the signal occurs, and notify the process of the signal until the process is ready)
The so-called blocking is not to prohibit the transmission of signals, but to suspend the transmission of signals. If the blocked signals are deleted from the signal blocking set and the corresponding signals occur when they are blocked, the process will receive the corresponding signals

sigprocmask function

Create a blocking collection

#include <signal.h>
int sigprocmask(int how, const sigset_t* set, sigset_t* oldset);

function
Check or modify the signal blocking set, and modify the blocking set of the process according to the method specified by how. The new signal blocking set is specified by set, while the original signal blocking set is saved by oldset
parameter
how: modification method of signal blocking set
Set: address of the signal set to operate
Save original signal set address
how:
SIG_BLOCK; Adds a set signal set to the signal blocking set
SIG_UNBLOCK; Delete set from signal blocking set
SIG_SETMASK; Set the signal blocking set to set
Note: if set is NULL, the signal blocking set will not be changed, and the function will only save the current signal blocking set to oldset
Return value
Success: return 0
Failed: Return - 1

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
int main()
{
   		  int i = 0;
		//Create a signal set and add signals to the signal set
        sigset_t set;
        sigemptyset(&set);
        sigaddset(&set, SIGINT);
        
        while(1)
        {
        		//Adds a set signal set to a signal blocking set
                sigprocmask(SIG_BLOCK, &set, NULL);
                for(i=0; i<5; i++)
                {
                        printf("SIGINT signal is blocked\n");
                        sleep(1);
                }
                //Delete the set signal set from the signal blocking set
				sigprocmask(SIG_UNBLOCK, &set, NULL);
				for(i=0; i<5; i++)
				{
					printf("SIGINT signal unblocked\n");
					sleep(1);
				}
        }
        return 0;
}

We can see that after five seconds, run the signal SIGINT in set immediately
Generally, the signal blocking set will play with the signal set, because the signal blocking set is added to the signal set, not a signal

Keywords: Linux

Added by xmarcusx on Mon, 27 Dec 2021 12:33:06 +0200