From CS61
Jump to: navigation, search

Baby Shell

For assignment 4 you'll be building a pretty functional shell (command line interpreter). Today, we'll get some practice with the system calls you'll need by building a pretty simple shell.

You'll find today's code in cs61-exercises/l13.


The code to read command lines and a few builtins are already there. All you need to do is implement the code to execute commands that require spawning a new process (and a few other things to print exit statuses nicely).

Let's begin by compiling the program as is (make generates the executable bsh). You can ignore the warning about an unused function; you'll rectify that soon enough. Currently, all the shell can do is execute a few builtin commands: cd, chdir, exit. Ideally by the end of class you'll be able to execute arbitrary programs.

Now examine the code and search for the the string "TODO". Those are the places you need to write code. Spend a few minutes familiarizing yourself with the structure of the code, but you needn't audit the entire file. The most important part is where you have to create a new process to execute the command typed at the prompt.

Add Fork/Execvp

Around line 185, you'll find the place where you need to add fork and execvp handling. Go for it! For now, just focus on getting the process you want running and passing the correct arguments to it. Once that's working, move on to:

Waiting for your Children

If all you did was add fork and execvp calls, then you'll find that the output of the child gets intermingled with the shell prompt. You can clean that up by calling dowait at the appropriate time and implementing that function correctly. Do that next.


Once you have a shell that properly waits for its children, you have free reign to implement any number of features. Try your hand at any of the following (many/all of which will be part of A4).

  • Run jobs in the background: this means that the parent does not wait on its children, but runs in parallel with its children. However, when the child exits, the parent should harvest its status, and report it.
  • Use pipe and dup2 to allow handling of command pipelines.
  • Add environment handling.
  • Add any other feature you want

Wrapping Up

Assignment 4 asks you to complete the implementation of a shell that is more advanced than what we did here. However, you have all the basics now to get started. If you wish to reuse any code from today's exercises, be sure to cite it appropriately in your README.

Please take a few moments to fill out this survey.