Summary: This article will dissect the ANSI <signal.h> Library and demonstrate how to use its interfaces. The POSIX signal processing API is then discussed.
Signal processing is similar to hardware interrupts. They cause a process to jump out of the current execution control process to implement a specific behavior that can be resumed until a specific process completes and then to the breakpoint. This article will dissect the ANSI <signal.h> Library and demonstrate how to use its interfaces. This article then discusses the POSIX signal processing APIs. By default, some signals cause the process to terminate. For example, attempting to access memory that the process does not own will trigger a SIGSEGV ("fault") signal that will terminate the execution of the process. Many applications have this problem, which is something we don't want to see. debugging, emulation and transaction processing systems must process such signals so that the process can continue to execute. So how do we prevent this from happening?
The answer is to install a processor to process incoming signals and capture them when they occur
Step one: Establish a signal processor
A signal is an integer that the kernel passes to a process. When a process receives a signal, it responds in one of the following ways:
ignoring the signal;
Let the kernel complete the default action associated with the signal;
Capturing the signal allows the kernel to pass control to the signal processing routines, such as the signal processing routines are completed, and then the execution of the program is recovered from the interrupted place.
A signal processing routine is a function that is automatically invoked by the kernel when a signal occurs. The signal () function registers a processing routine for a given signal:
typedef void (*handler)(void);
void * signal(int signum, handler);
The first parameter is the signal encoding. The second parameter user-defined function address, when the signal Signum is generated, the function that the handler points to is invoked.
In addition to the function address, the second parameter can also be two special values: Sig_ign and SIG_DFL. Sig_ign indicates that the signal should be ignored (note that SIGKILL and SIGSTOP cannot be blocked, captured, or ignored in any case); SIG_DFL indicates that the kernel completes the default behavior when the signal is generated.
Step two: Send a signal
There are three ways to send a signal to a process:
The process uses the raise () to send the signal explicitly to oneself;
The signal is sent from another process, for example, through a Kill () system call or a Perl script;
The signal is sent from the kernel. For example, when a process attempts to access memory that is not part of itself, or when memory is accessed during system shutdown;