samedi 18 septembre 2021

Implementing Background process in a dummy c++ shell

I've been trying to mimic & in my dummy shell. The foreground process works fine, but as soon as I include the & symbol, it doesn't behave as expected. The program shows unexpected behavior. It first executes the process (which should not be executed as a foreground process) and then it just freezes until I press the Enter key.

Here is the snippet of my code.

if(!background)
{
    int bgpid;
    pid_t fork_return;
    fork_return = fork();
    if(fork_return == 0)
    {
        if(execvp(path, args) == -1)
        {
            bgpid = getpid();
            cout<<"Error\n";
            return 1;
        }
    }else{
        waitpid(fork_return, &status, 0);
        return 1;
    }
}
else if(background)
{
    int bgpid;
    pid_t fork_return;
    fork_return = fork();
    if(fork_return == 0)
    {
        setpgid(0,0);
        if(execvp(path, args) == -1)
        {
            bgpid = getpid();
            cout<<"Error\n";
            return 1;
        }
        else if(fork_return != -1)
        {
            addToTable(bgpid);
            return 1;
        }
    }else{
        return 1;
    }
}

Aucun commentaire:

Enregistrer un commentaire