Linux knowledge lecture: Linux signal and blocking

  
                  


1. Signal Mask - Blocked Signal Set


Each process has a signal set that describes which signals are to be blocked, if some When the signal is in the blocking signal set of a process, the signal transmitted to the process will be blocked. The set of signals currently blocked by the process is also called the signal mask, of type sigset_t. Each process has its own signal mask, and when a child process is created, the child process inherits the signal mask of the parent process.


2. Differences between signal blocking and ignoring


The concept of blocking is different from ignoring signals: the operating system does not pass signals until the signal is unblocked by the process. Going out, the blocked signal will not affect the behavior of the process, the signal is only temporarily blocked from passing; when the process ignores a signal, the signal will be passed out, but the process will discard the signal.


3. Operation of the signal set


The signal set can be operated by the following functions:


int sigemptyset(sigset_t *set) //Clear the signal set


int sigfillset(sigset_t *set); //fill all the signals into the set


int sigaddset(sigset_t *set, int signum Add a signal to the set signum


int sigdelset(sigset_t *set, int signum); //Remove the signal from the set signum


int sigismember (const sigset_t *set, int signum); //determine whether signnum is included in the set, return 1, not return 0


initialization can often use sigemptyset () to clear the signal set, and then Use sigaddset() to add a signal to the signal set; or use sigfillset() to add all signals to the signal set, and then use sigdelset() to remove a signal from it.


4. Introduction to sigprocmask()


You can use the function sigprocmask() to check or modify the signal mask of a process. The function information is as follows:


#include


int sigprocmask ( int how, const sigset_t *restrict set,


sigset_t *restrict old ) ;


The parameter how is an integer indicating how the signal mask is modified:


SIG_BLOCK --- Add the signal in the signal set pointed by set to the current blocking signal


SIG_UNBLOCK --- Removes the signal in the signal set pointed by set from the current blocking signal set;


SIG_SETMASK --- Specifies the signal set pointed to by set Is the current blocking signal set.


In addition, if the parameter set is NULL, the description does not need to be modified. If old is NULL, sigprocmask will return the signal set before modification in *old. //This article transferred from www.45it.com computer software and hardware application network


5.sigaction () Review


used in the previous sigaction () function:


include


int sigaction(int signum,const struct sigaction *act,


const struct sigaction *oldact);


This function is used to register a signal processing function. The parameter structure sigaction has the same name as the function. The specific information is as follows:


struct sigaction {


void (*sa_handler)(int); //Old type signal processing function Pointer


void (*sa_sigaction)(int, siginfo_t *, void *);//New type of signal processing function pointer


sigset_t sa_mask; //will be Blocked signal set


int sa_flags; //Signal processing mode mask


void (*sa_restorer)(void); //Reserved

}



5.1 sa_handler: A function pointer that points to the address of the signal handler (the old type of signal handler) whose prototype is void handler(int);


5.2 sa_sigaction: is also a function pointer used to point to the prototype:


void handler(int (new type of signal processing function);


The meaning of the three parameters is:


iSignNum: the incoming signal


pSignInfo: some information related to the signal, it is a structure


pReserved: Reserved, not used now


5.3 sa_handler and sa_sigaction should only have one Effective, if you want to use the old signal processing mechanism, you should let sa_handler point to the correct signal processing function; otherwise you should let sa_sigaction point to the correct signal processing function, and let the field sa_flags contain the SA_SIGINFO option.


5.4 sa_mask is a structure containing a set of signals whose signals represent the signals to be blocked when signal processing is performed. The set of signals can be manipulated with the five functions mentioned in the previous heading 3. >


5.5 The field sa_flags is a composite of a set of masks indicating some behaviors that should be taken during signal processing. The meaning of each mask is:


(1)SA_RESETHAND --- After processing the signal to be captured, the registration of the signal processing function will be automatically revoked, that is, the signal processing function must be re-registered to continue processing the next generated signal.


(2) SA_NODEFER ---When processing a signal, if another signal occurs again, it immediately enters the processing of other signals. After the other signals are processed, the current signal is processed again, that is, the processing is performed in a regular manner. . If sa_flags contains the mask, the sa_mask of the structure sigaction will be invalid;


(3)SA_RESTART---If the program is blocking, a program is blocking, such as calling The read() function returns from the blocked system after processing the signal. This mask conforms to the normal program processing flow, so in general, the mask should be set, otherwise the blocked system call will return failure after the signal is processed;


(4)SA_SIGINFO - - Indicates which of the signal handler pointers of the structure is valid. If sa_flags contains the mask, the sa_sigactiion pointer is valid, otherwise the sa_handler pointer is valid.


It should be noted that:


The function sigprocmask is a full-time blocking. After the blocking set is set in sigprocmask, the blocked signal can no longer be captured by the signal processing function. Until the set of blocking signals is reset. When sigaction() registers the signal processing function, the selected signal set is only blocked when the captured signal is processed.

Copyright © Windows knowledge All Rights Reserved