我一直在阅读有关如何在 Linux 内核中实现管道的信息,并希望验证我的理解。如果我不正确,将选择具有正确解释的答案。
pipe:) 上,旁边是/pipe(2)系统调用pipe(2)shell 用于与|操作员(或从任何其他进程手动)进行管道传输的系统调用在 pipefs 中创建一个新文件,该文件的行为与普通文件非常相似stdout重定向到在 pipefs 中创建的临时文件stdin设置为 pipefs 上的文件这是对管道(例如ls -la | less)如何运作的解释非常正确吗?
我不明白的一件事是像 bash 这样的东西如何设置一个进程stdin或stdout由pipe(2). 我还没有找到任何关于它的信息。
Gre*_*ill 24
到目前为止,您的分析总体上是正确的。shell 可能将进程的 stdin 设置为管道描述符的方式可能是(伪代码):
pipe(p) // create a new pipe with two handles p[0] and p[1]
fork() // spawn a child process
close(p[0]) // close the write end of the pipe in the child
dup2(p[1], 0) // duplicate the pipe descriptor on top of fd 0 (stdin)
close(p[1]) // close the other pipe descriptor
exec() // run a new process with the new descriptors in place
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10664 次 |
| 最近记录: |