使用管道的两个进程的环
我有这个管道和 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,让我们假设您已经从终端启动了这个东西。然后您将:
现在您运行
pipe(fd)
,并且您将:现在您将运行前两个
dup2
和前两个close
s:现在你创建一个新的
管道
:现在你分叉。在子进程中,您调用
dup2(fd[1],1)
并关闭两个fd
(您的源代码在这里不太正确):在父进程中,您调用
dup2(fd[0],0)
,并再次关闭两个fd
:因此,我们让父进程将其stdout写入管道1,子进程将其stdout写入管道1从中读取其标准输入。类似地,我们让父进程从管道 2 读取其 stdin,子进程将其 stdout 写入其中。即,两个进程的环。
我总是被告知这种安排很容易陷入僵局。我不知道更现代的 Unix 是否也是如此,但值得一提。
Ok, let's just pretend you've started this thing from a terminal. Then you have:
Now you run
pipe(fd)
, and you have:Now you run the first two
dup2
s and the first twoclose
s:Now you make a new
pipe
:And now you fork. In the child process you call
dup2(fd[1],1)
and close bothfd
s (your source isn't quite right here):In the parent process you call
dup2(fd[0],0)
, and again close bothfd
s giving it: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.