FIFO管道是选择总是可读() [英] FIFO pipe is always readable in select()

查看:293
本文介绍了FIFO管道是选择总是可读()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C伪code:

while (1) {
    fifo = open("fifo", O_RDONLY | O_NONBLOCK);
    fd_set read;
    FD_SET(fifo, &read);
    select(nfds, &read, NULL, NULL, NULL);
}

进程睡眠由触发选择(),直到另一个进程写入 FIFO 。此后,它总是会找到 FIFO 作为一个可读的文件描述符。

The process sleeps as triggered by select() until another process writes into fifo. Afterwards it will always find fifo as a readable file descriptor.

如何避免这种行为(即 FIFO 已经被读取一次之后,如何让被发现为不可读的,直到它到达另一条写?)

How to avoid this behavior (that is, after fifo has been read once, how to make it be found as unreadable until it gets another write?)

推荐答案

您打开的FIFO为只读(O_RDONLY),每当有没有作家的FIFO,读端将收到 EOF

You opened that FIFO as read only (O_RDONLY), whenever there is no writer to the FIFO, the read end will receive an EOF.

选择系统调用将返回关于 EOF 和每次处理都会有一个新的<$ C EOF $ C> EOF 。这是观察到的行为的原因。

Select system call will return on EOF and for every EOF you handle there will be a new EOF. This is the reason for the observed behavior.

要避免这种打开FIFO为读写(O_RDWR)。这可以确保你至少有一位作家在FIFO因此不会有是一个 EOF 并选择将不会返回,除非有人写入到FIFO的结果。

To avoid this open that FIFO for both reading and writing (O_RDWR). This ensures that you have at least one writer on the FIFO thus there wont be an EOF and as a result select won't return unless someone writes to that FIFO.

这篇关于FIFO管道是选择总是可读()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆