【问题标题】:Implementation of multiple pipes in CC中多管道的实现
【发布时间】:2012-01-13 09:54:36
【问题描述】:

我正在尝试在我的 shell 中用 C 实现多个管道。我找到了关于这个 website 的教程,我制作的函数基于这个例子。这是函数

void executePipes(cmdLine* command, char* userInput) {
    int numPipes = 2 * countPipes(userInput);
    int status;
    int i = 0, j = 0;
    int pipefds[numPipes];

    for(i = 0; i < (numPipes); i += 2)
        pipe(pipefds + i);

    while(command != NULL) {
        if(fork() == 0){

            if(j != 0){
                dup2(pipefds[j - 2], 0);
            }

            if(command->next != NULL){
                dup2(pipefds[j + 1], 1);
            }    

            for(i = 0; i < (numPipes); i++){
                close(pipefds[i]);
            }
            if( execvp(*command->arguments, command->arguments) < 0 ){
                perror(*command->arguments);
                exit(EXIT_FAILURE);
            }
        }

        else{
                if(command != NULL)
                    command = command->next;

                j += 2;
                for(i = 0; i < (numPipes ); i++){
                   close(pipefds[i]);
                }
               while(waitpid(0,0,0) < 0);
        }
    }

}

在执行它并输入例如ls | grep bin 之类的命令后,shell 只是挂在那里并且不输出任何结果。我确保我关闭了所有管道。但它只是挂在那里。我认为问题出在waitpid。我删除了waitpid,执行后我没有得到任何结果。我做错了什么?谢谢。

添加代码:

void runPipedCommands(cmdLine* command, char* userInput) {
    int numPipes = countPipes(userInput);

    int status;
    int i = 0, j = 0;

    pid_t pid;

    int pipefds[2*numPipes];

    for(i = 0; i < 2*(numPipes); i++){
        if(pipe(pipefds + i*2) < 0) {
            perror("pipe");
            exit(EXIT_FAILURE);
        }
    }

    while(command) {
        pid = fork();
        if(pid == 0) {

            //if not first command
            if(j != 0){
                if(dup2(pipefds[(j-1) * 2], 0) < 0){
                    perror(" dup2");///j-2 0 j+1 1
                    exit(EXIT_FAILURE);
                    //printf("j != 0  dup(pipefd[%d], 0])\n", j-2);
                }
            //if not last command
            if(command->next){
                if(dup2(pipefds[j * 2 + 1], 1) < 0){
                    perror("dup2");
                    exit(EXIT_FAILURE);
                }
            }

            for(i = 0; i < 2*numPipes; i++){
                    close(pipefds[i]);
            }

            if( execvp(*command->arguments, command->arguments) < 0 ){
                    perror(*command->arguments);
                    exit(EXIT_FAILURE);
            }
        } else if(pid < 0){
            perror("error");
            exit(EXIT_FAILURE);
        }

        command = command->next;
        j++;
    }
        for(i = 0; i < 2 * numPipes; i++){
            close(pipefds[i]);
            puts("closed pipe in parent");
        }

        while(waitpid(0,0,0) <= 0);

    }

}

【问题讨论】:

  • 帖子的风格提示:删除注释掉的代码,并删除任何无关的空格。
  • 您能否发布完整的实现代码,说明您需要使用结构命令的原因

标签: c linux shell pipe


【解决方案1】:

我相信这里的问题是您在创建孩子的同一个循环中等待和关闭。在第一次迭代中,子程序将执行(这将破坏子程序,用您的第一个命令覆盖它),然后父程序关闭其所有文件描述符并等待子程序完成,然后再迭代创建下一个子程序.此时,由于父级已关闭其所有管道,因此任何其他子级将无任何可写入或读取的内容。由于您没有检查 dup2 调用是否成功,因此不会引起注意。

如果你想保持相同的循环结构,你需要确保父级只关闭已经使用过的文件描述符,而留下那些没有单独使用的文件描述符。然后,在创建了所有孩子之后,您的父母可以等待。

编辑:我在回答中混淆了父/子,但推理仍然成立:继续分叉的进程再次关闭其所有管道副本,因此之后的任何进程第一个 fork 将没有有效的文件描述符来读取/写入。

伪代码,使用预先创建的管道数组:

/* parent creates all needed pipes at the start */
for( i = 0; i < num-pipes; i++ ){
    if( pipe(pipefds + i*2) < 0 ){
        perror and exit
    }
}

commandc = 0
while( command ){
    pid = fork()
    if( pid == 0 ){
        /* child gets input from the previous command,
            if it's not the first command */
        if( not first command ){
            if( dup2(pipefds[(commandc-1)*2], 0) < ){
                perror and exit
            }
        }
        /* child outputs to next command, if it's not
            the last command */
        if( not last command ){
            if( dup2(pipefds[commandc*2+1], 1) < 0 ){
                perror and exit
            }
        }
        close all pipe-fds
        execvp
        perror and exit
    } else if( pid < 0 ){
        perror and exit
    }
    cmd = cmd->next
    commandc++
}

/* parent closes all of its copies at the end */
for( i = 0; i < 2 * num-pipes; i++ ){
    close( pipefds[i] );
}

在这段代码中,原始父进程为每个命令创建一个子进程,因此可以在整个考验中幸存下来。孩子们检查他们是否应该从上一个命令中获取输入,以及是否应该将输出发送到下一个命令。然后他们关闭所有管道文件描述符的副本,然后执行。在为每个命令创建一个子代之前,父代除了 fork 什么都不做。然后它会关闭所有描述符副本并继续等待。

首先创建您需要的所有管道,然后在循环中管理它们,这很棘手,并且需要一些数组运算。不过,目标看起来像这样:

cmd0    cmd1   cmd2   cmd3   cmd4
   pipe0   pipe1  pipe2  pipe3
   [0,1]   [2,3]  [4,5]  [6,7]

意识到,在任何给定时间,您只需要两组管道(上一个命令的管道和下一个命令的管道)将简化您的代码并使其更加健壮。 Ehemient 给出了这个here 的伪代码。他的代码更简洁,因为父子节点不必进行不必要的循环来关闭不需要的文件描述符,并且父节点可以轻松地在分叉后立即关闭文件描述符的副本。

附带说明:您应该始终检查 pipe、dup2、fork 和 exec 的返回值。

EDIT 2:伪代码中的错字。 OP:num-pipes 将是管道的数量。例如,“ls | grep foo | sort -r”将有 2 个管道。

【讨论】:

  • 感谢您的帮助。我可能听起来很绝望,但你能给我一个可以模仿你认为问题所在的伪代码吗?我尽我所能,但我仍然在某个地方有内存泄漏。我只是不明白问题出在哪里。谢谢
  • 感谢伪代码。在您的代码中,num-pipe 是指管道数吗?因为如果是这样的话,遵循你的伪代码会给我错误的文件描述符。
  • 我尝试实现您的代码。请检查我编辑的问题。我添加了代码。
  • 我实现了它以确认它有效。对不起索引错别字。同样,始终保留两组管道并在循环时旋转它们会更容易。
  • 没问题:)。只是为了确保,在我的情况下,如果我想声明管道文件描述符,它将是 int pipefds[2*numPipes] 对吗?
【解决方案2】:

这是正确的功能代码

void runPipedCommands(cmdLine* command, char* userInput) {
    int numPipes = countPipes(userInput);


    int status;
    int i = 0;
    pid_t pid;

    int pipefds[2*numPipes];

    for(i = 0; i < (numPipes); i++){
        if(pipe(pipefds + i*2) < 0) {
            perror("couldn't pipe");
            exit(EXIT_FAILURE);
        }
    }


    int j = 0;
    while(command) {
        pid = fork();
        if(pid == 0) {

            //if not last command
            if(command->next){
                if(dup2(pipefds[j + 1], 1) < 0){
                    perror("dup2");
                    exit(EXIT_FAILURE);
                }
            }

            //if not first command&& j!= 2*numPipes
            if(j != 0 ){
                if(dup2(pipefds[j-2], 0) < 0){
                    perror(" dup2");///j-2 0 j+1 1
                    exit(EXIT_FAILURE);

                }
            }


            for(i = 0; i < 2*numPipes; i++){
                    close(pipefds[i]);
            }

            if( execvp(*command->arguments, command->arguments) < 0 ){
                    perror(*command->arguments);
                    exit(EXIT_FAILURE);
            }
        } else if(pid < 0){
            perror("error");
            exit(EXIT_FAILURE);
        }

        command = command->next;
        j+=2;
    }
    /**Parent closes the pipes and wait for children*/

    for(i = 0; i < 2 * numPipes; i++){
        close(pipefds[i]);
    }

    for(i = 0; i < numPipes + 1; i++)
        wait(&status);
}

【讨论】:

  • 在这种情况下是否缺少某些东西? if(j != 0 ){
  • @Knu:可能为时已晚,但不,在那种情况下什么都没有。
  • @mkab 你确定吗?您在声明上方的评论中有j!= 2*numPipes
【解决方案3】:

(缩短的)相关代码是:

    if(fork() == 0){
            // do child stuff here
            ....
    }
    else{
            // do parent stuff here
            if(command != NULL)
                command = command->next;

            j += 2;
            for(i = 0; i < (numPipes ); i++){
               close(pipefds[i]);
            }
           while(waitpid(0,0,0) < 0);
    }

这意味着父(控制)进程这样做:

  • 分叉
  • 关闭所有管道
  • 等待子进程
  • 下一个循环/孩子

但它应该是这样的:

  • 分叉
  • 分叉
  • 分叉
  • 关闭所有管道(现在一切都应该被欺骗)
  • 等孩子

【讨论】:

  • 如果我理解你的话,我应该再创建一个fork,然后如果这个fork为0,我检查while(command != NULL)。然后,我将上面编写的整个代码保留在 while 命令中。我说的对吗?
  • 我的答案中的代码不是如何解决问题的建议。它是您的代码的摘要,稍微强调了实际发生的情况。
  • 是的,我知道。它与我的代码相同。我只是想了解您的建议。请检查我编辑的问题。我添加了一些代码。
【解决方案4】:

基本上你想做的是一个递归函数,如果没有其他命令,则子执行第一个命令,父执行第二个命令再次调用该函数。

【讨论】:

  • 发布您的电子邮件地址以供 OP 请求您的代码并不是在这里回答问题的真正方式。不过,请随意扩展您的答案。
【解决方案5】:

基于 Christopher Neylan 提到的在给定时间最多使用两个管道的想法,我将 n 管道的伪代码放在一起。 args 是一个大小为 'args_size' 的字符指针数组,它是一个全局变量。

// MULTIPLE PIPES
// Test case:   char *args[] = {"ls", "-l", "|", "head", "|", "tail", "-4", 
0};// "|", "grep", "Txt", 0};   
enum fileEnd{READ, WRITE};

void multiple pipes( char** args){
pid_t cpid;
// declare pipes
int pipeA[2]
int pipeB[2]
// I have done getNumberofpipes
int numPipes = getNumberOfPipes;
int command_num = numPipes+1;
// holds sub array of args 
// which is a statement to execute
// for example: cmd = {"ls", "-l", NULL}
char** cmd 
// iterate over args
for(i = 0; i < args_size; i++){
  // 
  // strip subarray from main array
  //  cmd 1 | cmd 2 | cmd3 => cmd
  // cmd = {"ls", "-l", NULL}
  //Open/reopen one pipe

  //if i is even open pipeB
    if(i % 2)  pipe(pipeB);
  //if i is odd open pipeA
    else       pipe(pipeA);


  switch(cpid = fork(){
      case -1: error forking
      case 0: // child process
            childprocess(i);
      default: // parent process
           parentprocess(i, cpid);
  }
}
}
// parent pipes must be closed in parent
void parentprocess(int i, pid_t cpid){

   // if first command
   if(i == 0)  
        close(pipeB[WRITE]);

   // if last command close WRITE
   else if (i == numPipes){
       // if i is even close pipeB[WRITE]
       // if i is odd close pipeA[WRITE]
   }

   // otherwise if in middle close READ and WRITE 
   // for appropriate pipes
      // if i is even
      close(pipeA[READ])
      close(pipeB[WRITE])
      // if i is odd
      close(pipeB[READ])
      close(pipeA[WRITE])
   }

   int returnvalue, status;
   waitpid(cpid, returnvalue, status);
}
void childprocess(int i){

    // if in first command
    if(i == 0)
        dup2(pipeB[WRITE], STDOUT_FILENO);
    //if in last command change stdin for
    // the necessary pipe. Don't touch stdout - 
    // stdout goes to shell
    else if( numPipes == i){
        // if i is even
        dup2(pipeB[READ], STDIN_FILENO)
        //if i is odd
        dup2(pipeA[READ], STDIN_FILENO);        
    }
    // otherwise, we are in middle command where
    // both pipes are used.
    else{
       // if i is even
       dup2(pipeA[READ], STDIN_FILENO)
       dupe(pipeB[WRITE], STDOUT_FILENO)
       // if i is odd
       dup2(pipeB[READ], STDIN_FILENO)
       dup2(pipeA[WRITE], STDOUT_FILENO)
    }

    // execute command for this iteration
    // check for errors!!
    // The exec() functions only return if an error has occurred. The return value is -1, and errno is set to indicate the error.
    if(exec(cmd, cmd) < 0)
        printf("Oh dear, something went wrong with read()! %s\n", strerror(errno));
    }   
}

【讨论】:

    【解决方案6】:

    你只需要两个交替的管道,如下所示:

    typedef enum {start, middle, end} pipekind;
    typedef int pipe_io[2];
    pipe_io file, _file;
    pid_t pid;
    bool alternate = false;
    
    #define currentpipe (alternate ? _file : file)
    #define previouspipe (alternate ? file : _file)
    #define nextpipe previouspipe
    
    #define READ 0
    #define WRITE 1
    #define ERROR -1
    #define CHILD 0
    #define PARENT default
    
    //call this function inside a loop
    void execute(char **command, pipekind kind)
    {
        switch (pid = fork())
        {
            case ERROR: perror("fork()"); break;
            case CHILD: 
                    if (*command == NULL) exit(EXIT_SUCCESS);
                    switch (kind)
                    {
                        case start: 
                                    dup2(currentpipe[WRITE], STDOUT_FILENO);
                                    break;
                        case middle: 
                                    dup2(previouspipe[READ], STDIN_FILENO); 
                                    dup2(currentpipe[WRITE], STDOUT_FILENO);
                                    break;
                        case end:
                                    dup2(currentpipe[READ], STDIN_FILENO);
                        default:    break;
                    }
                    execvp(*command, command);
                    perror(*command);
                    _exit(EXIT_FAILURE);
            PARENT:
                    switch (kind)
                    {
                        case start: 
                                    close(currentpipe[WRITE]);
                                    break;
                        case middle:  
                                    close(previouspipe[READ]);
                                    close(currentpipe[WRITE]);
                                    break;
                        case end: 
                                    close(currentpipe[READ]);
                        default:    break;
                    }
                    while(wait(NULL) > 0);
                    getnextpipe(); //not defined in this shorthand example
        }
        alternate = alternate ? false : true;
    }
    

    我会将link 留给需要它的人的完整工作代码。

    【讨论】:

      猜你喜欢
      • 2014-03-21
      • 1970-01-01
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多