Signal is a kind of simulation of interrupt mechanism at software level, and it is an asynchronous communication mode.
Signals can interact directly between user-space processes and kernel processes, which can also be used by kernel processes to inform user-space processes of those system events.
If the process is not currently in the execution state, the signal is saved by the kernel until the process resumes execution and then passes it, and if a signal is set by the process to block, the transmission of the signal is delayed until its blocking is canceled before it is passed to the process.
the generation of signals
1. When the user presses certain keys at the terminal, the terminal driver sends a signal to the foreground process, for example CTR+C generates SIGINT, CTR + \ Generates Sigqui signal, Ctr + Z generates SIGTSTP.
2. Hardware anomalies generate signals that the hardware detects and notifies the kernel, and the kernel sends the appropriate signal to the current process. For example, the current process executes the instruction divided by 0, and the CPU's unit of operation generates an exception, which the kernel interprets as a SIGFPE signal sent to the process. If the current process accesses an illegal memory address, the MMU generates an exception, and the kernel interprets the exception as a SIGSEGV signal sent to the current process.
3. A process call int kill (pid_t pid,int sig) function can send a signal to another process
4. You can send a signal to a process with the kill command, and if you do not explicitly specify a signal to send a sigterm signal, the default processing action of the signal is to terminate the process.
5. When the kernel detects that a certain software condition occurs, it can also notify the process by signaling, for example, the alarm time-out generates a SIGALRM signal and generates a sigpipe signal when writing data to a pipe that has been closed by the reader.
process-to-signal processing
1. Ignore this signal
Most of the signals are handled in this way, fearing that the two signals must never be ignored. They are: SIGKILL and SIGSTOP. The reason these two signals cannot be stopped is that they provide a way for the superuser to terminate or stop the process.
2. Perform the actions desired by the user
Notifies the kernel that a user function is called when a signal occurs. In the user function, perform the processing that the user wants.
3. Perform system default actions
The system default action for most signals is to terminate the process.
Signal-related APIs
int raise(int sig);# 向自己发送信号# sig : 信号的signum
int Kill (pid_t PID, int SIG); # sends a signal to a specified process # parameter description The PID parameters of # kill are in 4 different cases # pid > 0: Send the signal to process ID PID # pid = = 0: Sends the signal to the same group of processes # pid < 0: Sends a signal to a process whose process group ID equals the PID absolute value # pid = =-1: Sends the signal to all processes. # SIG: Signal Signum # signum Reference/HTTP// blog.csdn.net/u011641885/article/details/47252003
unsigned int Alarm (unsigned Span class= "Hljs-keyword" >int seconds); # use the alarm function to set a time value (alarm time), and when the time is set, the SIGALRM signal is generated. # if this signal is not captured, the default action is to terminate the process. # seconds: A signal seconds is generated after the specified sigalrm seconds. # each process can have only one alarm time. # if you have previously set an alarm time for this score when you call alarm, # And it has not timed out, the previously registered alarm time is refreshed # if the previously registered alarm time has not been exceeded, # and this time the seconds value is 0, it means canceling the previous alarm.
int pause(void);# pause 函数使调用进程挂起直至捕捉到一个信号# 只有执行了一个信号处理函数后,挂起才结束。
void (*sighandler_t)(intsignal(int signum, sighandler_t handler);# 注册信号,收到执行信号后做何处理# 参数 handler 有三种情况# SIG_IGN : 忽略该信号# SIG_DFL : 采用系统默认方式处理信号# 自定义的信号处理函数指针.(执行自定义操作)
Example:
#include <signal.h>#include <sys/types.h>#include <stdlib.h>#include <stdio.h>#include <string.h>voidMysingal (intSignum) {Switch(Signum) { CaseSIGINT: Case Break; }return;}intMainintargcChar*argv[]) {Charbuf[ -]; Signal (Sigint,mysingal); while(1){printf("[Email protected]>]); Fgets (BUF,sizeof(BUF), stdin);if(buf[0] ==' \ r ')Continue; System (BUF); } }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The signal of the Linux process communication