mka*_*kab 12 c linux shell pipe
我正在尝试在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);
    }
}
Chr*_*lan 15
我相信这里的问题是你在创建孩子的同一循环中等待和关闭.在第一次迭代中,子进程将执行exec(这会破坏子程序,用第一个命令覆盖它),然后父进程关闭所有文件描述符并等待子进程在迭代创建下一个子进程之前完成.此时,由于父级已关闭所有管道,因此任何其他子级都无需写入或读取.由于您没有检查dup2调用是否成功,因此未被注意到.
如果要保持相同的循环结构,则需要确保父级仅关闭已使用的文件描述符,但保留那些尚未单独使用的文件描述符.然后,在创建所有子项后,您的父项可以等待.
编辑:我在我的答案中混淆了父/子,但原因仍然存在:继续进行fork的过程会关闭所有管道副本,因此第一个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] );
}
在此代码中,原始父进程为每个命令创建一个子进程,因此可以在整个考验中幸存下来.孩子们检查他们是否应该从上一个命令获得他们的输入,以及他们是否应该将他们的输出发送到下一个命令.然后他们关闭所有管道文件描述符的副本,然后执行exec.父进程除了为每个命令创建一个子进程之前不进行任何操作.然后它关闭所有描述符的副本,然后继续等待.
首先创建所需的所有管道,然后在循环中管理它们是棘手的,需要一些数组算法.不过,目标看起来像这样:
cmd0    cmd1   cmd2   cmd3   cmd4
   pipe0   pipe1  pipe2  pipe3
   [0,1]   [2,3]  [4,5]  [6,7]
意识到,在任何给定的时间,您只需要两组管道(前一个命令的管道和下一个命令的管道)将简化您的代码并使其更加健壮.Ephemient 在这里给出了伪代码.他的代码更清晰,因为父代和子代不必进行不必要的循环来关闭不需要的文件描述符,并且因为父代可以在fork之后立即轻松地关闭文件描述符的副本.
作为旁注:您应该始终检查pipe,dup2,fork和exec的返回值.
编辑2:伪代码中的拼写错误.OP:num-pipes将是管道数量.例如,"ls | grep foo | sort -r"将有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);
}