我已经创建了一个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));
}
非阻塞写入工作得非常好。
另一方面,我打开FIFO进行读取,并执行相同的fcntl()使read()非阻塞。
现在,如果有读者附加,我想在写入端进行多个(CPU密集型)计算。
因此,我需要在写入端找到一种方法,以检测是否在其他地方打开了FIFO进行读取。
有没有人有办法实现这个目标?
open("fifo", O_WRONLY | O_NONBLOCK)
呢?如果没有读取器,这将失败并显示ENXIO
。 - melpomene