xref: /DragonOS/user/apps/test_signal/main.c (revision 70a4e5550a9fb49b537092287c3ddc36448c5b78)
1 /**
2  * @file main.c
3  * @author longjin (longjin@RinGoTek.cn)
4  * @brief 测试signal用的程序
5  * @version 0.1
6  * @date 2022-12-06
7  *
8  * @copyright Copyright (c) 2022
9  *
10  */
11 
12 /**
13  * 测试signal的kill命令的方法:
14  * 1.在DragonOS的控制台输入 exec bin/test_signal.elf &
15  *      请注意,一定要输入末尾的 '&',否则进程不会后台运行
16  * 2.然后kill对应的进程的pid (上一条命令执行后,将会输出这样一行:"[1] 生成的pid")
17  *
18  */
19 
20 #include <math.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <time.h>
25 #include <unistd.h>
26 #include <stdbool.h>
27 bool handle_ok = false;
28 int count = 0;
29 void handler(int sig)
30 {
31     printf("handle %d\n", sig);
32     handle_ok = true;
33     count++;
34 }
35 
36 int main()
37 {
38     signal(SIGKILL, &handler);
39     printf("registered.\n");
40 
41     while (1)
42     {
43         // handler(SIGKILL);
44         printf("Test signal running\n");
45         raise(SIGKILL);
46         if (handle_ok)
47         {
48             printf("Handle OK!\n");
49             handle_ok = false;
50         }
51         if (count > 0)
52         {
53             signal(SIGKILL, SIG_DFL);
54         }
55     }
56 
57     return 0;
58 }