英文:
Is there a way to open FIFO(named pipe) in write-only way without blocked
问题
I use FIFO in my server. Due to some limitations, I have to open the FIFO in write-only firstly, then I use fork to create another process to open FIFO in read-only. But I find that, it will be blocked when I open the FIFO in write-only firstly, thus the server can not continue to run to fork.
由于某些限制,我必须首先以只写模式打开 FIFO,然后使用 fork 创建另一个进程以只读模式打开 FIFO。但我发现当我首先以只写模式打开 FIFO 时,它会被阻塞,因此服务器无法继续运行到 fork。
For some reason I can not open FIFO in read-only firstly, Is there a way to open FIFO in write-only without blocked? I have tried open("/tmp/ngfifo", O_WRONLY | O_NONBLOCK) but will return error.
由于某些原因,我不能首先以只读模式打开 FIFO,有没有办法以只写模式打开 FIFO 而不被阻塞?我已经尝试过 open("/tmp/ngfifo", O_WRONLY | O_NONBLOCK),但会返回错误。
The small example is like this :
示例代码如下:
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
int main()
{
    int pipe_fd = open("/tmp/ngfifo", O_WRONLY);  // It will be blocked here, is there a way to open without block.
    if (pipe_fd < 0) {
        fprintf(stderr, "Opened fifo failed %d\n", errno);
        return -1;
    }
    printf("wait..\n");
    if (fork() > 0) {
        printf("begin to write \n");
    } else {
        int read_fd = open("/tmp/ngfifo", O_RDONLY);
        // ...
        printf("begin to read \n");
    }
    return 0;
}
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
int main()
{
    int pipe_fd = open("/tmp/ngfifo", O_WRONLY);  // 会在这里被阻塞,是否有办法在不阻塞的情况下打开。
    if (pipe_fd < 0) {
        fprintf(stderr, "打开FIFO失败 %d\n", errno);
        return -1;
    }
    printf("等待..\n");
    if (fork() > 0) {
        printf("开始写入\n");
    } else {
        int read_fd = open("/tmp/ngfifo", O_RDONLY);
        // ...
        printf("开始读取\n");
    }
    return 0;
}
英文:
I use FIFO in my server. Due to some limitations, I have to open the FIFO in write-only firstly, then I use fork to create another process to open FIFO in read-only. But I find that, it will be blocked when I open the FIFO in write-only firstly, thus the server can not continue to run to fork.
For some reason I can not open FIFO in read-only firstly, Is there a way to open FIFO in write-only without blocked? I have tried open("/tmp/ngfifo", O_WRONLY | O_NONBLOCK) but will return error
The small example is like this :
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
int main()
{
    int pipe_fd = open("/tmp/ngfifo", O_WRONLY);  // It will be blocked here, is there a way to open without block.
    if(pipe_fd < 0) {
        fprintf(stderr, "Opened fifo failed %d\n", errno);
        return -1;
    }
    printf("wait..\n");
    if (fork() > 0) {
        printf("begin to write \n");
    } else {
        int read_fd = open("/tmp/ngfifo", O_RDONLY);
        // ...
        printf("begin to read \n");
    }
    return 0;
}
答案1
得分: 3
通常情况下,打开 FIFO 会阻塞,直到另一端也被打开。
解决方法很简单:在分叉(fork)子进程之后再打开文件。
英文:
From the FIFO manual page:
> Normally, opening the FIFO blocks until the other end is opened also.
The solution is simple: Open the file after you have forked your child process.
答案2
得分: 1
在Linux下,以读写方式打开FIFO将在阻塞模式和非阻塞模式下均成功。POSIX未定义此行为。这可用于在没有可用读取器的情况下打开FIFO进行写入。
因此,在Linux上,写入进程可以调用open("/tmp/ngfifo", O_RDWR | O_NONBLOCK)以非阻塞方式打开管道的写入端,因为它也用于读取。然而,这在其他操作系统上不具备可移植性。
英文:
From the Linux FIFO man page:
> Under Linux, opening a FIFO for read and write will succeed both in blocking and nonblocking mode. POSIX leaves this behavior undefined. This can be used to open a FIFO for writing while there are no readers available.
So on Linux, it is possible for the writing process to call open("/tmp/ngfifo", O_RDWR | O_NONBLOCK) to open the write end of the pipe without blocking because it is also open for reading. However, this is not portable to other operating systems.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论