使用管道的两个进程的环

发布于 2024-11-14 21:12:02 字数 740 浏览 3 评论 0原文

我有这个管道和 dup 使用的例子。它应该创建一个由管道连接的两个进程组成的环。这是代码:

#include <unistd.h>

#define READ 0
#define WRITE 1

main (int argc, char *argv[]) {
 int fd[2];
 pipe(fd); //first call to pipe
 dup2(fd[READ],0);
 dup2(fd[WRITE],1);
 close(fd[READ]);
 close(fd[WRITE]);
 pipe(fd); //second call to pipe

 if (fork()==0) {
    dup2(fd[WRITE],1);
 } else
    dup2(fd[READ],0);

 close(fd[READ]);
 close(fd[WRITE]);
}

从“两个进程的环”中我了解到进程A的输出连接到进程B的输入,进程B的输出连接到进程A的输入

。第一次调用pipe,以及连续两次调用dup2,我认为标准输入和输出被重定向到我的新管道的输入和输出。

然后,第二次调用管道让我感到困惑,因为我认为它覆盖了我以前的 fd 值。此时标准输入和输出是怎么回事?

最后,fork 调用:

子进程是否将标准输出重定向到管道?

父级是否将标准输入重定向到管道?

我在这里看不到戒指。

我希望我能说清楚,因为我真的很困惑。

非常感谢!!

I have this example of pipe and dup use. It is supposed to create a ring of two processes connected by a pipe. Here is the code:

#include <unistd.h>

#define READ 0
#define WRITE 1

main (int argc, char *argv[]) {
 int fd[2];
 pipe(fd); //first call to pipe
 dup2(fd[READ],0);
 dup2(fd[WRITE],1);
 close(fd[READ]);
 close(fd[WRITE]);
 pipe(fd); //second call to pipe

 if (fork()==0) {
    dup2(fd[WRITE],1);
 } else
    dup2(fd[READ],0);

 close(fd[READ]);
 close(fd[WRITE]);
}

From "ring of two processes" I understand that the output of the process A is connected to the input of the process B, and the output of the process B is connected to the input of the process A.

After the first call of pipe, and the two succesive calls of dup2, I think the standard input and output were redirected to the input and output of my new pipe.

Then, it came the second call to pipe which confuses me, since I think it overwrites my previous fd values. At this point what is going on with the standard input and output?

Finally, the fork call:

does the child redirect the standard output to the pipe?

does the parent redirect the standard input to the pipe?

I can't see the ring here.

I hope I made myself clear, since I'm really confused.

Thank you very much!!

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

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

发布评论

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

评论(1

赠意 2024-11-21 21:12:02

好吧,让我们假设您已经从终端启动了这个东西。然后您将:

file 0 (stdin)  = terminal keyboard
file 1 (stdout) = terminal screen
file 2 (stderr) = terminal screen

现在您运行 pipe(fd),并且您将:

file 0-2 = as before
file 3 (fd[0]) = read end of pipe 1
file 4 (fd[1]) = write end of pipe 1

现在您将运行前两个 dup2 和前两个 close s:

file 0 = read end of pipe 1
file 1 = write end of pipe 1
file 2 = still terminal screen
file 3-4 = now closed

现在你创建一个新的管道

file 0-2 as before
file 3 (fd[0]) = read end of pipe 2
file 4 (fd[1]) = write end of pipe 2

现在你分叉。在子进程中,您调用 dup2(fd[1],1) 并关闭两个 fd (您的源代码在这里不太正确):

file 0: read end of pipe 1
file 1: write end of pipe 2
file 2: terminal screen
file 3-4: closed

在父进程中,您调用dup2(fd[0],0),并再次关闭两个fd

file 0: read end of pipe 2
file 1: write end of pipe 1
file 2: terminal screen
file 3-4: closed

因此,我们让父进程将其stdout写入管道1,子进程将其stdout写入管道1从中读取其标准输入。类似地,我们让父进程从管道 2 读取其 stdin,子进程将其 stdout 写入其中。即,两个进程的环。

我总是被告知这种安排很容易陷入僵局。我不知道更现代的 Unix 是否也是如此,但值得一提。

Ok, let's just pretend you've started this thing from a terminal. Then you have:

file 0 (stdin)  = terminal keyboard
file 1 (stdout) = terminal screen
file 2 (stderr) = terminal screen

Now you run pipe(fd), and you have:

file 0-2 = as before
file 3 (fd[0]) = read end of pipe 1
file 4 (fd[1]) = write end of pipe 1

Now you run the first two dup2s and the first two closes:

file 0 = read end of pipe 1
file 1 = write end of pipe 1
file 2 = still terminal screen
file 3-4 = now closed

Now you make a new pipe:

file 0-2 as before
file 3 (fd[0]) = read end of pipe 2
file 4 (fd[1]) = write end of pipe 2

And now you fork. In the child process you call dup2(fd[1],1) and close both fds (your source isn't quite right here):

file 0: read end of pipe 1
file 1: write end of pipe 2
file 2: terminal screen
file 3-4: closed

In the parent process you call dup2(fd[0],0), and again close both fds giving it:

file 0: read end of pipe 2
file 1: write end of pipe 1
file 2: terminal screen
file 3-4: closed

So we have the parent process writing its stdout to pipe 1, which the child reads its stdin from. Similarly, we have the parent process reading its stdin from pipe 2, which the child is writing its stdout to. I.e., a ring of two processes.

I was always taught that this sort of arrangement was prone to deadlock. I don't know if that is true with more modern Unixes, but it is worth a mention.

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