When I try to open a fifo O_WRONLY I get a “No such device or address” error

删除回忆录丶 提交于 2020-01-04 02:50:11

问题


In my code I create a fifo named "my_fifo", if I open it in O_WRONLY | O_NONBLOCK mode, open() returns a -1 and an error number of "No such device or address", on the other hand, if I open the fifo in O_RDONLY | O_NONBLOCK mode, it works perfectly. Why is this happening? Is there something I'm doing wrong?

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{

    char *fifoname = "my_fifo";
    mkfifo(fifoname, 0666);

    int fd;
    if ((fd = open(fifoname, O_WRONLY | O_NONBLOCK)) == -1)
    {
        perror("open pipe");
        exit(EXIT_FAILURE);
    }

    close(fd);

    exit(EXIT_SUCCESS);
}

回答1:


Check out the Linux fifo man page:

A process can open a FIFO in nonblocking mode. In this case, opening for read-only will succeed even if no-one has opened on the write side yet, opening for write-only will fail with ENXIO (no such device or address) unless the other end has already been opened.

If you want non-blocking mode, you need to make sure the reader opens the fifo before the writer.



来源:https://stackoverflow.com/questions/24972911/when-i-try-to-open-a-fifo-o-wronly-i-get-a-no-such-device-or-address-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!