MemoryShore a8753f8fff
feat(tty): 实现发送SIGINT终止信号 (#952)
* 实现SIGINT终止信号传递

* 添加test_sigint测试程序
2024-10-09 19:02:14 +08:00

30 lines
516 B
C

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
// 信号处理函数
void handle_signal(int signal)
{
if (signal == SIGINT)
{
printf("Caught SIGINT (Ctrl+C). Exiting gracefully...\n");
exit(0); // 终止程序
}
}
int main()
{
// 注册信号处理函数
signal(SIGINT, handle_signal);
// 模拟一个长时间运行的进程
while (1)
{
printf("Running... Press Ctrl+C to stop.\n");
sleep(5);
}
return 0;
}