尝试打开管道进行读取时,open()块 [英] open() blocks when trying to open pipe for reading
问题描述
我有两个进程,一个服务器和一个客户端,应该通过管道(C ++,Linux)进行通信.
服务器使用O_RDONLY
标志打开管道,而客户端使用O_WRONLY
打开管道.
但是,服务器在open
函数处阻塞,而客户端似乎已成功运行(open
函数返回成功,而write
函数也是如此).
I have two processes, a server and a client, that should communicate via pipes (C++, Linux).
The server opens the pipe with the O_RDONLY
flag, and the client with O_WRONLY
.
However, the server blocks at the open
function, while the client seems to run successfully (the open
function returns success and so do the write
functions).
我已阅读到如果设置了O_NONBLOCK
标志,则读取功能将继续,但是如果没有客户端连接,我不希望它继续-可以阻止直到连接了客户端,但就我而言,即使客户端完成运行后,它仍然会被阻止...
I have read that if the O_NONBLOCK
flag is set, the read function will continue, but I don't want it to continue if no client is connected - it is ok to block until a client is connected, but in my case it remains blocked even after the client finishes running...
您能告诉我我做错了什么吗...吗?
Can you plese tell me what I'm doing wrong...?
这是代码:
// Server side
int pipe;
int status, nr_read = 0;
status = mkfifo(FIFO_NAME, 0666);
if (status < 0)
{
// If the file already exists, delete it
unlink(FIFO_NAME);
// Try again
status = mkfifo(FIFO_NAME, 0666);
if(status < 0)
{
printf("mkfifo error: %d\n", status);
return status;
}
}
pipe = open(FIFO_NAME, O_RDONLY);
printf("Never gets here...\n");
[...]
nr_read = read(pipe, my_char_array, CHAR_ARRAY_SIZE);
[...]
close(pipe);
unlink(FIFO_NAME);
它永远不会到达"printf"行...
It never gets to the "printf" line...
// Client side:
int pipe, nr_sent = 0;
int status = 0;
pipe = open(FIFO_NAME, O_WRONLY);
if (pipe < 0)
{
printf("open fifo error: %d\n", status);
return pipe;
}
[...]
nr_sent = write(pipe, my_char_array, CHAR_ARRAY_LENGTH);
[...]
close(pipe);
编辑
我没有提到这条线
#define FIFO_NAME "MYFIFO"
...这就是问题所在: 正如乔迪·哈金斯(Jody Hagins)所说,该路径是一个相对路径,并且进程是从不同的文件夹启动的,他们试图打开不同的文件.
... and here was the problem: as Jody Hagins said, the path being a relative one and the processes being started from different folders, they were trying to open different files.
推荐答案
在写端完成管道之前,读端将不会完成阻塞打开.
The read-side will not complete a blocking open until the write-side has completed the pipe.
如果您不想要此功能,请打开读取端O_NONBLOCK
,然后使用select
确定写入端何时建立了连接并相应地处理输入.
If you do not want this functionality, then open the read-side O_NONBLOCK
, and use select
to determine when the write-side has established a connection and process input accordingly.
编辑
糟糕.只是注意到您说您的服务器即使在运行客户端后也无法完成打开.那很奇怪.我只是剪切/粘贴了您的代码,并在添加了缺少的标头包含和缺少的变量/常量之后,运行了服务器/客户端,并且它们按预期运行.
Oops. Just noticed that you said your server is not completing the open even after running the client. That's strange. I just cut/paste your code and after adding the missing header includes, and the missing variable/constants, ran the server/client and they operated as expected.
服务器等待客户端,当客户端运行时,服务器完成打开并读取数据.
The server waited for the client, and when the client ran, the server completed the open and read the data.
检查文件以确保您具有实际的FIFO.
Check your file to make sure you have an actual FIFO.
您应该看到类似这样的内容:
You should see something like this:
> ls -lat /tmp/FIFO
prw-r--r-- 1 user user 0 2012-09-11 10:22 /tmp/FIFO
> stat /tmp/FIFO
File: `/tmp/FIFO'
Size: 0 Blocks: 0 IO Block: 4096 fifo
Device: 6802h/26626d Inode: 186603 Links: 1
Access: (0644/prw-r--r--) Uid: (10042/ user) Gid: (10042/ user)
Access: 2012-09-11 10:22:48.000000000 -0400
Modify: 2012-09-11 10:22:48.000000000 -0400
Change: 2012-09-11 10:22:48.000000000 -0400
这篇关于尝试打开管道进行读取时,open()块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!