当我尝试打开fifo O_WRONLY时,系统显示“没有此类设备或地址".错误 [英] When I try to open a fifo O_WRONLY I get a "No such device or address" error

查看:75
本文介绍了当我尝试打开fifo O_WRONLY时,系统显示“没有此类设备或地址".错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我创建了一个名为"my_fifo"的fifo,如果我在O_WRONLY | O_NONBLOCK模式下将其打开,则open()返回-1且错误号为无此类设备或地址",如果我在O_RDONLY | O_NONBLOCK模式下打开fifo,则效果很好.为什么会这样呢?我在做错什么吗?

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);
}

推荐答案

查看Linux fifo 手册页:

Check out the Linux fifo man page:

一个进程可以在非阻塞模式下打开FIFO.在这种情况下,打开 即使在写操作中没有打开任何文件,只读文件也将成功 另一面,只能使用 ENXIO 打开只写操作(没有这样的设备 或地址),除非另一端已经打开.

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.

如果要使用非阻塞模式,则需要确保读者在写者之前先打开fifo.

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

这篇关于当我尝试打开fifo O_WRONLY时,系统显示“没有此类设备或地址".错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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