0

我创建了一个 FIFO,我可以通过这种方式进行非阻塞写入:

// others, searching for a non-blocking FIFO-writer may copy this ;-)
mkfifo("/tmp/myfifo", S_IRWXU);
int fifo_fd = open("/tmp/myfifo", O_RDWR);
fcntl(fifo_fd, F_SETFL, fcntl(fifo_fd, F_GETFL) | O_NONBLOCK);

// and then in a loop:
LOGI("Writing into fifo.");
if (write(fifo_fd, data, count) < 0) {
    LOGE("Failed to write into fifo: %s", strerror(errno));
}

非阻塞写入完美,但由于我的日志记录,我可以看到:

(..)
Writing into fifo.
Writing into fifo.
Writing into fifo.
Failed to write into fifo: Try again    
Writing into fifo.
Failed to write into fifo: Try again    
Writing into fifo.
Failed to write into fifo: Try again

在我创建了一个类似的阅读器之后cat /tmp/myfifo > foo.out,错误就消失了。

我认为 FIFO 像环形缓冲区一样工作,当缓冲区已满时会丢弃第一个写入的字节。但现在我了解到它会阻止/阻止新字节,直到第一个字节被读取。

有谁知道我可以做的简单的其他方式或额外的操作,以便 FIFO 像环形缓冲区一样工作?

4

0 回答 0