A shell program

The goal of the first assignment is to give you a taste of processes, threads, timers, and signals. You'll develop a shell which provides a command line, accepts commands, executes them, and returns results. You'll also compare the overhead in process and thread creation. (A complete comparison of threads and process will be done in the class.)

A session with the shell

The following is an example session with OOS (own own shell) which you'll develop.
% oos 10
oos% date
oos% ls -l
oos% a.out 1 2 3 &
oos% foo bar
^C
oos% ^D
%

The following are the highlights of the session.

  1. This shell takes a single argument, limit. No process created in this shell should take more than limit seconds. If any process takes more time, the shell kills it.
  2. The shell executes any command the same way as a normal shell. It searches the directories listed in the environment varialble PATH.
  3. The shell is able to handle background processes. The time limit does not apply to background processes.
  4. Typing ^C kills the current command, but not the shell. The way to exit the shell is by typing ^D or exit. If the user exits the shell, the shell should kill all the background processes it had created.
  5. If there is no user input for 30*limit seconds, the shell exits on its own. The shell kills all the background processes it had created before exiting.
  6. There is no need to handle indirection (<, >) and pipes (|). But you should discuss them in your report.

Anatomy of the shell

The following are the operations done by the shell.
  1. print a prompt
  2. read the command line
  3. parse the command
  4. find the executable file for the command
  5. prepare the parameters
  6. execute the command. Execution of the command requires the folowing system calls.
    1. fork: creates a new process.
    2. exec: executes a new program. You can use any of the exec system calls.
    3. wait: wait for a child process to complete.
  7. The shell can use the system call alarm as a timer to time the execution of a process and terminate it if required.

Processes vs. threads

Threads are light-weight processes. You should convince yourself that threads are indeed light-weight by comparing the creation times. You can choose a simple task, say, printing Hello world. You can create many, say 100 or 1000, processes to to this. You can also create 100 or 1000 threads to do the same job. You can now compare the execution times of these two approaches. You can use the command time to measure execution time. You should use POSIX threads.

Code Submission

Your program should be in C. Keep all your sources and a Makefile to compile them in a tar file called shell.tar. The Makefile should create the executables
  1. oos for the shell
  2. mthread which creates and runs multiple threads
  3. mprocess which creates and runs multiple processes
Use bundleP1 on ieng9 to submit the tar file. The code should be submitted before 11:59 PM on Apr 21. Make sure you run "prep cs120s" before running bundleP1. This sets up the correct path so you can find the bundleP1 program.

Report

You should write a report of about 5 pages discussing the following aspects.
  1. The structure of your shell and the control flow pattern for signals, timer, etc.
  2. How to make the shell full-fledged, say, like tcsh? In particular, how can indirection, pipes, and environment variables be handled? (You can look at the documentation of tcsh. You can use the command man tcsh to get the manual pages.)
  3. The advantages and disadvantages of using signals as a way of interprocess communication.
  4. Comparison of process and thread creation times.
  5. Whether a shell is a critical component of an OS. Why?
You should submit the report and a self-evalution report. You report should also contain the name of the account from which the code was turned in. The report should be submitted to Allen Chu during his office hours on Friday Apr 21.

You should do this project in groups of size 3 (exactly 3). No other form of collaboration is permitted. In particular, you cannot use the code developed by others.

Resources

The best source of information is the online man pages. If you want some information on, say, threads, try man threads. If you're not successful, try apropos threads. (If you want to know what apropos is, try man apropos.

Sample code: This program creates processes. You should run this program before working on the project.

POSIX threads tutorial: Only the section "Thread creation and destruction" is directly relevant to this assignment. You can use the code in this section as part of your programs. While compiling this code, use the " -lpthread" option.

You should attend the discussion section for more information on this project.

Important

Before logging out, make sure that you have no stray or zombie processes running. You can use ps command to list the processes. If your login id is student, you can list your processes by ps -U student. You can kill the unwanted processes (one by one) using kill -9 <PID> where PID is the process id of the unwanted process.