gpt4 book ai didi

c - 当我尝试打开 fifo O_WRONLY 时,出现 "No such device or address"错误

转载 作者:IT王子 更新时间:2023-10-29 01:06:12 27 4
gpt4 key购买 nike

在我的代码中,我创建了一个名为“my_fifo”的 fifo,如果我在 O_WRONLY | 中打开它的话O_NONBLOCK 模式,open() 返回 -1 和错误编号“没有这样的设备或地址”,另一方面,如果我在 O_RDONLY | 打开 fifo | O_NONBLOCK 模式,它工作得很好。为什么会这样?我做错了什么吗?

#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手册页:

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。

关于c - 当我尝试打开 fifo O_WRONLY 时,出现 "No such device or address"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24972911/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com