primitive shell that knows how to launch new programs in the foreground and the background

Hi, i want to write a shell that is structured as the following loop:

1. Print out a prompt (e.g., "CSCI212Shell$ ");
2. Read a line from the user;
3. Parse the line into the program name and an array of parameters;
4. Use the fork() system call to spawn a new child process;
5. The child process then uses the exec() system call (or one of its variants) to launch the specified program;
6. The parent process (the shell) uses the wait() system call (or one of its variants) to wait for the child to terminate;
7. Once the child (the launched program) finishes, the shell repeats the loop by jumping to 1.

The shell should recognize the internal commands such as exit, jobs, and cd. exit should use the exit() system call to terminate the shell. cd uses the chdir() system call to change to a new directory. jobs provides a numbered list of processes currently executing in the background.

If the command line does not indicate any internal commands, it should be in the following form:
program_name arg1 arg2 .... argN &
The shell should invoke the program, passing it the list of arguments in the command line. The shell must wait until the started program completes unless the user runs it in the background (with &).

The shell needs to support pipes, eg. program1 arglist1 | program2 arglist2 | ... | programN arglistN [&]

Multiple processes need to be launched for piped commands and all of them should be waited on in a foreground execution. The pipe() and dup2() system calls will be useful???

The shell runs programs using two core system calls: fork() and execvp(). In short, fork() creates an exact copy of the currently running process, and is used by the shell to spawn a new process. The execvp() call is used to overload the currently running program with a new program, which is how the shell turns a forked process into the program it wants to run. In addition, the shell must wait until the previously started program completes unless the user runs it in the background (with &). This is done with the wait() system call or one of its variants (such as waitpid()). All these system calls can fail due to unforeseen reasons and should check their return status and report errors if they occur.

No input the user gives should cause the shell to exit (except when the user types exit or Ctrl+D). This means the shell should handle errors gracefully, no matter where they occur. Even if an error occurs in the middle of a long pipeline, it should be reported accurately and your shell should recover gracefully. The shell should not generate leaking open file descriptors.
Topic archived. No new replies allowed.