【发布时间】:2026-01-15 13:15:02
【问题描述】:
我正在尝试构建一个可再发行的二进制文件,以放在只有 glibc 2.3 的旧 NAS 上。所以pipe2() 在该机器上不可用,但我正在尝试构建的代码具有以下行:
if (pipe2(info_pipe, O_CLOEXEC | O_NONBLOCK) < 0)
goto info_pipe_err;
我的理解是pipe2() 存在的原因是通过在打开时使用O_CLOEXEC | O_NONBLOCK 来避免竞争条件,而不是分两步进行。但是在我正在查看的案例中没有线程,所以我想我可以替换为:
if (pipe(info_pipe) < 0)
goto info_pipe_err;
int direction; // 0=READ, 1=WRITE
for (direction = 0; direction < 2; ++direction) {
int oldflags;
oldflags = fcntl(info_pipe[direction], F_GETFL);
if (oldflags < 0)
goto info_pipe_err;
if (fcntl(info_pipe[direction],
F_SETFL, oldflags | O_NONBLOCK | O_CLOEXEC) < 0)
goto info_pipe_err;
}
但它似乎不可互换,因为代码不起作用。为什么不等价?
【问题讨论】:
标签: c sockets pipe nonblocking