A signal is used to notify a process that something has happened, a process can be sent to another process, or a process can be sent by the kernel.
Each signal has an associated behavior that can be set by the Sigaction function to act on a signal, with 3 options:
1. Define a signal processing function to capture the signal, which is called when the signal occurs. Sigkill and Sigstop signals cannot be captured
Signal processing function prototype: void handler (int signo)
2, ignoring the signal, that is, the behavior of the signal is set to Sig_ign, the same sigkill and sigstop signal can not be ignored
3, the default behavior, that is, the signal behavior is set to SIG_DFL
Signal function: Signal interface of standard C
void (*signal (int signo, void (*func) ( int)))) (int)
The bold part is the function name and the argument list, the no bold part is the function return type, returns a parameter is an int, there is no return value of the functions pointer.
The first parameter is the signal name, the second parameter is a pointer to the signal handler function, or is a constant sig_ign or SIG_DFL
sigaction function: POSIX standard signal interface
int sigaction (int signo, const struct sigaction *act, Sigaciton *oact)
Parameter Act defines the behavior of the signal
The function saves the previous behavior of the signal to the position indicated by the parameter oact.
struct Sigaction {
union{
__sighandler_t Sa_handler; Sig_ign or SIG_DFL
void (*_sa_sigaction) (int,struct siginfo *, void *);//Signal processing function pointer
}_u
sigset_t Sa_mask; the signals in the signal set are added to the signal-shielding word of the process, which can be blocked and not passed to the process
unsigned long sa_flags;//Set flags to control the operation of signal processing functions
void (*sa_restorer) (void);//Temporarily unused
}
Processing of SIGCHLD Signal
When a process terminates, it sends a SIGCHLD signal to its parent process.
In a multi-process program, if a child process is in a zombie state, it can cause a waste of resources, so you need to capture the SIGCHLD signal and call the wait or waitpid function in the signal processing function
Handle Zombie processes.
In a network program, capturing a signal can disrupt system calls. When the system call is interrupted and the signal handler returns, the system call may return a EINTR error record in errno.
Can restart system calls with EINTR errors
Sigpipe Signal
When a process writes data to a set of interfaces that have received an RST, the kernel sends a SIGPIPE signal to the process, and the default behavior is to terminate the process. So it is necessary for the process to capture this signal and define
The desired behavior.
POSIX signal processing