From d96a0d2541b9815fae7d443001925e79f0a8e76e Mon Sep 17 00:00:00 2001 From: xiaolin2004 <1553367438@qq.com> Date: Wed, 27 Nov 2024 13:32:21 +0800 Subject: [PATCH] =?UTF-8?q?feat(test=5Ffifo=5Fwrite):=20=E5=A2=9E=E5=BC=BA?= =?UTF-8?q?FIFO=E5=86=99=E5=85=A5=E6=B5=8B=E8=AF=95=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E9=9D=9E=E9=98=BB=E5=A1=9E=E6=A8=A1=E5=BC=8F=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=92=8C=E5=A4=9A=E4=B8=AA=E6=B5=8B=E8=AF=95=E5=9C=BA?= =?UTF-8?q?=E6=99=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- user/apps/test_fifo_write/main.c | 43 ++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/user/apps/test_fifo_write/main.c b/user/apps/test_fifo_write/main.c index 1aa068e3a..2a3e32ff1 100644 --- a/user/apps/test_fifo_write/main.c +++ b/user/apps/test_fifo_write/main.c @@ -7,7 +7,7 @@ #include #include -#define FIFO_PATH "/bin/test_fifo" +#define FIFO_PATH "/bin/test_fifo" // 使用 /tmp 目录避免权限问题 // 信号处理函数 void sigpipe_handler(int signo) { @@ -19,9 +19,9 @@ void sigpipe_handler(int signo) { void test_fifo_write(const char *scenario_desc, int nonblocking) { int fd; char *data = "Hello, FIFO!"; - printf("\n--- Testing: %s ---\n", scenario_desc); + printf("\n--- Testing: %s (nonblocking=%d) ---\n", scenario_desc, nonblocking); - // 设置 O_NONBLOCK 标志 + // 设置写模式和非阻塞模式标志 int flags = O_WRONLY; if (nonblocking) { flags |= O_NONBLOCK; @@ -45,6 +45,8 @@ void test_fifo_write(const char *scenario_desc, int nonblocking) { printf("Result: Write failed with EPIPE (no readers available).\n"); } else if (errno == ENXIO) { printf("Result: Write failed with ENXIO (FIFO never had readers).\n"); + } else if (errno == EAGAIN) { + printf("Result: Write failed with EAGAIN (nonblocking write, pipe full or no readers).\n"); } else { perror("Write failed with an unexpected error"); } @@ -56,13 +58,15 @@ void test_fifo_write(const char *scenario_desc, int nonblocking) { close(fd); } -void run_tests() { - pid_t reader_pid; +void test_case1(int nonblocking) { + // Case 1: No readers (FIFO never had readers) + test_fifo_write("No readers (FIFO never had readers)", nonblocking); +} - // Case 1: Test with no readers (FIFO never had readers) - test_fifo_write("No readers (FIFO never had readers)", 1); +void test_case2(int nonblocking) { + pid_t reader_pid; - // Case 2: Test with a reader that disconnects + // Case 2: Reader exists but disconnects reader_pid = fork(); if (reader_pid == 0) { // 子进程充当读端 @@ -77,10 +81,14 @@ void run_tests() { } sleep(5); // 确保读端已打开 - test_fifo_write("Reader exists but disconnects", 0); + test_fifo_write("Reader exists but disconnects", nonblocking); waitpid(reader_pid, NULL, 0); // 等待读端子进程退出 +} + +void test_case3(int nonblocking) { + pid_t reader_pid; - // Case 3: Test with an active reader + // Case 3: Active reader exists reader_pid = fork(); if (reader_pid == 0) { // 子进程充当读端 @@ -95,7 +103,7 @@ void run_tests() { } sleep(1); // 确保读端已打开 - test_fifo_write("Active reader exists", 0); + test_fifo_write("Active reader exists", nonblocking); waitpid(reader_pid, NULL, 0); // 等待读端子进程退出 } @@ -109,8 +117,17 @@ int main() { exit(EXIT_FAILURE); } - // 运行测试 - run_tests(); + // 测试阻塞模式下的三种情况 + printf("========== Testing Blocking Mode ==========\n"); + test_case1(0); // 阻塞模式下没有读端 + test_case2(0); // 阻塞模式下读端断开 + test_case3(0); // 阻塞模式下读端存在 + + // 测试非阻塞模式下的三种情况 + // printf("\n========== Testing Nonblocking Mode ==========\n"); + // test_case1(1); // 非阻塞模式下没有读端 + // test_case2(1); // 非阻塞模式下读端断开 + // test_case3(1); // 非阻塞模式下读端存在 // 删除 FIFO unlink(FIFO_PATH);