用C语言编写多管道代码

用C语言编写多管道代码,c,C,我正在尝试用C为我的shell实现一个多管道 我所拥有的只是一个管道函数,管道a | b而不是a | b | 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],

我正在尝试用C为我的shell实现一个多管道

我所拥有的只是一个管道函数,管道a | b而不是a | b | 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;
这是第二个版本:

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];
inti=0;
而(i
我怎样才能添加一些小东西,使这段代码能够管理多个管道呢? 非常感谢!根据您的评论编辑:

要执行多重管道,您需要将所有命令存储在某个位置。 这就是我使用结构选项卡的原因

检查这个新版本可能更容易理解

因此,首先需要一个选项卡或其他东西来存储所有命令:

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);
}
然后函数将运行该选项卡并启动所有内容

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++;
        }
    }
}

我将给出两个管道模型的工作版本和三个管道模型的提示。尝试一下,看看它是否有效。注意:如果没有包含正确的头文件,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;
}
#包括
#包括
#包括
#包括
#包括
int p[2];
int-pid;
INTR;
main()
{
char*ls_args[]={“ls”,NULL};
char*grep_args[]={“grep”,“pipe”,NULL};
管道(p);
pid=fork();
如果(pid!=0){
//父级:通过管道[1]将输出传递给子级
//将标准输出更改为管道[1]
dup2(p[1],1);
接近(p[0]);
r=execvp(“ls”,ls_参数);
}否则{
//子级:输入来自管道[0],输出通过标准输出。
dup2(p[0],0);
关闭(p[1]);
r=execvp(“grep”,grep_args);
接近(p[0]);
}
返回r;
}

对于a | b | c,提示是使用两个管道,即p1[2]和p2[2]。尝试一下,让我们知道它是如何工作的。

实际上您调用fork()两次,而只需要一次。这是因为fork()返回两次:子进程返回0,父进程返回>1(通常是子进程的pid).我不认为你需要所有的代码来完成你需要的。我在这一个上花了太多时间,这是唯一可行的^^我想多管道只需要调用一次execvp,但我无法让它工作:(不要从帖子中删除代码,否则会使邮件无效。)answers@user2145240我已经改变了我的答案,旧的答案有点混乱,因为我用它做了很多其他的事情;@user2145240你能告诉我你怎么称呼它吗,因为我知道它在我的电脑上运行得很好,在ideone@user2145240上也运行得很好。有几件事需要检查:printf(“9——关于rentre ici?”);请不要忘记结尾处的\n,在exec下添加一个perror,以确保在env@user2145240唯一能制造问题的是int i=place,因为你没有在循环中显示任何东西,我们不能确定它是否真的进入了循环。但是我没有主意,如果你能找到解决方案的话让我知道很好,所以我想我的答案是正确的:)(因为在我的例子中,我在结尾处放了null:D)