CS644 The UNIX Shell, an example of a user-level program supplied with the system.

 

How the shell works, from http://www.cs.duke.edu/courses/spring05/cps210/Lab1.html

 

The OS command interpreter is the program that people interact with in order to launch and control programs. On UNIX systems, the command interpreter is usually called the shell: it is a user-level program that gives people a command-line interface to launching, suspending, and killing other programs. sh, ksh, csh, tcsh, bash, ... are all examples of UNIX shells. (It might be useful to look at the manual pages of these shells, for example, type: "man csh".)

Every shell is structured as the following loop:

  1. print out a prompt (write() syscall via say printf)
  2. read a line of input from the user (read() syscall via say fgets())
  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. when the child (i.e. the launched program) finishes, the shell repeats the loop by jumping to 1.

Although most of the commands people type on the prompt are the name of other UNIX programs (such as ls or more), shells recognize some special commands (called internal commands) which are not program names. For example, the exit command terminates the shell, and the cd command changes the current working directory. Shells directly make system calls to execute these commands, instead of forking a child process to handle them.

 

Additional Info for CS644

Similarly the cmd.exe program of Windows does a similar loop with CreateProcess for the specified program followed by waiting for the spawned process completion. Xinu has no command shell. If we want to run a certain program under Xinu, we build an executable with that certain program in it as its main.