xref: /DragonOS/user/apps/test_sigint/main.c (revision a8753f8fffb992e4d3bbd21eda431081b395af6b)
1*a8753f8fSMemoryShore #include <stdio.h>
2*a8753f8fSMemoryShore #include <signal.h>
3*a8753f8fSMemoryShore #include <unistd.h>
4*a8753f8fSMemoryShore #include <stdlib.h>
5*a8753f8fSMemoryShore 
6*a8753f8fSMemoryShore // 信号处理函数
handle_signal(int signal)7*a8753f8fSMemoryShore void handle_signal(int signal)
8*a8753f8fSMemoryShore {
9*a8753f8fSMemoryShore     if (signal == SIGINT)
10*a8753f8fSMemoryShore     {
11*a8753f8fSMemoryShore         printf("Caught SIGINT (Ctrl+C). Exiting gracefully...\n");
12*a8753f8fSMemoryShore         exit(0); // 终止程序
13*a8753f8fSMemoryShore     }
14*a8753f8fSMemoryShore }
15*a8753f8fSMemoryShore 
main()16*a8753f8fSMemoryShore int main()
17*a8753f8fSMemoryShore {
18*a8753f8fSMemoryShore     // 注册信号处理函数
19*a8753f8fSMemoryShore     signal(SIGINT, handle_signal);
20*a8753f8fSMemoryShore 
21*a8753f8fSMemoryShore     // 模拟一个长时间运行的进程
22*a8753f8fSMemoryShore     while (1)
23*a8753f8fSMemoryShore     {
24*a8753f8fSMemoryShore         printf("Running... Press Ctrl+C to stop.\n");
25*a8753f8fSMemoryShore         sleep(5);
26*a8753f8fSMemoryShore     }
27*a8753f8fSMemoryShore 
28*a8753f8fSMemoryShore     return 0;
29*a8753f8fSMemoryShore }
30