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 == 1 )
{
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 == 2 )
{
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 (pid, NULL, 0)) < 0)
{
perror ("waitpid");
exit (EXIT_FAILURE);
}
}
// END
// ********************************************
if ((waitpid(pid, &stat_loc, 0)) == -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 == 2 )
{
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 == 1 )
{
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!
Lesezeichen