|
|
看到原创小软件板块里几乎都是 Windows 程序,于是我突发灵感,写了一个用 Ctrl + C 关不掉的 Linux 程序
原理也很简单,Ctrl + C 结束进程是通过发送 SIGINT 信号实现的,kill 和 killall 结束进程默认通过发送 SIGTERM 信号实现,因此把 SIGINT 和 SIGTERM 信号捕获即可。
源码如下:
- #include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void sig_handler(int signo) {
switch (signo) {
case SIGINT:
puts("You pressed Ctrl + C, but it won't exit!");
break;
case SIGTERM:
puts("You want to terminate the program, but it won't exit!");
break;
}
}
int main() {
struct sigaction action = {.sa_handler = sig_handler};
sigaction(SIGINT, &action, NULL);
sigaction(SIGTERM, &action, NULL);
while (1)
sleep(1);
return 0;
}
------
正确解法:发送 SIGKILL 信号强制结束程序
执行
或
|
|