Capture signals in Linux and capture signals in Linux
Knowledge about signal here
There are three signal processing methods:
If the signal processing is a user-defined function, the user-defined function is called when the signal is delivered.Capture Signal.
After receiving a signal, the process will not be processed immediately, but will process it at the right time! That isBefore the kernel state is returned to the user State!
However, because the code of the signal processing function is in the user space, this increases the complexity of the kernel processing signal capturing.
To capture signals, follow these steps:
Signal
Register a corresponding processing function for a signal of a process (marked as signum), that is, modify the default processing action of the signal to the method pointed by the handler function;
# Include <signal. h> typedef void (* sighandler_t) (int); sighandler_t signal (int signum, sighandler_t handler );
// That is:
Void (* signal (int, void (*) (int );
The signal function accepts two parameters: an integer signal number and a pointer to a user-defined signal processing function.
In addition, the return value of the signal function is a pointer to the call to the User-Defined signal processing function.
Sigaction
The sigaction function can read and modify the processing actions associated with the specified signal.
# Include <signal. h> int sigaction (int signum, const struct sigaction * act, struct sigaction * oldact); struct sigaction {void (* sa_handler) (int ); // Signal Processing Method void (* sa_sigaction) (int, siginfo_t *, void *); // the processing method of real-time signals is not discussed for the moment; // int sa_flags; void (* sa_restorer) (void );};
Signum is the ID of the specified signal.
Processing Method:
(Note: The last two parameters are both input and output parameters !)
Three methods are available for sa_handler:
(Note: This is a callback function, not called by the main function, but called by the System)
When a signal processing function is called, the kernel automatically adds the current signal to the process's signal shielding word. When the signal processing function returns, the original signal shielding word is automatically restored, this ensures that when processing a signal, if the signal is generated again, it will be blocked until the current processing ends.
Pause
The pause function suspends the calling process until a signal is delivered!
#include <unistd.h>int pause(void);
Processing Method:
- If the signal processing is to terminate the process, the process is terminated, and the pause function has no chance to return;
- If the signal processing is ignored, the process continues to be suspended, and pause does not return;
- If the signal is captured, pause returns-1 after the signal processing function is called, and errno is set to EINTR.
Therefore, pause only returns error values (similar to the exec function family ). The error code EINTR indicates "interrupted by signal ".
Example
/*************************************** * *********************************> File Name: pause. c> Author: Lynn-Zhang> Mail: iynu17@yeah.net> Created Time: sun 14 Aug 2016 12:27:03 pm cst ********************************* ***************************************/ # include <stdio. h> # include <signal. h> # include <unistd. h> void sig_alarm (int signum) {printf ("I am a custom handler! \ N ");} void mysleep (unsigned int times) {// register two signal processing actions, struct sigaction new, old; new. sa_handler = sig_alarm; // signal processing function sigemptyset (& new. sa_mask); // do not shield any signal shielding word new. sa_flags = 0; // The default processing action for the SIGALRM signal is changed to the custom processing action sigaction (SIGALRM, & new, & old); alarm (times); pause (); // pending alarm (1); sleep (2); alarm (0); // cancel the alarm // resume the SIGALRM signal to the default processing action sigaction (SIGALRM, & old, NULL); alarm (1); sleep (2);} int main () {while (1) {mysleep (2); printf ("seconds passed \ n "); printf ("#################### \ n");} return 0 ;}
Define an alarm and wait. After receiving the signal, execute a custom processing action. Before the default processing action is restored, the SIGALRM signal will be processed according to its custom processing function. After the User-Defined processing action is restored and the SIGALRM signal is received, the default processing action is executed to terminate the process!