Anzeige:
Ergebnis 1 bis 13 von 13

Thema: Nach dem PIPEN verabschiedet sich die eigene Shell automatisch - ungewollt :(

  1. #1
    Registrierter Benutzer
    Registriert seit
    04.04.2008
    Beiträge
    50

    Exclamation Nach dem PIPEN verabschiedet sich die eigene Shell automatisch - ungewollt :(

    Hallo!

    Ich bastel an einer eigenen Shell und habe jetzt noch das kleine Problem das sich die Shell nach dem PIPEN verabschiedet.

    Also wenn im Elternprozess die execvp(cmd_in[0], cmd_in) aufgerufen wurde - und die richtige Ausgabe auf der Konsole erscheint, dann geht die Shell komplett raus.

    Wenn ich nur eine einfache Anweisung an die Shell übergebe, dann erwartet die Shell nach jeder Eingabe und Ausgabe (Konsole) eine neue Eingabe.
    Das funktioniert auch bestens.

    Nur sobald ich PIPE, dann geht die Shell aus der while(1) - Schleife raus.


    ###########################################

    Einfache Eingabe und deren Ausgabe auf der Konsole:


    Shell-Eingabe ( [command] [parameter] ) > ls -l
    #########################################

    INDEX_PIPE : 1
    cmd_input_output[0] : ls -l

    index : 0 ################################################## #################
    $$$$$$$$$

    strcpy_search_parameter : ls -l | size : 5

    strcpy_search_command_2 : ls -l | size : 5


    strcpy_search_parameter : ls -l | size : 5
    search_parameter MANUELL : PWD | size : 3
    command_3 READY : -l | size : 2
    command_1 READY : ls | size : 2

    3. Ich bin INDEX 0 !!!!!!!!!!!!!!!!!!!!


    cmd_in[0] : ls


    ERROR Zeile 652: cmd_in[0] : ls
    Elternprozess:
    ##############

    prog1
    Kindprozess:
    ##############

    prog2


    Gesamt 694
    -rw-r--r-- 1 std11999 other 1866 Mai 14 18:17 myTypes.h
    -rwxr-xr-x 1 std11999 other 16156 Mai 24 10:45 shell_aufg5_massimo
    -rw-r--r-- 1 std11999 other 21593 Mai 24 10:45 shell_aufg5_massimo.c
    -rwxr-xr-x 1 std11999 other 16052 Mai 19 14:48 shell_v1
    -rw-r--r-- 1 std11999 other 27711 Mai 21 10:48 shell_v1.c
    -rwxr-xr-x 1 std11999 other 20108 Mai 22 18:49 shell_v2
    -rw-r--r-- 1 std11999 other 29030 Mai 21 17:17 shell_v2.c
    -rwxr-xr-x 1 std11999 other 20636 Mai 24 19:33 shell_v3
    -rw-r--r-- 1 std11999 other 31334 Mai 25 10:52 shell_v3.c
    -rwxr-xr-x 1 std11999 other 21760 Mai 24 19:23 shell_v4
    -rw-r--r-- 1 std11999 other 33187 Mai 24 19:23 shell_v4.c
    -rw-r--r-- 1 std11999 other 20272 Mai 24 19:23 shell_v4.o
    -rwxr-xr-x 1 std11999 other 18172 Mai 25 11:35 shell_v5
    -rw-r--r-- 1 std11999 other 24900 Mai 25 11:29 shell_v5.c
    -rwxr-xr-x 1 std11999 other 18412 Mai 25 13:04 shell_v6
    -rw-r--r-- 1 std11999 other 25162 Mai 25 12:29 shell_v6.c
    -rw-r--r-- 1 std11999 other 784 Mai 14 18:18 shell.cmake
    status: 0
    Die Shell wurde im Kindprozess NICHT beendet!
    NO EXIT #####################################

    status: 0

    Shell-Eingabe ( [command] [parameter] ) >


    ###########################################

    Eingabe mit PIPE und deren Ausgabe auf der Konsole:


    Shell-Eingabe ( [command] [parameter] ) > ls | more
    #########################################

    PIPE gefunden!!!

    INDEX_PIPE : 2
    GESAMT : ls | more | size : 9

    Eingabestrom!!!

    search_INPUT_tmp : | more | size : 6

    Ausgabestrom!!!

    search_INPUT : more | size : 4

    memcpy_search_OUTPUT : ls | SIZE : 2

    search_OUTPUT : ls | size : 2

    cmd_input_output[0] : more
    cmd_input_output[1] : ls

    index : 0 ################################################## #################
    $$$$$$$$$

    strcpy_search_parameter : more | size : 4

    strcpy_search_command_2 : more | size : 4


    strcpy_search_parameter : more | size : 4
    search_parameter MANUELL : PWD | size : 3

    command_1 READY : more | size : 4

    1.1 Ich bin INDEX 0 !!!!!!!!!!!!!!!!!!!!


    IN IN IN

    cmd_in[0] : more

    index : 1 ################################################## #################
    $$$$$$$$$

    strcpy_search_parameter : ls | size : 2

    strcpy_search_command_2 : ls | size : 2


    strcpy_search_parameter : ls | size : 2
    search_parameter MANUELL : PWD | size : 3

    command_1 READY : ls | size : 2

    1.2 Ich bin INDEX 1 !!!!!!!!!!!!!!!!!!!!


    OUT OUT OUT

    cmd_out[0] : ls


    ERROR Zeile 652: cmd_in[0] : more
    ERROR Zeile 653: cmd_out[0] : ls
    Elternprozess:
    ##############

    prog1
    Kindprozess:
    ##############

    prog2
    myTypes.h
    shell_aufg5_massimo
    shell_aufg5_massimo.c
    shell_v1
    shell_v1.c
    shell_v2
    shell_v2.c
    shell_v3
    shell_v3.c
    shell_v4
    shell_v4.c
    shell_v4.o
    shell_v5
    shell_v5.c
    shell_v6
    shell_v6.c
    shell.cmake
    aa0106-05 std11999/81:
    prog1
    Kindprozess:
    ##############

    prog2

    aa0106-05 std11999/81:



    ###########################################

    Nachdem er in die execvp des Elternprozesses zu gehen scheint - beendet sich die Shell automatisch. Ich weiß einfach nicht wieso.
    Mehhr als 2 Tage habe ich jetzt schon diese Nebenerscheinung - wärend ich an dem Programm arbeite. Ich weiß einfach nicht wie ich die Shell bei der PIPE in der Schleife halten soll, bis ich Sie mit EXIT beende.


    Hier mal der Code:

    PHP-Code:
    // Verzeichniswechsel:
                // ###########
                
    if ( INDEX_PIPE == )
                {
                    if(
    strcmp(cmd_in[0],"cd") == 0)
                    {
                        
    DEBUGOUT("\n\nVerzeichniswechsel: -> Vom aktuellen Verzeichnis ...\n");
                        if(
    chdir(cmd_in[1]) == -1)
                        {
                            
    printf("Verzeichniswechsel fehlgeschlagen! \n");
                        }
                        else
                        {
                            
    printf("    ... ins Verzeichnis %s gewechselt \n"cmd_in[1]);
                        }
                    }
                    else if(
    strcmp(cmd_in[0],"exit") == 0)
                    {
                        
    DEBUGOUT("exit(%i)\n"2);
                        
    printf("\nDer aktuelle Kindprozess wird beendet \n");
                        
    printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
                        exit(
    2);
                    }
                }
                
    // Prozesserzeugung
                
    if((pid=fork()) < 0)
                {  
                    
    perror("pipe : "); 
                    exit(
    0); 
                }

                if(
    pid 0)        // Fehler beim kreieren des neuen Prozesses;
                
    {
                    
    perror("fork");
                    continue;
                }
                if(
    pid != 0)
                {    
    // Elternprozess;
                    // pid == Prozess-ID
                    
    printf("Elternprozess: \n");
                    
    printf("############## \n\n");
                    
    DEBUGOUT("prog1 \n");
                    
    int stat_loc;
                    
    // ********************************************
                    // PIPE - Shell-Erweiterung
                    
    if( INDEX_PIPE == )
                    {    
                        if
                        ( 
                            ( 
    err close(fd[0])) == ERROR ||    // mit pipe() erzeugter Filediskriptor wird wieder geschlossen
                            
    err close(1)    ) == ERROR ||    // mit pipe() erzeugter Diskriptor wird wieder geschlossen
                            
    err dup(fd[1])    ) == ERROR ||    // mit pipe() erzegter Filediskriptor wird dupliziert ( field 1)
                            
    err close(fd[1])) == ERROR         // mit pipe() erzeugter Filediskriptor wird wieder geschlossen
                        
    )
                        {
                            
    printf("Fehler beim Kind 'ls': %i \n"err);
                            exit(
    err);
                        }
                        else
                        {
                            
    //execl(prog1, prog1, 0);        // Programme prog1 und prog2 werden so gestartet,
                            
    if(execvp(cmd_out[0],cmd_out) == 0)
                            {    
                                
    perror("cannot execl");
                                exit(
    err);
                            }
                            else
                            {
                                
    DEBUGOUT("cmd_out BEENDET!!!\n");
                                exit(
    1);
                            }
                            
                        }
                        if ((
    waitpid (pidNULL0)) < 0
                        {
                            
    perror ("waitpid");
                            exit (
    EXIT_FAILURE);
                        }
                    }    
                    
    // END
                    // ********************************************
                    
    if ((waitpid(pid, &stat_loc0)) == -1// falls fork() scheitert;
                    
    {
                        
    perror("cannot wait");
                        exit(-
    1);
                    }    
                    else if ((
    WIFEXITED(stat_loc)) == 1// Liefert true zurueck, wenn der Kindprozess sich normal beendet hat
                    
    {
                        
    printf("status: %d\n"WEXITSTATUS(stat_loc));
                        if((
    WEXITSTATUS(stat_loc)) == 2)
                        {
                            
    printf("Die Shell wurde im Kindprozess normal beendet!\n");
                            
    printf("EXIT #########################################\n\n");
                            
    printf("status: %d\n"WEXITSTATUS(stat_loc));
                            exit(
    1);
                        }            
                        else
                        {
                            
    printf("Die Shell wurde im Kindprozess NICHT beendet!\n");
                            
    printf("NO EXIT #####################################\n\n");
                            
    printf("status: %d\n"WEXITSTATUS(stat_loc));
                        }
                    }
                }
                else
                {        
    // Kindprozess;
                        
                    
    printf("Kindprozess: \n");
                    
    printf("############## \n\n");    
                    
    DEBUGOUT("prog2 \n");
                    
    // ********************************************
                    // PIPE - Shell-Erweiterung
                    
    if( INDEX_PIPE == )
                    {    
                        if
                        (
                            ( 
    err close(fd[1])) == ERROR ||    // mit pipe() erzeugter Filediskriptor wird wieder geschlossen
                            
    err close(0)    ) == ERROR ||    // mit pipe() erzeugter Diskriptor wird wieder geschlossen
                            
    err dup(fd[0])    ) == ERROR ||    // mit pipe() erzeugter Filediskriptor wird dupliziert ( field 0)
                            
    err close(fd[0])) == ERROR        // mit pipe() erzeugter Filediskriptor wird wieder geschlossen
                        
    )
                        {
                            
    printf("Fehler beim Kind 'more': %i \n"err);
                            exit(
    err);
                        }
                        else
                        {
                            
    //execl(prog2, prog2, 0);
                            
                            
    if(execvp(cmd_in[0],cmd_in) == 0)
                            {
                                
    perror("cannot exec");
                                exit(
    err);
                            }
                            
    printf("\n\n");
                        }
                        
                    }
                    if( 
    INDEX_PIPE == )
                    {
                        
    printf("\n\n");
                        if((
    execvp(cmd_in[0],cmd_in)) == 0)
                        {                                 
                            
    perror("cannot exec");
                            
    DEBUGOUT("exit(1)\n");
                            exit(
    1);
                        }
                        
                    }
                    
    //exit(1);
                
    }
        }    
        return;    



    Ich bitte vielmals um Eure Hilfe!!!!


    Vielen Dank!

  2. #2
    Registrierter Benutzer Avatar von BLUESCREEN3D
    Registriert seit
    08.11.2002
    Beiträge
    665
    Du weißt schon, dass exec() den aktuellen Prozess ersetzt?
    Oder anders gesagt: Dein Programm läuft nach einem exec() nur dann weiter, wenn das exec() fehlschlug.

  3. #3
    Registrierter Benutzer
    Registriert seit
    04.04.2008
    Beiträge
    50
    Das wusste ich nicht.

    Muss ich dann etwa execl( ... ) nutzen?

    und wie bringe ich da dann cmd_in[0] und cmd_in ein?

  4. #4
    Registrierter Benutzer
    Registriert seit
    04.04.2008
    Beiträge
    50
    ok ... so wie ich das sehe muss ich im kindprozess die

    execlp("/bin/sh", "/bin/sh", "-c", command, NULL);


    und im elternprozess irgendwie


    if ((l = read(fd[0], buffer, 79)) == -1)
    perror("pipe2: read() failed");
    else
    write(STDOUT_FILENO, buffer, l);


    nutzen.


    Nur verstehe ich noch nicht die zusammenhänge .... wie funktioniert das genau?

    Irgendwie kann ich mir das nur schwer vorstellen.

    Ich bitte um eure Hilfe!

  5. #5
    Registrierter Benutzer
    Registriert seit
    04.04.2008
    Beiträge
    50
    Ich habe das Programm mal ein wenig überarbeitet.

    In dieser Version weiß ich nicht so recht wie ich cmd_in[0], cmd_in --> more
    und cmd_out[0], cmd_out --> ls

    für die eingabe: ls | more

    ... einbringen soll.

    ich hatte es vorher mit execvp gelöst ... diese Vorgehensweise war aber falsch.

    Nun bin ich mir nicht sicher wie ich die Shell mit dieser PIPE zum laufen bekommen soll.


    Hier mal der Code:


    PHP-Code:
                            if( INDEX_PIPE == )
                {
                    if( (
    pipe(fd1) != 0) || (pipe(fd2) != 0) ) // pipe() wird benutzt und die erzeugten Deskriptoren müssen mit close() wieder geschlossen werden
                    
    {
                        
    perror("cannot create pipe"); // bei Misserfolg
                        
    return;
                    }
                }    
    // Prozesserzeugung
                
    if((pid=fork()) < 0)
                {  
                    
    perror("pipe : "); 
                    exit(
    0); 
                }

                if(
    pid 0)        // Fehler beim kreieren des neuen Prozesses;
                
    {
                    
    perror("fork");
                    continue;
                }
                if(
    pid != 0)
                {    
    // Elternprozess;
                    // pid == Prozess-ID
                    
    printf("Elternprozess: \n");
                    
    printf("############## \n\n");
                    
    DEBUGOUT("prog1 \n");
                    
    int stat_loc;
                    
    // ********************************************
                    // PIPE - Shell-Erweiterung cmd_out
                    
    if( INDEX_PIPE == )
                    {    
                        
    close(fd1[0]);      
                        
    close(fd2[1]);

                        
    write(fd1[1], cmd_out[0], 28); // <-- HIER irgendwie cmd_out[0] und cmd_out für 'ls'
                        
    close(fd1[1]); 
                        
    wait(NULL);

                        if ((
    read(fd2[0], bufferMAXLINE-1)) == -1)
                            
    perror("pipe2: read() failed");
                        else
                            
    write(STDOUT_FILENObufferl); 
                        
                        
    close(fd2[0]);
                    }    
                    
    // END
                    // ********************************************
                    
    if ((waitpid(pid, &stat_loc0)) == -1// falls fork() scheitert;
                    
    {
                        
    perror("cannot wait");
                        exit(-
    1);
                    }    
                    else if ((
    WIFEXITED(stat_loc)) == 1// Liefert true zurueck, wenn der Kindprozess sich normal beendet hat
                    
    {
                        
    printf("status: %d\n"WEXITSTATUS(stat_loc));
                        if((
    WEXITSTATUS(stat_loc)) == 100)
                        {
                            
    printf("\n\nDie Shell wurde im ELTERNPROZESS normal beendet!\n");
                            
    printf("EXIT #########################################\n\n");
                            
    printf("status: %d\n"WEXITSTATUS(stat_loc));
                            exit(
    1);
                        }            
                        else
                        {
                            
    printf("\n\nDie Shell wurde im ELTERNPROZESS NICHT beendet!\n");
                            
    printf("NO EXIT #####################################\n\n");
                            
    printf("status: %d\n"WEXITSTATUS(stat_loc));
                        }
                    }
                }
                else
                {        
    // Kindprozess;
                        
                    
    printf("Kindprozess: \n");
                    
    printf("############## \n\n");    
                    
    DEBUGOUT("prog2 \n");
                    
    // ********************************************
                    // PIPE - Shell-Erweiterung
                    
    if( INDEX_PIPE == )
                    {    
                        
    close(fd1[1]);
                        
    close(fd2[0]);
                        
                        if((
    dup2(fd1[0], STDIN_FILENO) == -1) || (dup2(fd2[1], STDOUT_FILENO) == -1))
                        {
                            
    perror("pipe2: dup2() failed");
                            exit(
    1);
                        }
                        
    close(fd1[0]);
                        
    close(fd2[1]);
                        
    execlp("/bin/sh""/bin/sh"cmd_in[0], cmd_in); // <-- cmd_in[0] und cmd_in für 'more'
                        
    perror("pipe2: execlp() failed");
                        exit(
    1);
                    }
                    if( 
    INDEX_PIPE == )
                    {
                        
    printf("\n\n");
                        if((
    execvp(cmd_in[0],cmd_in)) == 0)
                        {     
    DEBUGOUT("\nERROR bei der Eingabe\n");    
                            
    perror("cannot exec");
                            
    DEBUGOUT("exit(1)\n");
                            exit(
    1);
                        }
                    }
                    
    //exit(1);
                
    }
        }    
        return; 


    Bitte gebt mir doch einen Hinweis. Ich bin echt schon langsam am verzweifeln


    Vielen Dank!!!
    Geändert von Chito (25-05-2008 um 15:46 Uhr)

  6. #6
    Registrierter Benutzer Avatar von BLUESCREEN3D
    Registriert seit
    08.11.2002
    Beiträge
    665
    Ich habe jetzt keine Lust, deinen Quellcode zu interpretieren, aber ich vermute mal, dass du zwei Programme starten willst, die per Pipe verbunden sind.
    Wenn ja, geht das so (ohne Fehlerbehandlung):
    Code:
    int pipe_fd[2];
    pipe(pipe_fd);
    
    if (fork()) //parent
    	{
    	close(pipe_fd[0]);
    	dup2(pipe_fd[1], STDOUT_FILENO); //write to pipe
    
    	execl("/bin/echo", "/bin/echo", "wort1", "wort2", NULL);
    	}
    else //child
    	{
    	close(pipe_fd[1]);
    	dup2(pipe_fd[0], STDIN_FILENO); //read from pipe
    
    	execl("/usr/bin/wc", "/usr/bin/wc", NULL);
    	}
    Das wäre jetzt "echo wort1 wort2 | wc".

    PS: Alle exec()-Befehle ersetzen den aktuellen Prozess (siehe "man 3 exec").

  7. #7
    Registrierter Benutzer
    Registriert seit
    04.04.2008
    Beiträge
    50
    wenn ich das zum laufen gebracht habe ... gehen wir mal einen trinken *gg

    Vielen Dank

  8. #8
    Registrierter Benutzer
    Registriert seit
    04.04.2008
    Beiträge
    50
    hm ... ich habe das mal in meine shell eingebaut ... das funktioniert ganz gut ... nur beendet sich meine programmierte shell, nachdem die ausgabe erfolgte.

    Shell-Eingabe ( [command] [parameter] ) > ls -al /home1 | cat -n
    #########################################

    PIPE gefunden!!!

    INDEX_PIPE : 2
    GESAMT : ls -al /home1 | cat -n | size : 22

    Eingabestrom!!!

    search_INPUT_tmp : | cat -n | size : 8

    Ausgabestrom!!!

    search_INPUT : cat -n | size : 6

    memcpy_search_OUTPUT : ls -al /home1 | SIZE : 13

    search_OUTPUT : ls -al /home1 | size : 13

    cmd_input_output[0] : cat -n
    cmd_input_output[1] : ls -al /home1

    index : 0 ################################################## #################
    $$$$$$$$$

    strcpy_search_parameter : cat -n | size : 6
    search_parameter MANUELL : PWD | size : 3
    command_3 READY : -n | size : 2
    command_1 READY : cat | size : 3

    3. Ich bin INDEX 0 !!!!!!!!!!!!!!!!!!!!


    cmd_in[0] : cat

    index : 1 ################################################## #################
    $$$$$$$$$

    strcpy_search_parameter : ls -al /home1 | size : 13
    search_parameter MANUELL : PWD | size : 3
    command_3 READY : -al /home1 | size : 10
    command_0 READY : ls | size : 2
    command_2 READY : /home1 | size : 6
    command_1 READY : -al | size : 3

    2. Ich bin INDEX 1 !!!!!!!!!!!!!!!!!!!!


    cmd_in[0] : ls


    EINGABE OHNE PIPE : ################################################## ######

    Ergebnis : cmd_out[0] : ls
    Ergebnis : cmd_out[1] : -al
    Ergebnis : cmd_out[2] : /home1


    Ergebnis : cmd_in[0] : cat
    Ergebnis : cmd_in[1] : -n

    EINGABE OHNE PIPE ENDE: ################################################## ##

    Elternprozess:
    ##############

    prog1
    Kindprozess:
    ##############

    prog2
    1 lrwxrwxrwx 1 root root 18 Dez 19 12:53 /home1 -> /storage3_l0/home1
    aa0106-10 std11999/120:




    Wie halte ich die Shell denn in der Schleife? ...

  9. #9
    Registrierter Benutzer
    Registriert seit
    04.04.2008
    Beiträge
    50
    der Code:

    PHP-Code:
                // *************************************************************************************************************************
                // PIPE - Shell-Erweiterung
                
    if( INDEX_PIPE == )
                {
                    if( (
    pipe(pipe_fd) != 0) ) // pipe() wird benutzt und die erzeugten Deskriptoren müssen mit close() wieder geschlossen werden
                    
    {
                        
    perror("pipe2: pipe() failed"); // bei Misserfolg
                        
    return;
                    }
                }    
                
    // END
                // *************************************************************************************************************************
                //
                // Verzeichniswechsel:
                // ###########
                
    if ( INDEX_PIPE == )
                {
                    if(
    strcmp(cmd_in[0],"cd") == 0)
                    {
                        
    DEBUGOUT("\n\nVerzeichniswechsel: -> Vom aktuellen Verzeichnis ...\n");
                        if(
    chdir(cmd_in[1]) == -1)
                        {
                            
    printf("Verzeichniswechsel fehlgeschlagen! \n");
                        }
                        else
                        {
                            
    printf("    ... ins Verzeichnis %s gewechselt \n"cmd_in[1]);
                        }
                    }
                    else if(
    strcmp(cmd_in[0],"exit") == 0)
                    {
                        
    DEBUGOUT("exit(%i)\n"100);
                        
    printf("\nDer aktuelle Kindprozess wird beendet \n");
                        
    printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
                        exit(
    100);
                    }
                }
                
    // Prozesserzeugung
                
    if((pid=fork()) < 0)
                {  
                    
    perror("pipe : "); 
                    exit(
    0); 
                }

                if(
    pid 0)        // Fehler beim kreieren des neuen Prozesses;
                
    {
                    
    perror("fork");
                    continue;
                }
                if(
    pid == 0)
                {        
    // Kindprozess;
                        
                    
    printf("Kindprozess: \n");
                    
    printf("############## \n\n");    
                    
    DEBUGOUT("prog2 \n");
                    
    // ********************************************
                    // PIPE - Shell-Erweiterung
                    
    if( INDEX_PIPE == )
                    {    
                        
                        
    close(pipe_fd[1]);
                        
    dup2(pipe_fd[0], STDIN_FILENO); // lese aus der PIPE

                        
    execvp(cmd_in[0], cmd_in);
                    }
                    if( 
    INDEX_PIPE == )
                    {
                        
    printf("\n\n");
                        if((
    execvp(cmd_in[0],cmd_in)) == 0)
                        {     
    DEBUGOUT("\nERROR bei der Eingabe\n");    
                            
    perror("cannot exec");
                            
    DEBUGOUT("exit(1)\n");
                            exit(
    1);
                        }
                    }
                }
                if(
    pid != 0)
                {    
    // Elternprozess;
                    // pid == Prozess-ID
                    
    printf("Elternprozess: \n");
                    
    printf("############## \n\n");
                    
    DEBUGOUT("prog1 \n");
                    
    int stat_loc;
                    
    // ********************************************
                    // PIPE - Shell-Erweiterung cmd_out
                    
    if( INDEX_PIPE == )
                    {    
                        
                        
    close(pipe_fd[0]);
                        
    dup2(pipe_fd[1], STDOUT_FILENO); // Schreibe in die PIPE
                    
                        
                        
                        
    if((execvp(cmd_out[0], cmd_out)) == 0)
                        {
                            
    perror("cannot exec");
                            
    DEBUGOUT("exit(1)\n");
                            exit(
    1);
                        }
                        exit(
    0);
                        
    DEBUGOUT("\nHALLO\n");
                    }    
                    
    // END
                    // ********************************************
                    
                    
    if ((waitpid(pid, &stat_loc0)) == -1// falls fork() scheitert;
                    
    {
                        
    perror("cannot wait");
                        exit(-
    1);
                    }
                    
                    if ((
    WIFEXITED(stat_loc)) == 1// Liefert true zurueck, wenn der Kindprozess sich normal beendet hat
                    
    {
                        
    printf("status: %d\n"WEXITSTATUS(stat_loc));
                        if((
    WEXITSTATUS(stat_loc)) == 100)
                        {
                            
    printf("\n\nDie Shell wurde im ELTERNPROZESS normal beendet!\n");
                            
    printf("EXIT #########################################\n\n");
                            
    printf("status: %d\n"WEXITSTATUS(stat_loc));
                            exit(
    1);
                        }            
                        else
                        {
                            
    printf("\n\nDie Shell wurde im ELTERNPROZESS NICHT beendet!\n");
                            
    printf("NO EXIT #####################################\n\n");
                            
    printf("status: %d\n"WEXITSTATUS(stat_loc));
                        }
                    }
                    
                }

        }    


  10. #10
    Registrierter Benutzer
    Registriert seit
    04.04.2008
    Beiträge
    50
    also ... mein programm arbeitet jetzt tatellos ... es besteht lediglich noch das problem, das sich meine programmierte shell bei der Eingabe:

    'ls -l | cat -n" ... z.B.

    nach der Ausgabe verabschiedet ... sich also schließt. Genau dieser Effekt ist nicht so schön.

    Leider komme ich auf keine Lösung.
    Ich möchte schon gerne verstehen wieso sich die Shell hierbei so verhält und was für Ursachen das haben kann.

    Bitte gebt mir diesbezüglich einen Lösungshinweis ... sonst werde ich die Nacht schlecht träumen


    Danke!



    Chito

  11. #11
    Registrierter Benutzer Avatar von ContainerDriver
    Registriert seit
    10.01.2003
    Beiträge
    418
    Hallo,

    ich weiß nicht wie du das Problem lösen kannst, aber es entsteht auf jeden Fall beim Aufruf von execvp, da (wie von BLUESCREEN3D schon angesprochen) folgendes gilt:
    Zitat Zitat von man execvp
    The exec() family of functions replaces the current process image with a new process image.
    [...]
    If any of the exec() functions returns, an error will have occurred. The return value is -1, and the global variable errno will be set to indicate the error.
    Lösung könnte sein, erst zu forken und dann noch mal weiter zu forken, bin mir jetzt aber nicht sicher, ob das wirklich eine die beste Lösung ist.

    Gruß, Florian
    Ein gebrechlich Wesen ist der X-Server.

  12. #12
    Registrierter Benutzer
    Registriert seit
    04.04.2008
    Beiträge
    50
    Zitat Zitat von ContainerDriver Beitrag anzeigen
    Hallo,

    ich weiß nicht wie du das Problem lösen kannst, aber es entsteht auf jeden Fall beim Aufruf von execvp, da (wie von BLUESCREEN3D schon angesprochen) folgendes gilt:

    Gruß, Florian
    Das sehe ich auch so. Wenn ich aber die execvp OHNE zu pipen aufrufe ( 'ls -l' ) dann bleibt die Eingabeaufforderung in der Schleife. hm ...

    sobald mir eine Lösung hierzu einfällt und funktioniert ... poste ich die mal.



  13. #13
    Registrierter Benutzer
    Registriert seit
    04.04.2008
    Beiträge
    50
    Die Lösung des Problems:

    Im Kindprozess wird ein weiterer Kindprozess erzeugt, der dann mit dem Ersten das PIPEN übernimmt.


    Hier der zugehörige Codeausschnitt: (getestet und OK)

    PHP-Code:
    // Eingabe MIT PIPE:
            // ##########
            // *************************************************************************************************************************
            // PIPE - Shell-Erweiterung
            
    if( INDEX_PIPE == )
            {
                if( (
    pipe(pipe_fd) != 0) ) // pipe() wird benutzt und die erzeugten Deskriptoren müssen mit close() wieder geschlossen werden
                
    {
                    
    perror("pipe2: pipe() failed"); // bei Misserfolg
                    
    return;
                }
            }    
            
    // END
            // *************************************************************************************************************************
            //
            // Verzeichniswechsel:
            // ###########
            
    switch (pid fork ())
            {
                case -
    1:
                        
    perror("fork1 : ");
                        break;
                case 
    0:
                        if( 
    INDEX_PIPE == )
                        {
                            
    close(0);
                            
    stat_loc dup2(pipe_fd[0], 0); // Schreibe in die PIPE // 1
                            
    if (stat_loc == -1
                            {
                                
    perror("progname pid1");
                                return;
                            }
                            
    close(pipe_fd[1]);
                        }
                        
                        if((
    execvp(cmd_in[0], cmd_in)) == 0)
                        {
                            
    perror("cannot exec");
                            exit(
    1);
                        }
                        
                        break;

                default:
                                                                            
                        if( 
    INDEX_PIPE == )
                        {
                            switch (
    pid2 fork ())
                            {
                                case -
    1:
                                            
    perror("fork2 : ");
                                            break;

                                case 
    0:
                                            
    close(1);
                                            
    stat_loc dup2(pipe_fd[1], 1); // lese aus der PIPE // 0
                                            
    if (stat_loc == -1
                                            {
                                                
    perror("progname pid2");
                                                return;
                                            }
                                            
    close(pipe_fd[0]);
                                        
                                            if((
    execvp(cmd_out[0], cmd_out)) == 0){
                                                
    perror("cannot exec");
                                                exit(
    1);
                                            }
                                            break;

                                default:
                                            
    close(pipe_fd[1]);
                                            
    close(pipe_fd[0]);
                                            if ((
    waitpid(pid2, &stat_loc0)) != -1// falls fork() scheitert;
                                            
    {    
                                                continue;
                                            }
                                            else
                                            {    
                                                exit(
    1);
                                            }
                                            exit(
    1);
                                            break;
                            }
                        }
                            
                        if ((
    waitpid(pid, &stat_loc0)) != -1// falls fork() scheitert;
                        
    {
                            continue;
                        }
                        else
                        {
                            exit(
    1);
                        }       
                        
                        break;
            } 
            }
        }      


Lesezeichen

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •