Durchführung mehrerer Rohre in C

Ich versuche zu implementieren mehrere Rohre in meiner shell in C. fand ich ein tutorial auf dieser website und die Funktion, die ich gemacht habe, basiert auf diesem Beispiel. Hier ist die Funktion

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

}

Nachdem Sie es ausgeführt haben und Sie einen Befehl eingeben, wie zum Beispiel ls | grep bin die shell hängt nur dort und nicht ausgeben, ohne Ergebnis. Ich stellte sicher, ich Schloss alle Rohre. Aber es hängt ja nur. Ich dachte, dass es die waitpid das war das problem. Ich entfernte die waitpid und nach der Ausführung bekomme ich keine Ergebnisse. Was habe ich falsch gemacht? Danke.

Code Hinzugefügt:

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

    }

}
Stilistischer Hinweis für Beiträge: entfernen Sie den auskommentierten code, und entfernen Sie überflüssige Leerzeichen.
Getan
können Sie nach dem vollständigen code der Implementierung, wie, warum Sie brauchte, um struct-Befehle

InformationsquelleAutor mkab | 2011-12-05

Schreibe einen Kommentar