C中多管道的实现

新手上路,请多包涵

我正在尝试用 C 在我的 shell 中实现多个管道。我在这个 网站 上找到了一个教程,我制作的功能是基于这个例子。这是功能

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);

    }

}

原文由 mkab 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 655
2 个回答

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

如果要保持相同的循环结构,则需要确保父级仅关闭已使用的文件描述符,而保留那些尚未使用的文件描述符。然后,在创建了所有孩子之后,您的父母可以等待。

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

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

 /* 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 在这里 给出了伪代码。他的代码更简洁,因为父子节点不必进行不必要的循环来关闭不需要的文件描述符,并且父节点可以在分叉后立即轻松地关闭文件描述符的副本。

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

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

原文由 Christopher Neylan 发布,翻译遵循 CC BY-SA 3.0 许可协议

这是正确的功能代码

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

原文由 mkab 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题