Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在shell中使用管道连接n个命令?_C_Shell_Posix_Pipe - Fatal编程技术网

C 在shell中使用管道连接n个命令?

C 在shell中使用管道连接n个命令?,c,shell,posix,pipe,C,Shell,Posix,Pipe,我正在尝试用C实现一个shell。我可以用一个简单的execvp()来执行简单的命令,但其中一个要求是管理这样的命令:“ls-l | head | tail-4”带有一个“for”循环,只有一个“pipe()”语句重定向stdin和stdout。几天后我有点迷路了 N=简单命令的数量(示例中为3个:ls、head、tail) commands=包含命令的结构列表,如下所示: commands[0].argv[0]: ls commands[0].argv[1]: -l commands[1].a

我正在尝试用C实现一个shell。我可以用一个简单的execvp()来执行简单的命令,但其中一个要求是管理这样的命令:“ls-l | head | tail-4”带有一个“for”循环,只有一个“pipe()”语句重定向stdin和stdout。几天后我有点迷路了

N=简单命令的数量(示例中为3个:ls、head、tail) commands=包含命令的结构列表,如下所示:

commands[0].argv[0]: ls
commands[0].argv[1]: -l
commands[1].argv[0]: head
commands[2].argv[0]: tail
commands[2].argv[1]: -4
所以,我做了for循环,并开始重定向stdin和stdout,以便用管道连接所有命令,但是…我不知道为什么它不起作用

for (i=0; i < n; i++){

pipe(pipe);
if(fork()==0){  // CHILD

    close(pipe[0]);
    close(1);
    dup(pipe[1]);
    close(pipe[1]);

    execvp(commands[i].argv[0], &commands[i].argv[0]);
    perror("ERROR: ");
    exit(-1);

}else{      // FATHER

    close(pipe[1]);
    close(0);
    dup(pipe[0]);
    close(pipe[0]);

}
}
(i=0;i{ 管道; 如果(fork()==0){//CHILD 关闭(管道[0]); 关闭(1); dup(管道[1]); 关闭(管道[1]); execvp(命令[i].argv[0],&命令[i].argv[0]); 佩罗尔(“错误:”); 出口(-1); }否则{//父亲 关闭(管道[1]); 关闭(0); dup(管道[0]); 关闭(管道[0]); } } 我要创建的是一系列子进程:

[ls-l]----管道-->[头部]----管道-->[尾部-4]

所有这些进程都有一个根(运行我的shell的进程),所以,第一个父亲也是shell进程的孩子,我已经有点累了,这里有人能帮我吗

我甚至不确定孩子们是否应该是执行命令的人


谢谢大家

首先,您过早地关闭管道。只关闭当前进程中不需要的结尾,并记住在子进程中关闭stdin/stdout

其次,您需要记住上一个命令中的fd。因此,对于两个进程,这看起来像:

int pipe[2];
pipe(pipe);
if ( fork() == 0 ) {
     /* Redirect output of process into pipe */
     close(stdout);
     close(pipe[0]);
     dup2( pipe[1], stdout );
     execvp(commands[0].argv[0], &commands[0].argv[0]);
} 
if ( fork() == 0 ) {
     /* Redirect input of process out of pipe */
     close(stdin);
     close(pipe[1]);
     dup2( pipe[0], stdin );
     execvp(commands[1].argv[0], &commands[1].argv[0]);
}
/* Main process */
close( pipe[0] );
close( pipe[1] );
waitpid();

现在,您的工作是将错误处理添加到该文件中,并为n个进程生成n-1个管道。第一个fork()块中的代码需要为进程1..n-1的相应管道运行,第二个fork()块中的代码需要为进程2..n运行

这里并不复杂,只要记住最后一个命令应该输出到原始进程的文件描述符1,第一个命令应该从原始进程文件描述符0读取。您只需按顺序生成进程,并携带上一个
管道
调用的输入端

因此,以下是类型:

#include <unistd.h>

struct command
{
  const char **argv;
};
下面是主要的fork例程:

int
fork_pipes (int n, struct command *cmd)
{
  int i;
  pid_t pid;
  int in, fd [2];

  /* The first process should get its input from the original file descriptor 0.  */
  in = 0;

  /* Note the loop bound, we spawn here all, but the last stage of the pipeline.  */
  for (i = 0; i < n - 1; ++i)
    {
      pipe (fd);

      /* f [1] is the write end of the pipe, we carry `in` from the prev iteration.  */
      spawn_proc (in, fd [1], cmd + i);

      /* No need for the write end of the pipe, the child will write here.  */
      close (fd [1]);

      /* Keep the read end of the pipe, the next child will read from there.  */
      in = fd [0];
    }

  /* Last stage of the pipeline - set stdin be the read end of the previous pipe
     and output to the original file descriptor 1. */  
  if (in != 0)
    dup2 (in, 0);

  /* Execute the last stage with the current process. */
  return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);
}

似乎有效。:)

这是家庭作业吗?如果不是-只需使用适当的参数运行
/bin/sh
。为什么要重新发明轮子?这只是3页长的自愿实践的要求之一。不完全是家庭作业,但我想知道怎么做,或者至少得到一些线索。这里有很多关于S.O.的好帖子,涵盖了你需要掌握这个主题的背景材料。祝你好运。@user1031296你能发布完整的代码吗?非常有趣,似乎有用。是时候把代码弄乱一点了:PThis工作得很好,但在一个例子中,我试图重定向管道链的输出,它抛出了一个奇怪的错误。我的意思是像在“ls-l | head>文件”中一样,@user1031296对我有效,const char*ls[]={“ls”,“-l”,0};const char*head[]={“head”,0};结构命令cmd[]={{ls},{head};返回叉管(2个,cmd);未使用的
fd[0]
在子进程中未关闭;除最后一个管道外,应在父管道中关闭两个管道末端。父管道应等待子管道输出,对吗?
int
fork_pipes (int n, struct command *cmd)
{
  int i;
  pid_t pid;
  int in, fd [2];

  /* The first process should get its input from the original file descriptor 0.  */
  in = 0;

  /* Note the loop bound, we spawn here all, but the last stage of the pipeline.  */
  for (i = 0; i < n - 1; ++i)
    {
      pipe (fd);

      /* f [1] is the write end of the pipe, we carry `in` from the prev iteration.  */
      spawn_proc (in, fd [1], cmd + i);

      /* No need for the write end of the pipe, the child will write here.  */
      close (fd [1]);

      /* Keep the read end of the pipe, the next child will read from there.  */
      in = fd [0];
    }

  /* Last stage of the pipeline - set stdin be the read end of the previous pipe
     and output to the original file descriptor 1. */  
  if (in != 0)
    dup2 (in, 0);

  /* Execute the last stage with the current process. */
  return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);
}
int
main ()
{
  const char *ls[] = { "ls", "-l", 0 };
  const char *awk[] = { "awk", "{print $1}", 0 };
  const char *sort[] = { "sort", 0 };
  const char *uniq[] = { "uniq", 0 };

  struct command cmd [] = { {ls}, {awk}, {sort}, {uniq} };

  return fork_pipes (4, cmd);
}