【发布时间】:2020-07-06 20:44:38
【问题描述】:
如何在读取器进程中知道管道的写入端是否仍处于活动状态(写入器进程没有关闭其描述符或退出)。我的意思是 Posix(C 语言)接口。看起来,如果读取器进程具有描述符 NONBLOCK,则无法通过“读取”调用的效果来猜测。我尝试检查描述符标志,但它们没有改变:
作者:
#include <unistd.h>
int main() {
sleep(2);
//actually doesn't write anything but has
//stdout open and exits before reader does
}
读者:
#include <fcntl.h>
#include <cstdio>
#include <unistd.h>
int main() {
int i = fcntl(0, F_GETFL);
int j = fcntl(0, F_GETFD);
//now the write end is alive
printf("i: %d, j: %d\n", i, j);
sleep(3);
i = fcntl(0, F_GETFL);
j = fcntl(0, F_GETFD);
//now the write end has exited
printf("i: %d, j: %d\n", i, j);
//all flags are all the time 0
}
假设这些程序被某个外部程序连接到管道中(使用“管道”调用)。
【问题讨论】: