First, the signal is a simulation of the interrupt mechanism at the software level, in principle, a process receives a signal and the processor receives an interrupt request can be said to be the same. The signal is asynchronous, and a process does not have to wait for the signal to arrive by any action, in fact, the process does not know exactly when the signal arrives.
ii. Types of signalsThe name of the signal is defined in the header file signal.h, and the signal is entered kill-l the command line at the beginning of the sig to see all the signals. Third, process response to the signal
A process can respond to a signal in three ways:
(1) Ignore the signal, that is, the signal does not do any processing, wherein, there are two signals can not be ignored: Sigkill and sigstop;
(2) Capture the signal. The signal processing function is defined and the corresponding processing function is executed when the signal occurs;
(3) Perform the default operation, and Linux specifies the default action for each type of signal. Note that the default response of a process to a real-time signal is a process termination.
Which of the three ways Linux responds to a signal depends on the parameters passed to the corresponding API function.
Iv. Transmission of Signals
The main functions for sending signals are: Kill (), raise (), Sigqueue (), Alarm (), Setitimer (), and Abort ().
Kill (int pid, int signl) can either send a signal to itself or send a signal to another process
Raise (int signl) can only send signals to the process itself.
Alarm () sends itself a SIGALRM signal after a specified time, and if this signal is not captured, the process is terminated by default.
unsigned int alarm (unsigned int seconds); After how many seconds
Pause () suspends the process until it snaps to a signal.
V. Processing of Signals
sighandler_t signal (int signum, sighandler_t handler);
Signal (SIGINT, my_func);
The first parameter specifies the value of the signal,
The second parameter specifies the processing of the preceding signal value, which can be ignored (the parameter set to Sig_ign) can be processed by the system default (parameter set to SIG_DFL), or you can implement the processing method (parameter specifies a function address) my_func ().
If the signal () call succeeds, returns the handler value of the last Call to signal () for the installation signal Signum, and the failure to return Sig_err.
#include <signal.h>#include<stdio.h>#include<stdlib.h>voidMy_func (intsign_no) { if(sign_no==SIGINT) printf ("I have get sigint\n"); Else if(sign_no==sigquit) printf ("I have get sigquit\n");}intMain () {printf ("waiting for signal SIGINT or sigquit \ n"); /*registering signal processing functions*/signal (SIGINT, my_func); Signal (Sigquit, my_func); Pause (); Exit (0);}
Linux Inter-process communication-signal