main.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 <signal.h>
  19. #include <stdbool.h>
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. bool handle_ok = false;
  23. int count = 0;
  24. void handler(int sig)
  25. {
  26. printf("handle %d\n", sig);
  27. handle_ok = true;
  28. count++;
  29. }
  30. int main()
  31. {
  32. signal(SIGKILL, &handler);
  33. printf("registered.\n");
  34. while (1)
  35. {
  36. // handler(SIGKILL);
  37. printf("Test signal running\n");
  38. raise(SIGKILL);
  39. if (handle_ok)
  40. {
  41. printf("Handle OK!\n");
  42. handle_ok = false;
  43. }
  44. if (count > 0)
  45. {
  46. signal(SIGKILL, SIG_DFL);
  47. }
  48. }
  49. return 0;
  50. }