32

The shell i'm writing needs to execute a program given to it by the user. Here's the very shortened simplified version of my program

int main()
{
    pid_t pid = getpid(); // this is the parents pid

    char *user_input = NULL;
    size_t line_sz = 0;
    ssize_t  line_ct = 0; 

    line_ct = getline(&user_input, &line_sz, stdin); //so get user input, store in user_input

    for (;;)
    {
        pid_t child_pid = fork(); //fork a duplicate process

        pid_t child_ppid = getppid(); //get the child's parent pid

        if (child_ppid == pid) //if the current process is a child of the main process
        {
            exec(); //here I need to execute whatever program was given to user_input
            exit(1); //making sure to avoid fork bomb
        }

        wait(); //so if it's the parent process we need to wait for the child process to finish, right?

    }
}
  1. Have I forked the new process & checked to see if it's a child process correctly
  2. What exec could I use here for what I'm trying to do? What is the most simple way
  3. What are my arguments to wait? the documentation I'm looking at isn't helping much

Assume the user might input something like ls, ps, pwd

Thanks.

Edit:

const char* hold = strdup(input_line);
char* argv[2]; 

argv[0] = input_line;
argv[1] = NULL;

char* envp[1];
envp[0] = NULL;

execve(hold, argv, envp);
Collin
  • 1,777
  • 4
  • 26
  • 42

1 Answers1

64

Here's a simple, readable solution:

pid_t parent = getpid();
pid_t pid = fork();

if (pid == -1)
{
    // error, failed to fork()
} 
else if (pid > 0)
{
    int status;
    waitpid(pid, &status, 0);
}
else 
{
    // we are the child
    execve(...);
    _exit(EXIT_FAILURE);   // exec never returns
}

The child can use the stored value parent if it needs to know the parent's PID (though I don't in this example). The parent simply waits for the child to finish. Effectively, the child runs "synchronously" inside the parent, and there is no parallelism. The parent can query status to see in what manner the child exited (successfully, unsuccessfully, or with a signal).

Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Ok so from linux man pages: int execve(const char *filename, char *const argv[], char *const envp[]); Could you explain those arguments in the context of my program – Collin Sep 30 '13 at 17:04
  • @user2079802: Doesn't the man page explain how `execve` works? You can also use various [other exec interfaces](http://linux.die.net/man/3/execl). – Kerrek SB Sep 30 '13 at 17:05
  • @user2079802: No need for `strdup`. Just `execve(argv[0], argv, envp)` should do. – Kerrek SB Sep 30 '13 at 18:14
  • In fact, the child can call `getppid()` to get the parent process' ID. No need to save it in advance. :-) – Alan Haggai Alavi Jan 06 '14 at 10:32
  • @AlanHaggaiAlavi: One less system call :-) – Kerrek SB Jan 06 '14 at 11:32
  • Where the child says "exec never returns" - this isn't quite correct, the documentation says "On success, execve() does not return, on error -1 is returned, and errno is set appropriately." So shouldn't the code be: `if (execve(...) == -1) _exit(errno) else _exit(EXIT_SUCCESS);` – parsley72 Jan 25 '21 at 01:21