简体   繁体   English

尝试打开管道进行读取时打开()块

[英]open() blocks when trying to open pipe for reading

I have two processes, a server and a client, that should communicate via pipes (C++, Linux). 我有两个进程,服务器和客户端,应该通过管道(C ++,Linux)进行通信。 The server opens the pipe with the O_RDONLY flag, and the client with O_WRONLY . 服务器使用O_RDONLY标志打开管道,使用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). 但是,服务器在open函数处阻塞,而客户端似乎成功运行( open函数返回成功, write函数也是如此)。

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... 我已经读过,如果设置了O_NONBLOCK标志,读取功能将继续,但如果没有连接客户端,我不希望它继续 - 在客户端连接之前可以阻止,但在我的情况下它仍然被阻止即使客户完成运行...

Can you plese tell me what I'm doing wrong...? 你能告诉我我做错了什么吗?

Here is the code: 这是代码:

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

It never gets to the "printf" line... 它永远不会到达“printf”线......

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

EDIT 编辑

I didn't mention the line #define FIFO_NAME "MYFIFO" 我没有提到#define FIFO_NAME "MYFIFO"这一行

... 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. ...这就是问题所在:正如Jody Hagins所说,路径是一个相对的路径,而且流程是从不同的文件夹开始的,他们试图打开不同的文件。

The read-side will not complete a blocking open until the write-side has completed the pipe. 在写入端完成管道之前,读取端不会完成阻塞打开。

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. 如果您不想要此功能,请打开读取端O_NONBLOCK ,然后使用select确定写入端何时建立连接并相应地处理输入。

EDIT 编辑

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. 服务器等待客户端,当客户端运行时,服务器完成打开并读取数据。

Check your file to make sure you have an actual FIFO. 检查您的文件以确保您有一个实际的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

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

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