在单个进程中使用命名管道

发布于 2024-08-21 20:53:59 字数 537 浏览 1 评论 0原文

我正在尝试使用命名管道在进程内进行通信。 这是代码

#include <stdio.h>
#include <fcntl.h>
#include <signal.h>

void sigint(int num)
{
    int fd = open("np", O_WRONLY);
    write(fd, "y", 1);
    close(fd);
}

main()
{
    char ch[1];
    int fd;

    mkfifo("np", 0666);

    signal(SIGINT, sigint);

    fd = open("np", O_RDONLY);

    read(fd, ch, 1);

    close(fd);

    printf("%c\n", ch[0]);
    return;
}

我想要的是让 main 阻塞,直到有东西写入管道。 问题是信号处理程序 sigint() 在打开管道后也会阻塞。鉴于管道已经在 main() 之前打开以供读取,是否应该发生这种情况?

I am trying to use a named pipe for communication within a process.
Here is the code

#include <stdio.h>
#include <fcntl.h>
#include <signal.h>

void sigint(int num)
{
    int fd = open("np", O_WRONLY);
    write(fd, "y", 1);
    close(fd);
}

main()
{
    char ch[1];
    int fd;

    mkfifo("np", 0666);

    signal(SIGINT, sigint);

    fd = open("np", O_RDONLY);

    read(fd, ch, 1);

    close(fd);

    printf("%c\n", ch[0]);
    return;
}

What I want is for main to block till something is written to the pipe.
The problem is that the signal handler sigint() also blocks after opening the pipe. Is this supposed to happen given that the pipe is already opened for reading earlier in main() ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨落成白 2024-08-28 20:53:59

您在 open() 中阻塞,打开一个 fifo 来读取块,直到有人打开它进行写入。

并打开一个 fifo 来写入块,直到有人打开它来读取。

信号处理程序与 main() 在同一线程中运行,因此您会遇到死锁。两者都无法打开 fifo。

您可以通过在 strace 下运行程序来检查发生了什么。

You're blocking in open() , opening a fifo for reading blocks until someone opens it for writing.

And opening a fifo for writing blocks until someone opens it for reading.

the signal handler runs in the same thread as your main(), so you'll get a deadlock. Neither will be able to open the fifo.

You can check what's going on by running your program under strace.

像极了他 2024-08-28 20:53:59

从手册页:

打开一个FIFO进行正常读取
阻塞直到其他进程打开
相同的 FIFO 用于写入,反之亦然
反之亦然。

和:

进程可以在以下位置打开 FIFO:
非阻塞模式。在这种情况下,
以只读方式打开将会成功
即使没有人打开写入
一边还;打开只写将
ENXIO 失败(没有这样的设备或
地址)除非另一端有
已经打开了。

Linux下,打开一个FIFO进行读取
并且 write 都会成功
阻塞和非阻塞模式。 POSIX
未定义此行为。这
可用于打开 FIFO 进行写入
虽然没有读者可用。
使用两端的进程
连接以进行通信
对其本身应该非常小心
避免死锁。

From the man page:

Opening a FIFO for reading normally
blocks until some other process opens
the same FIFO for writing, and vice
versa.

and:

A process can open a FIFO in
non-blocking 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.

Under Linux, opening a FIFO for read
and write will succeed both in
blocking and non-blocking mode. POSIX
leaves this behaviour undefined. This
can be used to open a FIFO for writing
while there are no readers available.
A process that uses both ends of the
connection in order to communicate
with itself should be very careful to
avoid deadlocks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文