先贴上测试代码
pid_t reader_pid;
// Case 2: Reader exists but disconnects
reader_pid = fork();
if (reader_pid == 0) {
// Child process acts as a reader
int reader_fd = open(FIFO_PATH, O_RDONLY);
if (reader_fd == -1) {
perror("Reader failed to open FIFO");
exit(EXIT_FAILURE);
}
sleep(2); // Simulate a brief existence of the reader
close(reader_fd);
exit(EXIT_SUCCESS);
}
sleep(5); // Ensure the reader has opened the FIFO
FifoWriteResult result = test_fifo_write(1, nonblocking);
waitpid(reader_pid, NULL, 0); // Wait for the reader process to exit
测试代码期望子进程可以在父进程醒来之前醒来关闭管道读端。
运行测试会得到以下结果:
- 两者同时醒来(可以通过连续捶打回车键稳定复现)
- 子进程在父进程醒来之后醒来
- 期望情况