SIGTERM SIGKILL的区别
前者可以被阻塞、处理和忽略,但是后者不可以。KILL命令的默认不带参数发送的信号就是SIGTERM.让程序有好的退出。因为它可以被阻塞,所以有的进程不能被结束时,用kill发送后者信号,即可。即:kill -9 进程号。
BLOCKED
如果进程设置了SIGTERM可以被block,则进程处于block状态时无法被SIGTERM信号杀死。
sigset_t blockSet, savedSigMask;
sigemptyset(&blockSet); /* initialize empty */
sigaddset(&blockSet, SIGTERM);
sigprocmask(SIG_BLOCK, &blockSet, &savedSigMask);
/* process will not be interrupted by SIGTERM, which can be restored later*/
Ignored
struct sigaction sa;
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_NODEFER; /* Dont block the signal when it's handler is running */
sigaction(SIGTERM, &sa, NULL);
Handled
struct sigaction sa;
sa.sa_sigaction = functionp; /* functionp is your handler function */
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_NODEFER | SA_SIGINFO;
sigaction(signo, &sa, NULL;
参考链接:
https://stackoverflow.com/questions/62342802/why-sigterm-does-not-kill-some-processes-in-linux
https://blog.csdn.net/qq_26836575/article/details/82147558