C语言 在 C 中编码多个管道
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17630247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Coding multiple pipe in C
提问by user2145240
I'm trying to implement a multiple pipe for my shell in C.
我正在尝试在 C 中为我的 shell 实现一个多管道。
All I have is a pipe function that pipe a | b but not a | b | c.
我所拥有的只是一个管道函数,用于管道 a | b 但不是 a | 乙 | C。
int c[2];
int returnv;
pid_t id;
pipe(c);
pid = fork()) == 0
if (pid)
{
dup2(c[1], 0);
close(p[1]);
close(p[1]);
execvp(array(0), array);
}
if ((pid = fork()) == 0)
{
dup2(p[0], 1);
close(p(0));
close(p[0]);
returnv = execvp(array[0], array);
}
close(p[1]);
wait(NULL);
wait(NULL);
wait(NULL);
return returnv;
And this is a second version:
这是第二个版本:
int i = 0;
while (i < x)
{
pipe(c);
if ((pid = fork()) == 0)
{
dup2(t[i], 1);
if (i < 2)
dup2(p[0], 1);
close(p[1]);
r= execvp(cmd[i][0], cmd[i]);
}
wait(NULL);
close(p[0]);
i += 1;
t[i] = p[1];
How can I add the little something that will make this code manage multiple pipe please ? Thanks a lot in advance !
我怎样才能添加一些可以使此代码管理多个管道的小东西?非常感谢!
回答by Alexis
Edit:according to your comment
编辑:根据您的评论
To perform multiples pipes you need to store all your commands somewhere. That's why I used a tab of structure.
要执行多个管道,您需要将所有命令存储在某处。这就是我使用结构标签的原因。
Check this new version maybe easier to understand
检查这个新版本可能更容易理解
So first you need a tab or something to store all your commands:
所以首先你需要一个选项卡或其他东西来存储你的所有命令:
int main()
{
char *ls[] = {"ls", NULL};
char *grep[] = {"grep", "pipe", NULL};
char *wc[] = {"wc", NULL};
char **cmd[] = {ls, grep, wc, NULL};
loop_pipe(cmd);
return (0);
}
Then the function who will run through the tab and launch everything
然后是将运行选项卡并启动所有内容的功能
void loop_pipe(char ***cmd)
{
int p[2];
pid_t pid;
int fd_in = 0;
while (*cmd != NULL)
{
pipe(p);
if ((pid = fork()) == -1)
{
exit(EXIT_FAILURE);
}
else if (pid == 0)
{
dup2(fd_in, 0); //change the input according to the old one
if (*(cmd + 1) != NULL)
dup2(p[1], 1);
close(p[0]);
execvp((*cmd)[0], *cmd);
exit(EXIT_FAILURE);
}
else
{
wait(NULL);
close(p[1]);
fd_in = p[0]; //save the input for the next command
cmd++;
}
}
}
回答by lsk
I will give a working version of the two pipe model and a hint for the three pipe model. Try it out and see if it works. NOTE: If you do not include the proper header files, dup2() will be a nightmare.
我将给出两管模型的工作版本和三管模型的提示。尝试一下,看看它是否有效。注意:如果您没有包含正确的头文件,dup2() 将是一场噩梦。
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int p[2];
int pid;
int r;
main()
{
char *ls_args[] = {"ls", NULL};
char *grep_args[] = {"grep", "pipe", NULL};
pipe(p);
pid = fork();
if (pid != 0) {
// Parent: Output is to child via pipe[1]
// Change stdout to pipe[1]
dup2(p[1], 1);
close(p[0]);
r = execvp("ls", ls_args);
} else {
// Child: Input is from pipe[0] and output is via stdout.
dup2(p[0], 0);
close(p[1]);
r = execvp("grep", grep_args);
close(p[0]);
}
return r;
}
For a|b|c, the hint is to use two pipes i.e. p1[2] and p2[2]. Try this out and let us know how it works.
对于 a|b|c,提示是使用两个管道,即 p1[2] 和 p2[2]。试试这个,让我们知道它是如何工作的。

