管道可以用来连接同一进程的2个文件描述符吗?
Can pipe be used to connect 2 file descriptors of the same process?
这是我的项目声明,但我似乎不太明白什么将标准输出链接到管道的输入端(读取端)。我不是在寻求解决方案,但我对问题的含义感到困惑
Project Statement
项目要求
本项目模拟Unix管道命令。 parent 进程分叉一个 child 和
两个进程通过内核管道连接。
parent 进程接受两个 command-line 参数,它们是两个独立的
可执行程序:p1和p2。 parent 打开一个内核管道并派生一个 child 进程。这
child 继承了 parent 的开放管道。
parent 进程将其标准输出链接到管道的输入端并关闭
输出端,然后用p1替换自己。 child 流程链接是输出的标准输入
管道的末端并关闭输入端,然后 child 将自身替换为 p2.
项目助手
在编写这个程序的过程中,您将学习和使用系统调用pipe、dup2 和execlp。
管道是一种非常简单的 inter-process 通信设计,所有 Unix/Linux 都支持它
分布。每个管道都被编码为两个文件描述符 (int fd[2]) 的整数数组。 fd[0] 是
管道的输入端,fd1是管道的输出端。一个child进程继承open
来自其 parent 的管道。谨慎的做法是立即关闭进程中未使用的管道端。
有人能解释一下这一行的意思吗”parent进程将它的标准输出链接到管道的输入端并关闭
输出端,据我所知我们无法从STDOUT_FILENO读取所以调用dup2没有任何意义。
linked 问题图片说(material 在编写答案时添加到问题中 — 也将它留在这里,因为我可以留下一些 [sic] 评论):
This project simulates Unix pipe command. …
The parent process takes in two command-line arguments, which are two independent executable programs: p1 and p2. The parent open [sic] a kernel pipe and forks child process. The child inherits the open pipe from parent.
The parent process links its standard output to the input end of the pipe and closes the output end, then it replaces itself with p1. The child process links is [sic] standard input to the output end of the pipe and closes the input end, then the child replaces itself with p2.
… Each pipe is coded as an integer array of two file descriptors (int fd[2]). fd[0] is the input end of the pipe, and fd[1] is the output end of the pipe. …
定义了术语 'input end' 和 'output end'。
您剩下的是将读取描述符 (fd[0]
) 连接到写入通道 (FILENO_STDOUT
),这 "can be done" 但通常没有意义。频道的方向是传统的,确保程序之间的互通。但是你可以颠覆一组程序的惯例——这使得代码在其他情况下毫无用处。除非专门创建程序 p1
和 p2
以从文件描述符 1 (FILENO_STDOUT
) 读取并写入文件描述符 0 (FILENO_STDIN
),否则由 p1
不会转发给 p2
。
- 练习中有错误。
你可以完全按照要求做,并证明它不起作用。您可以创建一个工作解决方案,以正统的方式做事并证明它确实有效。
我还对术语 'pipe command' 提出异议。在 macOS 上,有一个名为 pipe(8)
:
的实际命令
NAME
pipe - Postfix delivery to external command
SYNOPSIS
pipe [generic Postfix daemon options] command_attributes...
DESCRIPTION
The pipe(8) daemon processes requests from the Postfix queue manager to deliver messages to external commands. This program
expects to be run from the master(8) process manager.
|
表示法不是命令;它是一种inter-process通信方式,IPC。
更好的描述是:
This project simulates the Unix shell pipe notation p1 | p2.
问题标题为'Can pipe be used to connect 2 file descriptors of the same process?'标题中的问题实际上并未在问题的body中提出。标题中问题的答案是"Yes"。实际上,pipe()
系统调用在同一个进程中创建了两个文件描述符,这两个文件描述符是连接的,因此可以从另一个文件描述符读取写入其中一个文件描述符的数据。这就是 pipe()
系统调用的重点。
如果你需要在一对特定的文件描述符之间设置link,你必须调用pipe()
,然后dup2()
两次,然后close()
两次. pipe()
调用returns 2 任意文件描述符编号。 dup2()
调用将所需号码连接到 pipe()
提供的号码; close()
调用断开了 pipe()
返回的号码。唯一需要注意的问题是 pipe()
返回的数字是否与所需数字冲突。然后你必须更加小心——可能用 pipe()
返回的描述符两次调用 dup()
,两次调用 pipe()
返回的描述符上的 close()
,然后两次调用到 dup2()
将描述符从 dup()
映射到所需的数字,然后再调用两次 close()
以关闭来自 dup()
的描述符。 (请注意 dup()
为您选择文件描述符;dup2()
允许您指定返回的文件描述符。)
仍然有可能 pipe()
returns 需要的两个描述符之一;然后 dup()
之一调用 returns 两个描述符中的另一个。只需在序列中添加另一个 dup()
即可。在调用 dup2()
.
之前不要调用 close()
这是我的项目声明,但我似乎不太明白什么将标准输出链接到管道的输入端(读取端)。我不是在寻求解决方案,但我对问题的含义感到困惑
Project Statement
项目要求
本项目模拟Unix管道命令。 parent 进程分叉一个 child 和 两个进程通过内核管道连接。 parent 进程接受两个 command-line 参数,它们是两个独立的 可执行程序:p1和p2。 parent 打开一个内核管道并派生一个 child 进程。这 child 继承了 parent 的开放管道。 parent 进程将其标准输出链接到管道的输入端并关闭 输出端,然后用p1替换自己。 child 流程链接是输出的标准输入 管道的末端并关闭输入端,然后 child 将自身替换为 p2.
项目助手
在编写这个程序的过程中,您将学习和使用系统调用pipe、dup2 和execlp。 管道是一种非常简单的 inter-process 通信设计,所有 Unix/Linux 都支持它 分布。每个管道都被编码为两个文件描述符 (int fd[2]) 的整数数组。 fd[0] 是 管道的输入端,fd1是管道的输出端。一个child进程继承open 来自其 parent 的管道。谨慎的做法是立即关闭进程中未使用的管道端。
有人能解释一下这一行的意思吗”parent进程将它的标准输出链接到管道的输入端并关闭 输出端,据我所知我们无法从STDOUT_FILENO读取所以调用dup2没有任何意义。
linked 问题图片说(material 在编写答案时添加到问题中 — 也将它留在这里,因为我可以留下一些 [sic] 评论):
This project simulates Unix pipe command. …
The parent process takes in two command-line arguments, which are two independent executable programs: p1 and p2. The parent open [sic] a kernel pipe and forks child process. The child inherits the open pipe from parent.
The parent process links its standard output to the input end of the pipe and closes the output end, then it replaces itself with p1. The child process links is [sic] standard input to the output end of the pipe and closes the input end, then the child replaces itself with p2.
… Each pipe is coded as an integer array of two file descriptors (int fd[2]). fd[0] is the input end of the pipe, and fd[1] is the output end of the pipe. …
定义了术语 'input end' 和 'output end'。
您剩下的是将读取描述符 (fd[0]
) 连接到写入通道 (FILENO_STDOUT
),这 "can be done" 但通常没有意义。频道的方向是传统的,确保程序之间的互通。但是你可以颠覆一组程序的惯例——这使得代码在其他情况下毫无用处。除非专门创建程序 p1
和 p2
以从文件描述符 1 (FILENO_STDOUT
) 读取并写入文件描述符 0 (FILENO_STDIN
),否则由 p1
不会转发给 p2
。
- 练习中有错误。
你可以完全按照要求做,并证明它不起作用。您可以创建一个工作解决方案,以正统的方式做事并证明它确实有效。
我还对术语 'pipe command' 提出异议。在 macOS 上,有一个名为 pipe(8)
:
NAME
pipe - Postfix delivery to external commandSYNOPSIS
pipe [generic Postfix daemon options] command_attributes...DESCRIPTION
The pipe(8) daemon processes requests from the Postfix queue manager to deliver messages to external commands. This program expects to be run from the master(8) process manager.
|
表示法不是命令;它是一种inter-process通信方式,IPC。
更好的描述是:
This project simulates the Unix shell pipe notation p1 | p2.
问题标题为'Can pipe be used to connect 2 file descriptors of the same process?'标题中的问题实际上并未在问题的body中提出。标题中问题的答案是"Yes"。实际上,pipe()
系统调用在同一个进程中创建了两个文件描述符,这两个文件描述符是连接的,因此可以从另一个文件描述符读取写入其中一个文件描述符的数据。这就是 pipe()
系统调用的重点。
如果你需要在一对特定的文件描述符之间设置link,你必须调用pipe()
,然后dup2()
两次,然后close()
两次. pipe()
调用returns 2 任意文件描述符编号。 dup2()
调用将所需号码连接到 pipe()
提供的号码; close()
调用断开了 pipe()
返回的号码。唯一需要注意的问题是 pipe()
返回的数字是否与所需数字冲突。然后你必须更加小心——可能用 pipe()
返回的描述符两次调用 dup()
,两次调用 pipe()
返回的描述符上的 close()
,然后两次调用到 dup2()
将描述符从 dup()
映射到所需的数字,然后再调用两次 close()
以关闭来自 dup()
的描述符。 (请注意 dup()
为您选择文件描述符;dup2()
允许您指定返回的文件描述符。)
仍然有可能 pipe()
returns 需要的两个描述符之一;然后 dup()
之一调用 returns 两个描述符中的另一个。只需在序列中添加另一个 dup()
即可。在调用 dup2()
.
close()