小编典典

用C实现多个管道

linux

我试图在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);

    }

}

阅读 502

收藏
2020-06-02

共1个答案

小编典典

我认为这里的问题是,您的等待和结账在创建子进程的同一循环内。在第一次迭代中,子进程将执行(将破坏子程序,并用您的第一个命令将其覆盖),然后父进程关闭其所有文件描述符,并等待子进程完成后再创建下一个子进程。到那时,由于父级关闭了所有管道,因此任何其他子级都将没有写入或读取的内容。由于您没有检查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] );
}

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

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

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

意识到在任何给定时间,您只需要两套管道(上一条命令的管道和下一条命令的管道)将简化您的代码并使它更加健壮。Ephemient给出了伪代码这一这里。他的代码更加简洁,因为父级和子级不必执行不必要的循环来关闭不需要的文件描述符,并且父级可以在派生之后立即关闭其文件描述符的副本。

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

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

2020-06-02