0

Good afternoon, I'm a student, and I'm trying to create my own shell in C, so far I've gotten it to do everything I need correctly but I'm looking for a way to implement asynchronous synchronization when typing & behind a statement or command (Also called background), that the shell does not wait for the completion of the child process but continue to wait for new user commands as the linux shell normally does. I leave my code to see if anyone can help me, I guess I have to do something with the waitpid. Thank you very much in advance.

PS: I also need to write & show the PID of the process that is generated, just like the unix shell does.

PS: I'm Spanish student, and my english isn't good, i try translate, i hope you can understand me.

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
#include <unistd.h> 
#include <libgen.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <ctype.h>  

#define TAM 1000 

void  parseo(char *line, char **argv);
char* normalizar(char *str);

int main(void) {

    char  cad[TAM];             
    char  *argv[TAM]; 
    char  *to;

    pid_t pid;
    int status;

    while (1) {                   

        printf("user@PC $: ");    
        fgets(cad, TAM, stdin);

        // Si encontramos un salto de linea (se pulsa enter)
        if (cad[strlen(cad) - 1] == '\n')                                                           
            cad[strlen(cad) - 1] = '\0';    // lo marcamos como final de sentencia    

        // Si no se escribe nada, se espera
        if (strlen(cad) == 0) continue;

        // Parseamos lo introducido en el shell
        parseo(cad, argv);

        // Normalizamos los comandos
        normalizar((char*)argv[0]);

        // Exit para salir del shell
        if (!strcmp(argv[0], "exit")) exit(0);  

        // Implementación del comando CD
        if (!strcmp(argv[0], "cd")){

            to = argv[1];
            // Retorno al directorio anterior
            if(argv[1] == NULL) chdir("..");

            chdir(to);
            continue;
        }            
        // Creamos el proceso
        pid = fork();

        if (pid  < 0) { 
            printf("Error al crear los procesos \n");
            exit(1);

        } else if (pid == 0) {   
            // Ejecutamos los comandos, a no ser que sea erroneo
            if (execvp(*argv, argv) < 0) {
                printf("%s: no se encontró la orden \n", argv[0]);
                exit(1);
            }
        }else {  

            waitpid(pid,&status,0);
        }   

    }
    return 0;
}

void  parseo(char *line, char **argv){

    while (*line != '\0') {       
        while (*line == ' ' || *line == '\t' || *line == '\n')

            *line++ = '\0';     
            *argv++ = line; 

        while (*line != '\0' && *line != ' ' && *line != '\t' && *line != '\n') 
            line++;             
    } 
    *argv = '\0';    
}

char* normalizar(char *str){

    unsigned char *p = (unsigned char *)str;

    while (*p) {
        *p = tolower((unsigned char)*p);
        p++;
    }

    return str;
}
Michael Bueno
  • 33
  • 1
  • 1
  • 5
  • Please do a search. This task has been done by many students (and non-students) before and I think you should be able to find something to help you there. – kaylum May 07 '17 at 23:41
  • Hey Michael, if you have a problem with English, try the Spanish StackOverflow. Hey Michael, si tienes un problema con el inglés, prueba el español StackOverflow. – Manav Dubey May 07 '17 at 23:42
  • yes, we see this Q every week (practically). Searching for `[c] background shell` show 309 Q/As. In general, please search for your terms before posting a Q. Good luck. – shellter May 08 '17 at 02:25
  • Simply don't use `wait()` or `waitpid()` to wait for it to complete before moving on to the next command. Of course, that then leaves you with the problem that the child will probably die at some time and you'll need to report it when it does — even if you were mainly waiting for another process to die instead. – Jonathan Leffler May 08 '17 at 06:14

0 Answers0