The concept of a signal
Signal (signal)--the way communication between processes is a software interrupt. Once a process receives a signal, it interrupts the original program execution process to process the signal.
Several common signals:
SIGINT terminating process interrupt process (CONTROL+C)
SIGTERM terminating process software termination signal
SIGKILL Terminate process Kill process
SIGALRM Alarm Clock signal
The difference between the process end signal sigterm and Sigkill
Sigterm is friendly and the process captures the signal and shuts down the program according to your needs. Before you close the program, you can end the open record file and complete the task you are doing. In some cases, if the process is working and cannot be interrupted, the process can ignore the sigterm signal.
For Sigkill signals, the process cannot be ignored. This is a "I don't care what you are doing, stop immediately" signal. If you send a sigkill signal to the process, Linux stops the process there.
There are two general reasons for sending a signal:
1 (passive) The kernel detects a system event. For example, a child process exit sends a SIGCHLD signal like a parent process. Keyboard presses CONTROL+C sends SIGINT signal
2 (Active) sends a signal to the specified process via system call Kill
The operating system specifies the default behavior after a process receives a signal
However, we can modify the behavior of the process after it receives the signal by binding the signal handler function.
There are two signals that are immutable sigtop and Sigkill
binding Signal Processing functions :
ImportOSImportSignal fromTimeImportSleepdefOnsignal_term (A, b):Print 'Receive Sigterm signal' #here is the binding signal processing function, which binds sigterm to the function Onsignal_termsignal.signal (signal. Sigterm,onsignal_term)defONSIGNAL_USR1 (A, b):Print 'Receive SIGUSR1 signal' #here is the binding signal processing function, which binds SIGUSR1 to the function Onsignal_termsignal.signal (signal. SIGUSR1,ONSIGNAL_USR1) while1: Print 'My process ID is', Os.getpid () sleep (10)
Run the program. The signal is then sent through another process.
The code to send the message is as follows:
Import OS Import Signal # send a signal, 16175 is the PID that binds the signal processing function in front, need to modify itself Os.kill (16175, signal. SIGTERM) # sends a signal, 16175 is the PID that binds the signal processing function in front, needs to modify itself
where to use the signal requires special attention
:
If a process receives a SIGUSR1 signal and then executes the signal binding function, the second SIGUSR2 signal comes again and the first signal is not processed, the second signal is discarded.
Therefore, try not to use the signal in multi-threading.
This is inappropriate, the test didn't find a signal lost.
Example Demo:
The program that receives the signal, you will find that if there is another end using multithreading to send signals to this process, some signals will be omitted.
The concept of a signal