CSE150 Operating Systems (Spring semester 2008): project 1

In this project you will write a simple Unix shell in C. The project can be done in groups of up to 2 students and is due April 30. The shell should have the following functionality:

A shell can do far many more things, but you are only required to do the ones above. Your basic program should follow fig. 1-19 in the textbook; then, expand it appropriately to handle pipelines, run a process in the background, parse the command line, etc. You will need to understand well the following system calls (or similar ones): fork, execve, dup, pipe, open, close, exit, wait. To see their description and parameters, read their manual page (section 2 for system calls: man 2 fork; section 3 for library functions, e.g. man 3 perror).

The crucial bit is to get a pipeline A | ... | Z to work. First focus on understanding the simple case A | B. To get a pipeline A | B to work, you'll need to:

  1. Define two file descriptors: int fd[2];
  2. Create a pipe: pipe(&fd[0]);
  3. Call fork() to create another process; now both processes have the same address space and file descriptors, including the pipe's.
  4. For A, connect its output to the pipe's input: close(fd[0]); close(STDOUTPUT); dup(fd[1]); close(fd[1]);. Then execve A. Note that dup uses the lowest-numbered unused descriptor for the new descriptor (this will be STDOUTPUT, i.e., 1, right after closing it).
  5. For B, a similar procedure but to read from the pipe.

For a more complex pipeline, apply this technique over a loop. Redirection works similarly but opening a file instead of a pipe. Other issues (e.g. how to store the command line, parse it, etc.) are left up to you; keep things simple.

Examples to try with your shell:

Your mysh should produce (almost) the same result as running them from the system shell (sh, bash, etc.).

You can use strace and ltrace to trace the calls to system calls and library functions.

What you have to submit is a single file project1.tar.gz, by email to the TA, containing:


Miguel A. Carreira-Perpinan
Last modified: Mon Mar 31 23:50:39 PST 2008

UC Merced | EECS | MACP's Home Page