main.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include <signal.h>
  7. #include <sys/stat.h>
  8. #include <sys/wait.h>
  9. #define FIFO_PATH "/bin/test_fifo" // 使用 /tmp 目录避免权限问题
  10. // 信号处理函数
  11. void sigpipe_handler(int signo) {
  12. if (signo == SIGPIPE) {
  13. printf("Received SIGPIPE signal. Write operation failed.\n");
  14. }
  15. }
  16. void test_fifo_write(const char *scenario_desc, int nonblocking) {
  17. int fd;
  18. char *data = "Hello, FIFO!";
  19. printf("\n--- Testing: %s (nonblocking=%d) ---\n", scenario_desc, nonblocking);
  20. // 设置写模式和非阻塞模式标志
  21. int flags = O_WRONLY;
  22. if (nonblocking) {
  23. flags |= O_NONBLOCK;
  24. }
  25. // 打开 FIFO 写端
  26. fd = open(FIFO_PATH, flags);
  27. if (fd == -1) {
  28. if (errno == ENXIO) {
  29. printf("Result: Failed to open FIFO for writing (ENXIO: No readers).\n");
  30. } else {
  31. perror("Failed to open FIFO for writing");
  32. }
  33. return;
  34. }
  35. // 写入数据
  36. ssize_t bytes_written = write(fd, data, sizeof(data));
  37. if (bytes_written == -1) {
  38. if (errno == EPIPE) {
  39. printf("Result: Write failed with EPIPE (no readers available).\n");
  40. } else if (errno == ENXIO) {
  41. printf("Result: Write failed with ENXIO (FIFO never had readers).\n");
  42. } else if (errno == EAGAIN) {
  43. printf("Result: Write failed with EAGAIN (nonblocking write, pipe full or no readers).\n");
  44. } else {
  45. perror("Write failed with an unexpected error");
  46. }
  47. } else {
  48. printf("Result: Write succeeded. Bytes written: %zd\n", bytes_written);
  49. }
  50. // 关闭 FIFO 写端
  51. close(fd);
  52. }
  53. void test_case1(int nonblocking) {
  54. // Case 1: No readers (FIFO never had readers)
  55. test_fifo_write("No readers (FIFO never had readers)", nonblocking);
  56. }
  57. void test_case2(int nonblocking) {
  58. pid_t reader_pid;
  59. // Case 2: Reader exists but disconnects
  60. reader_pid = fork();
  61. if (reader_pid == 0) {
  62. // 子进程充当读端
  63. int reader_fd = open(FIFO_PATH, O_RDONLY);
  64. if (reader_fd == -1) {
  65. perror("Reader failed to open FIFO");
  66. exit(EXIT_FAILURE);
  67. }
  68. sleep(1); // 模拟读端短暂存在
  69. close(reader_fd);
  70. exit(EXIT_SUCCESS);
  71. }
  72. sleep(5); // 确保读端已打开
  73. test_fifo_write("Reader exists but disconnects", nonblocking);
  74. waitpid(reader_pid, NULL, 0); // 等待读端子进程退出
  75. }
  76. void test_case3(int nonblocking) {
  77. pid_t reader_pid;
  78. // Case 3: Active reader exists
  79. reader_pid = fork();
  80. if (reader_pid == 0) {
  81. // 子进程充当读端
  82. int reader_fd = open(FIFO_PATH, O_RDONLY);
  83. if (reader_fd == -1) {
  84. perror("Reader failed to open FIFO");
  85. exit(EXIT_FAILURE);
  86. }
  87. sleep(5); // 保持读端存在
  88. close(reader_fd);
  89. exit(EXIT_SUCCESS);
  90. }
  91. sleep(1); // 确保读端已打开
  92. test_fifo_write("Active reader exists", nonblocking);
  93. waitpid(reader_pid, NULL, 0); // 等待读端子进程退出
  94. }
  95. int main() {
  96. // 设置 SIGPIPE 信号处理
  97. signal(SIGPIPE, sigpipe_handler);
  98. // 创建 FIFO
  99. if (mkfifo(FIFO_PATH, 0666) == -1 && errno != EEXIST) {
  100. perror("mkfifo failed");
  101. exit(EXIT_FAILURE);
  102. }
  103. // 测试阻塞模式下的三种情况
  104. printf("========== Testing Blocking Mode ==========\n");
  105. test_case1(0); // 阻塞模式下没有读端
  106. test_case2(0); // 阻塞模式下读端断开
  107. test_case3(0); // 阻塞模式下读端存在
  108. // 测试非阻塞模式下的三种情况
  109. // printf("\n========== Testing Nonblocking Mode ==========\n");
  110. // test_case1(1); // 非阻塞模式下没有读端
  111. // test_case2(1); // 非阻塞模式下读端断开
  112. // test_case3(1); // 非阻塞模式下读端存在
  113. // 删除 FIFO
  114. unlink(FIFO_PATH);
  115. printf("\nAll tests completed.\n");
  116. return 0;
  117. }