gpt4 book ai didi

c - Fifo开口问题C

转载 作者:太空宇宙 更新时间:2023-11-04 00:47:44 26 4
gpt4 key购买 nike

我有这个代码:

mkfifo ("bPipe", 0660); /* Create named pipe */


int fd1 = open ("bPipe", O_WRONLY|O_NONBLOCK); /* Open named pipe for writing */

perror("FD1");

int fd = open ("bPipe", O_RDONLY|O_NONBLOCK); /* Open it for reading */

perror("FD");

char*mex="Hello world\n\0";

write (fd1,mex , getStringLenght(mex)+1);

char* result = readline(fd1);

printf("Rc %s : \n",result);

我正在研究 C 中的 FIFO,我试图创建两个 FIFO,但是在打开它们之后我得到了这两个错误:

FD1:Device not configured
FD:Device not configured

我不明白为什么。

P.S "readLine"和 "getStringLenght"是我的函数

最佳答案

fifo(7) 的文档状态:

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.

这正是您所做的。您首先打开管道进行写入,但尚未打开读取端。如果将读/写调用交换为 open()你应该没事。

我相信您实际上是在调用 perror() 之前检查 open() 的返回码。在所有情况下,您都应该设置 errno0在将库函数(包括 <errno.h> )调用为 errno 之前可能会被其他一些库/系统调用设置为其他一些值。

关于c - Fifo开口问题C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31113672/

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