test_signal.c 1008 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. * 测试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. printf("handle %d\n", sig);
  26. handle_ok = true;
  27. count++;
  28. }
  29. int main() {
  30. signal(SIGKILL, &handler);
  31. printf("registered.\n");
  32. while (1) {
  33. // handler(SIGKILL);
  34. printf("Test signal running\n");
  35. raise(SIGKILL);
  36. if (handle_ok) {
  37. printf("Handle OK!\n");
  38. handle_ok = false;
  39. }
  40. if (count > 0) {
  41. signal(SIGKILL, SIG_DFL);
  42. }
  43. }
  44. return 0;
  45. }