main.c 1.1 KB

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