Linux signal mechanism analysis

  
                

In Linux, a signal is also called a soft interrupt. The process processes the signal after receiving the signal. It can be said that it is an interrupt process. This article will give you a brief explanation of the Linux signaling mechanism.

1, the signal processing function is installed

on the system programming level most directly related to the relationship between the process signals and has two functions, they are used to install the signal processing function: < Br>

sighandler_t signal(int signum, sighandler_t handler);

int sigaction(int signum, const struct sigaction *act,,struct sigaction *oldact);

first function Signal is relatively simple, sighandler_t is an alias, its prototype is typedef void (*sighandler_t) (int), which is a function pointer, accepts a parameter of type int (signal number), returns void. For example, to process the SIGUSR1 signal:

void handler(int sig)

//strsiganl function is to convert the number of the signal to the string of the signal description < Br>

printf(“Rcv a signal:%s”,strsignal(sig));

}

int main()

{

signal(SIGUSR1,handler);

while(1)

;

}

(This program is actually problematic, As I will see later, this program is originally an infinite loop, but sending a SIGUSR1 signal to him, the program will go from the while in the "interruption" to execute the code in the handler. Use the kill command in the shell to send the signal SIGUSR1 and the program promises a message like this: Rcv a signal: User defined signal 1. The usage of signal() is almost as simple as that. However, for portability reasons, the following function should be used when participating in project development.

There are two structures in the parameters of the sigaction() function. The man page prototype is as follows:

struct sigaction {

void (*sa_handler)(int); Br>

void (*sa_sigaction)(int, siginfo_t *, void *);

sigset_t sa_mask;

int sa_flags;

void (*sa_restorer)( Void);

};

As far as I know, sa_handler and sa_sigaction are actually in a union, they are pointers to signal handlers.

sa_mask is the signal to be masked, and sa_flags has several options. (More on these two points later). From the sigaction() prototype, we can find that there are two struct sigaction parameters in the parameter, where act is the signal processing to be installed, and oldact is used to bring back the original processing method so that we can recover after processing the signal. If you don't need to get back the previous signal processing method, you can set the third parameter to NULL. If you only want to get the previous processing method instead of installing a new signal processing, you can set the second parameter to NULL. Signal() is not available. Rewriting the above example with sigaction() is like this:

1 void handler(int sig)

2 {

3 printf(“Rcv a signal:%s&rdquo ;,strsignal(sig));

4 }

5

6 int main()

7 {

8 struct Sigaction act;

9 sigemptyset(&act.sa_mask);

10 act.sa_handler = handler;

11 act.sa_flags = 0;

12 sigaction(SIGUSR1,&act,NULL);

13 while(1)

14 ;

15 } Previous12Next page Total 2 pages

Copyright © Windows knowledge All Rights Reserved