main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <signal.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #define TEST_ASSERT(left, right, success_msg, fail_msg) \
  6. do { \
  7. if ((left) == (right)) { \
  8. printf("[PASS] %s\n", success_msg); \
  9. } else { \
  10. printf("[FAIL] %s: Expected 0x%lx, but got 0x%lx\n", \
  11. fail_msg, \
  12. (unsigned long)(right), \
  13. (unsigned long)(left)); \
  14. } \
  15. } while (0)
  16. static int signal_received = 0;
  17. void signal_handler(int signo) {
  18. if (signo == SIGINT) {
  19. printf("\nReceived SIGINT (Ctrl+C)\n");
  20. signal_received = 1;
  21. }
  22. }
  23. void print_signal_mask(const char *msg, const sigset_t *mask) {
  24. printf("%s: ", msg);
  25. for (int signo = 1; signo < NSIG; ++signo) {
  26. if (sigismember(mask, signo)) {
  27. printf("%d ", signo);
  28. }
  29. }
  30. printf("\n");
  31. }
  32. // 获取当前屏蔽字的函数
  33. unsigned long get_signal_mask() {
  34. sigset_t sigset;
  35. if (sigprocmask(SIG_BLOCK, NULL, &sigset) == -1) {
  36. perror("sigprocmask");
  37. return -1; // 返回错误标记
  38. }
  39. // 将信号集编码为位掩码
  40. unsigned long mask = 0;
  41. for (int i = 1; i < NSIG; i++) {
  42. if (sigismember(&sigset, i)) {
  43. mask |= 1UL << (i - 1);
  44. }
  45. }
  46. return mask;
  47. }
  48. int main() {
  49. sigset_t new_mask, old_mask;
  50. sigemptyset(&old_mask);
  51. // 注册 SIGINT 的信号处理函数
  52. if (signal(SIGINT, signal_handler) == SIG_ERR) {
  53. perror("signal");
  54. exit(EXIT_FAILURE);
  55. }
  56. printf("Signal handler for SIGINT is registered.\n");
  57. signal_received = 0;
  58. kill(getpid(), SIGINT);
  59. sleep(5);
  60. TEST_ASSERT(signal_received, 1, "SIGINT was received", "SIGINT was not received");
  61. signal_received = 0;
  62. // 初始化新的信号集,并将 SIGINT 添加到其中
  63. sigemptyset(&new_mask);
  64. sigaddset(&new_mask, SIGINT);
  65. // 打印 new_mask 的值
  66. print_signal_mask("new_mask", &new_mask);
  67. // 屏蔽 SIGINT
  68. if (sigprocmask(SIG_BLOCK, &new_mask, &old_mask) < 0) {
  69. perror("sigprocmask - SIG_BLOCK");
  70. exit(EXIT_FAILURE);
  71. }
  72. // 打印 old_mask 的值
  73. print_signal_mask("old_mask", &old_mask);
  74. // 检查 SIGINT 是否被屏蔽
  75. unsigned long actual_mask = get_signal_mask();
  76. unsigned long expected_mask = (1UL << (SIGINT - 1));
  77. TEST_ASSERT(actual_mask,
  78. expected_mask,
  79. "Signal mask is as expected",
  80. "Signal mask mismatch");
  81. printf("SIGINT is now blocked.\n");
  82. signal_received = 0;
  83. // 向当前进程发送 SIGINT
  84. kill(getpid(), SIGINT);
  85. // 等待 5 秒,以便测试 SIGINT 是否被屏蔽
  86. sleep(5);
  87. TEST_ASSERT(signal_received, 0, "SIGINT was blocked", "SIGINT was not blocked");
  88. signal_received = 0;
  89. // 恢复原来的信号屏蔽字
  90. if (sigprocmask(SIG_SETMASK, &old_mask, &old_mask) < 0) {
  91. perror("sigprocmask - SIG_SETMASK");
  92. exit(EXIT_FAILURE);
  93. }
  94. print_signal_mask("old_mask returned", &old_mask);
  95. // 检查 SIGINT 是否被解除屏蔽
  96. actual_mask = get_signal_mask();
  97. expected_mask = 0;
  98. TEST_ASSERT(actual_mask,
  99. expected_mask,
  100. "Signal mask is as expected",
  101. "Signal mask mismatch");
  102. printf("SIGINT is now unblocked.\n");
  103. signal_received = 0;
  104. kill(getpid(), SIGINT);
  105. // 等待 5 秒,以便测试 SIGINT 是否解除屏蔽
  106. sleep(5);
  107. TEST_ASSERT(signal_received, 1, "SIGINT was received", "SIGINT was not received");
  108. printf("Exiting program.\n");
  109. return 0;
  110. }