管道的哪一端用于输入,哪一端用于输出?

发布于 2024-12-11 14:19:02 字数 955 浏览 0 评论 0原文

最近我开始怀疑我错误地使用了管道的末端:

从手册页:

pipe() 创建一个管道.. ..pipefd[0] 指的是管道的读取端 管道。 pipelinefd[1]指的是管道的写端。

所以在我看来,我是这样的:

                .---------------------------.
               /                            /\
              | pipedfd[0]       pipedfd[1]|  |
process1 ---> |                            | -----> process2
              | input                output|  |
               \____________________________\/

但是,我在这里的代码和工作表明不然:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
  int pipedfd[2];
  char buf[30];

  pipe(pipedfd);
  printf("writing to file descriptor #%d\n", pipedfd[1]);
  write(pipedfd[1], "test", 5);
  printf("reading from file descriptor #%d\n", pipedfd[0]);
  read(pipedfd[0], buf, 5);
  printf("read \"%s\"\n", buf);

  return 0;
}

即它写入管道的输出(?)并从管道的输入(?)读取?

Recently I started suspecting that I use the ends of the pipes wrongly:

From the man pages:

pipe() creates a pipe.. ..pipefd[0] refers to the read end of the
pipe. pipefd[1] refers to the write end of the pipe.

So in my mind I had it like this:

                .---------------------------.
               /                            /\
              | pipedfd[0]       pipedfd[1]|  |
process1 ---> |                            | -----> process2
              | input                output|  |
               \____________________________\/

However the code that I have here and works suggests otherwise:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
  int pipedfd[2];
  char buf[30];

  pipe(pipedfd);
  printf("writing to file descriptor #%d\n", pipedfd[1]);
  write(pipedfd[1], "test", 5);
  printf("reading from file descriptor #%d\n", pipedfd[0]);
  read(pipedfd[0], buf, 5);
  printf("read \"%s\"\n", buf);

  return 0;
}

Namely it writes to the output(?) of the pipe and reads from the input(?) of the pipe?

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

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

发布评论

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

评论(2

兮颜 2024-12-18 14:19:02

简而言之,交换图表中的数字 01,您就得到了我将在下面描述的内容。

来自 Mac OS X 手册页:

pipe() 函数创建一个管道(允许单向数据流的对象)并分配一对文件描述符。第一个描述符连接到读取
管道末端;第二个连接到写入端。

写入fildes[1]的数据出现在fildes[0]上(即可以从中读取)。这允许一个程序的输出发送到另一个程序:源的标准输出
put 设置为管道的写入端;接收器的标准输入设置为管道的读取端。管道本身持续存在,直到所有与其相关的
描述符已关闭。

我将描述它的常用方式,这可能会澄清它。想象一下,您有一个进程并想要生成一个子进程,您想要向其发送命令。

  • 首先,调用 pipe 并获取两个文件描述符。
  • 然后调用fork来创建子进程。
    • 在子进程中,关闭写入文件描述符 (fd[1]) 并保持读取打开。
    • 在父级中,您执行相反的操作:关闭读取 (fd[0]) 文件描述符并保留写入 文件描述符打开。
  • 现在,父级可以写入管道的“他的”部分 (fd[1]),而子级可以读取另一部分 (fd[0])。

关闭不是必需的,但通常会完成。如果您需要双向通信,您要么需要第二组文件描述符以及第二次对管道的调用,要么使用双向通道,例如 Unix 域套接字或命名管道。

In a nutshell, swap the numbers 0 and 1 in your diagram and you got what I'll describe below.

From the Mac OS X man page:

The pipe() function creates a pipe (an object that allows unidirectional data flow) and allocates a pair of file descriptors. The first descriptor connects to the read
end of the pipe; the second connects to the write end.

Data written to fildes[1] appears on (i.e., can be read from) fildes[0]. This allows the output of one program to be sent to another program: the source's standard out-
put is set up to be the write end of the pipe; the sink's standard input is set up to be the read end of the pipe. The pipe itself persists until all of its associated
descriptors are closed.

I'll describe how it's often used, that might clear it up. Imagine you have a process and want to spawn a child, to which you want to send commands.

  • First, you call pipe and get the two file descriptors.
  • Then you call fork to create the child.
    • In the child, you close the writing file descriptor (fd[1]) and leave the reading one open.
    • In the parent, you do the reverse: you close the reading (fd[0]) file descriptor and leave the writing one open.
  • Now the parent can write into "his" part of the pipe (fd[1]) and the child can read on the other (fd[0]).

The closing is not necessary but is usually done. If you need two-way communication you either need a second set of file descriptors plus a second call to pipe, or you use a two way channel like Unix domain sockets or a named pipe.

醉生梦死 2024-12-18 14:19:02

pipe 的 Linux 手册页消除了歧义,如下所示:

写入管道写端的数据被内核缓冲
直到从管道的读取端读取。

也就是说,您从 fd[0]读取写入fd[1]

The Linux man page for pipe disambiguates this as follows:

Data written to the write end of the pipe is buffered by the kernel
until it is read from the read end of the pipe.

That is, you read from fd[0] and write to fd[1]

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