0

I am currently writing my own shell program. This simple shell can just execute commands.

When executing commands like vi or calc which require input from the terminal , the command is getting executed and is waiting for the input from the user. But I am unable to give any input on the screen.

How should the input be handled during the fork and exec.

Here is the piece of code which is executing commands:

    if((pid = fork()) < 0)
    {
            perror("Fork failed");
            exit(errno);
    }
    if(pid == 0)
    {
            // Child process
            if(execvp(arguments[0], arguments) == -1)
            {
                    child_status = errno;
                    switch(child_status)
                    {
                            case ENOENT:
                                    printf(" command not found \n");
                                    break;
                    }
                    exit(errno);
            }
    }
    else
    {
            // parent process
            int wait_stat;
            if(waitpid(pid , &wait_stat, WNOHANG) == -1)
            {
                    printf(" waitpid failed \n");
                    return;
            }
    }

} ~

Thanks,

0

1 Answer 1

3

The WNOHANG is causing the parent process not to wait and therefore (depending on platform) the child process will be detached from terminal IO or die.

Remove the WNOHANG.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.