gpt4 book ai didi

c - 为什么管道可以使用的进程之间的关系在 Linux API 和 Bash 中不同

转载 作者:可可西里 更新时间:2023-11-01 11:48:28 26 4
gpt4 key购买 nike

我想知道为什么管道可以使用的进程之间的关系在 Linux API 和 Bash 中是不同的。

  • 在 Linux API 中,无名管道 (pipe()) 只能在父子进程之间使用。

  • 在 Bash 中,可以在两个进程之间使用管道,这两个进程有一个 shell 进程作为它们的公共(public)父进程。

Bash 中的管道是根据 Linux API 中的未命名管道实现的吗?谢谢。

来自 APUE 3ed:

15.2 Pipes

Pipes are the oldest form of UNIX System IPC and are provided by allUNIX systems. Pipes have two limitations.

  1. Historically, they have been half duplex (i.e., data flows in only one direction). Some systems now provide full-duplex pipes, but formaximum portability, we should never assume that this is the case.

  2. Pipes can be used only between processes that have a common ancestor. Normally, a pipe is created by a process, thatprocess calls fork, and the pipe is used between the parent and thechild.

We’ll see that FIFOs (Section 15.5) get around thesecond limitation, and that UNIX domain sockets (Section 17.2) getaround both limitations.

...

15.5 FIFOs

FIFOs are sometimes called named pipes. Unnamed pipes can be usedonly between related processes when a common ancestor has created thepipe. With FIFOs, however, unrelated processes can exchange data.

最佳答案

  • In Linux API, unnamed pipes (pipe()) can be used only between parent-child processes.

不是真的。它们可以被任何具有以下任一文件描述符的进程使用:父进程、任何 子进程、任何具有 received them via domain sockets 的进程。等

bash 中的管道只是子对子通信的一个特例。

关于c - 为什么管道可以使用的进程之间的关系在 Linux API 和 Bash 中不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45766856/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com