简体   繁体   English

Fifo开题C

[英]Fifo opening issue C

I have this code: 我有以下代码:

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

I'm studying the FIFO in C and I tried to create two FIFO but after opening them I get this two errors: 我正在研究C语言中的FIFO,并尝试创建两个FIFO,但是在打开它们后出现了两个错误:

FD1:Device not configured
FD:Device not configured

and I dont understand why. 我不明白为什么。

PS "readLine" and "getStringLenght" are my functions PS“ readLine”和“ getStringLenght”是我的功能

Documentation of fifo(7) states: fifo(7)文档指出:

A process can open a FIFO in nonblocking mode. 进程可以非阻塞模式打开FIFO。 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. 在这种情况下,即使未在写端打开任何人,只读打开也将成功,除非ENXIO(没有这样的设备或地址)打开,否则只写打开将失败,除非另一端已经打开。

That's exactly what you do. 那就是你要做的。 You open the pipe for write first but there's not a read end opened yet. 您先打开管道以进行写入,但尚未打开读取端。 If you swap the read/write calls to open() you should be fine. 如果将读写调用交换为open() ,则应该没问题。

I believe you are actually checking the return codes of open() before calling perror(). 我相信您实际上是在调用perror()之前检查open()的返回码。 In all cases, you should set errno to 0 before calling a library function (include <errno.h> ) as errno might be set to some other value by the some other library/system call. 在所有情况下,您都应在调用库函数(包括<errno.h> )之前将errno0 ,因为其他一些库/系统调用可能会将errno设置为其他值。

Looking at your code I really don't see anything, that tells me there's an error here somewhere. 查看您的代码,我真的什么都没有看到,这告诉我这里某处有错误。 As for the printed messages you get, you see, first you have to figure if an error is really there. 至于您得到的打印消息,首先,您必须确定是否确实存在错误。 For that you have to check return values of open() . 为此,您必须检查open()返回值。 And only if return values indicate error, then perror() makes sense. 而且只有返回值指示错误时, perror()才有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM