main.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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"
  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 ---\n", scenario_desc);
  20. // 设置 O_NONBLOCK 标志
  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 {
  43. perror("Write failed with an unexpected error");
  44. }
  45. } else {
  46. printf("Result: Write succeeded. Bytes written: %zd\n", bytes_written);
  47. }
  48. // 关闭 FIFO 写端
  49. close(fd);
  50. }
  51. void run_tests() {
  52. pid_t reader_pid;
  53. // Case 1: Test with no readers (FIFO never had readers)
  54. test_fifo_write("No readers (FIFO never had readers)", 1);
  55. // Case 2: Test with a reader that disconnects
  56. reader_pid = fork();
  57. if (reader_pid == 0) {
  58. // 子进程充当读端
  59. int reader_fd = open(FIFO_PATH, O_RDONLY);
  60. if (reader_fd == -1) {
  61. perror("Reader failed to open FIFO");
  62. exit(EXIT_FAILURE);
  63. }
  64. sleep(1); // 模拟读端短暂存在
  65. close(reader_fd);
  66. exit(EXIT_SUCCESS);
  67. }
  68. sleep(5); // 确保读端已打开
  69. test_fifo_write("Reader exists but disconnects", 0);
  70. waitpid(reader_pid, NULL, 0); // 等待读端子进程退出
  71. // Case 3: Test with an active reader
  72. reader_pid = fork();
  73. if (reader_pid == 0) {
  74. // 子进程充当读端
  75. int reader_fd = open(FIFO_PATH, O_RDONLY);
  76. if (reader_fd == -1) {
  77. perror("Reader failed to open FIFO");
  78. exit(EXIT_FAILURE);
  79. }
  80. sleep(5); // 保持读端存在
  81. close(reader_fd);
  82. exit(EXIT_SUCCESS);
  83. }
  84. sleep(1); // 确保读端已打开
  85. test_fifo_write("Active reader exists", 0);
  86. waitpid(reader_pid, NULL, 0); // 等待读端子进程退出
  87. }
  88. int main() {
  89. // 设置 SIGPIPE 信号处理
  90. signal(SIGPIPE, sigpipe_handler);
  91. // 创建 FIFO
  92. if (mkfifo(FIFO_PATH, 0666) == -1 && errno != EEXIST) {
  93. perror("mkfifo failed");
  94. exit(EXIT_FAILURE);
  95. }
  96. // 运行测试
  97. run_tests();
  98. // 删除 FIFO
  99. unlink(FIFO_PATH);
  100. printf("\nAll tests completed.\n");
  101. return 0;
  102. }