|
@@ -7,7 +7,7 @@
|
|
|
#include <sys/stat.h>
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
-#define FIFO_PATH "/bin/test_fifo"
|
|
|
+#define FIFO_PATH "/bin/test_fifo"
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
-
|
|
|
+
|
|
|
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) {
|
|
|
+
|
|
|
+ test_fifo_write("No readers (FIFO never had readers)", nonblocking);
|
|
|
+}
|
|
|
|
|
|
-
|
|
|
- test_fifo_write("No readers (FIFO never had readers)", 1);
|
|
|
+void test_case2(int nonblocking) {
|
|
|
+ pid_t reader_pid;
|
|
|
|
|
|
-
|
|
|
+
|
|
|
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;
|
|
|
|
|
|
-
|
|
|
+
|
|
|
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);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
unlink(FIFO_PATH);
|