进程组内的管道化

发布于 2024-12-11 09:47:38 字数 869 浏览 0 评论 0原文

我目前正在阅读 Stevens/Rago 的《Unix 环境中的高级编程》。

在本书的进程组部分,作者讨论了 shell 通常如何利用进程组进行管道传输。

例如,下面显示的参数可能是由 shell 命令的形式:

proc1 | proc2 &过程3 |过程4 |过程5

许多其他资源也讨论了进程组和管道之间的关系。然而,我找不到的一件事是如何实现其管道部分的解释。

我知道在像 Boune-again shell (BASH) 这样的 posix/unix shell 中,管道中的进程是并行执行的——也就是说,在前面显示 proc3 | 的示例中。过程4 | proc5,这三个进程都是同时执行的。 proc4 的 stdin 连接到 proc3 的 stdin。 (我还知道 MS-DOS 使用临时文件并且不并行执行管道,但我们暂时忽略它)。

因此,我将 proc3、proc4、proc5 全部放在一个进程组中。极好的。这实际上如何帮助在它们之间创建管道?

据我所知,我需要执行以下操作才能在我构建的 shell 中启用管道:

  1. 创建 N-1 个管道,其中 N 是进程中的进程数 管道语句 fork() shell 进程 N 次
  2. 在每个 fork 进程中,我需要使用 dup2 来正确设置 共享管道
  3. 然后,在所有分叉进程都确认它们之后 已经全部完成了管道的设置(可能通过某些 IPC 通过 共享内存空间),然后每个都可以运行 exec() 并实际启动 他们各自的流程。

然而,我一直在阅读的所有文本都表现得好像进程组提供了一些神奇的功能来创建这些管道——或者它们只是忽略了我上面概述的过程。

任何意见或建议总是值得赞赏。

I'm currently reading Advanced Programming in the Unix Environment by Stevens/Rago.

Within the process group section of the book, the author discusses how process groups are utilized commonly by shells for pipelining.

For example, the argument shown below could have been generated by
shell commands of the form:

proc1 | proc2 & proc3 | proc4 | proc5

Lots of other resources also discuss the relationship between process groups and pipelining. However, the one thing which I cannot find is an explanation of how the pipelining portion of this is implemented.

I know that in posix/unix shells like Boune-again shell (BASH), processes in a pipeline are executed in parallel -- that is, in the previous example showing proc3 | proc4 | proc5, all three of these processes are simultaneously executed. The stdin of proc4 is connected to the stdin of proc3. (I also know that MS-DOS used temporary files and did not execute pipes in parallel, but let's ignore that for the moment).

So, I've got proc3, proc4, proc5 all in a process group. Fantastic. How does this actually help with creating the pipelines between them?

As far as I can tell, I need to do the following to enable pipelining in a shell which I build:

  1. Create N-1 pipelines, where N is the number of processes in the
    pipelined statement fork() the shell process N times
  2. In each forked process, I need to use dup2 to properly set up the
    shared pipelines
  3. Then, after all of the forked processes have confirmation that they
    have all finalized setting up their pipes (likely via some IPC via a
    shared memory space), each can then run exec() and actually launch
    their respective processes.

However, all of the texts which I keep reading act as if process groups provide some magical functionality to create these pipelines -- or they are simply neglecting to mention the procedure I outlined above.

Any comments or advice is always appreciated.

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

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

发布评论

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

评论(1

牵你的手,一向走下去 2024-12-18 09:47:38

进程组不会神奇地提供管道。您确实需要按照您的描述设置管道(除了您不需要在步骤 3 中需要任何类型的确认 - 每个进程只是开始使用其管道,并且如果另一端没有使用管道)尚未设置,它会简单地阻塞,直到它完成为止)。

进程组提供的“魔力”在于信号处理和共享终端。

Process groups don't magically provide the pipes. You do need to set the pipes up as you describe (except that you don't need any kind of confirmations in step 3 - each processes just starts using its pipes, and if the other end isn't set up yet, it will simply block until it is).

The "magic" that process groups provide is around the handling of signals and sharing a terminal.

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