#include #include #include #include #include #include #include void mataZombies(int); void ingreso_cmd(char *, char *); int comando_background(char *); int main(int argc,char **argv) { signal(SIGCHLD,mataZombies); char comando[512]; int retorno; char back = ' ' ; printf("Ingrese Comando sin argumentos (ls,ps,cal,ls&,cal&,./suenio&,...):\n"); ingreso_cmd(comando,&back); while (strncmp(comando,"exit",4)) { pid_t pid = fork(); if ( pid == 0 ) { // proceso hijo se convierte en el comando int r = execlp(comando,comando,NULL); printf("Comando [%s] Erroneo!\n",comando); exit(r); } else { // proceso padre MiniShell if ( back != '&' ) // Es foreground { pid = waitpid(pid,&retorno,0); printf("Proceso %d finalizo con status = %d\n",pid,retorno/256); } else usleep(500000); } printf("Ingrese Comando sin argumentos (ls,ps,cal,ls&,cal&,./suenio&,...):\n"); ingreso_cmd(comando,&back); } printf("fin shell\n"); exit(0); } // ingresa comando por teclado void ingreso_cmd(char *cmd, char *back) { *back = ' ' ; memset(cmd,0,512); printf("%%"); fgets(cmd,512,stdin); cmd[strlen(cmd)-1] = '\0'; //elimina el enter if (comando_background(cmd)) { *back = '&' ; cmd[strlen(cmd)-1] = '\0';//elimina el & } } // verifica si el comando es en background int comando_background(char *cmd) { int n = strlen(cmd)-1; while(n >= 0 && cmd[n] == ' ') { cmd[n] = '\0'; n--; } return ( n >= 0 && cmd[n] == '&'); } // recupera los hijos terminados void mataZombies(int signo) { while(waitpid(0,0,WNOHANG) > 0); }