一、进程与信号不可靠问题
linux信号不可靠问题:
1,系统层面信号是可靠的
2,用户层面信号是可靠的
将依赖于信号而执行的代码放在信号处理函数中执行,否则这些代码将不被执行
进程在处理过程中是否还可以接收处理信号,相同信号/不同信号
范列
#include <signal.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> void set_signal(int signo) { if(signo == SIGINT) { printf("%d catch SIGINT\n",getpid()); sleep(5); printf("process the sigint finished\n"); } if(signo==SIGTSTP) { printf("%d catch SIGTSTP\n",getpid()); sleep(5); printf("process the sigtstp finished\n"); } } int main() { if(signal(SIGINT,set_signal)==SIG_ERR) { printf("signal error\n"); return 1; } if(signal(SIGTSTP,set_signal)==SIG_ERR) { printf("signal error\n"); return 1; } //暂停等待信号 while(1) pause(); }
编译执行
进程处理中中发送相同信号,先发送ctrl+c 在发送ctrl+c ^C3267 catch SIGINT ^Cprocess the sigint finished 3267 catch SIGINT process the sigint finished 进程处理中发送不同信号,先发送ctrl+c 在发送ctrl+z ^C3267 catch SIGINT ^Z3267 catch SIGTSTP process the sigtstp finished process the sigint finished
结论
进程处理中发送相同信号/不同信号仍然会处理,但是超过2次进程就会屏蔽
信号处理中:信号会创建2个数据结构
信号屏蔽字(mask)
信号未决字(pending)
用户层面信号可靠性
#include <signal.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h> int signal_init=0; void set_signal(int signo) { printf("catch signal %d\n",signo); signal_init =1; } int main() { if(signal(SIGINT,set_signal)==SIG_ERR) { printf("set signal error"); } while(signal_init == 0) { sleep(5); pause(); } printf("process run success\n"); return 0; }