我想使用打开管道,popen()并对其具有非阻塞的“读取”访问权限。
popen()
我该如何实现?
(我发现的示例都是阻塞/同步的)
设置如下:
FILE *f = popen("./output", "r"); int d = fileno(f); fcntl(d, F_SETFL, O_NONBLOCK);
现在您可以阅读:
ssize_t r = read(d, buf, count); if (r == -1 && errno == EAGAIN) no data yet else if (r > 0) received data else pipe closed
完成后,清理:
pclose(f);