CS 3120 Operating Systems, Summer 2003

Assignment 1, due: Friday, May 23, 1:35pm

 

This is an exercise designed to familiarize you with C programming in Unix, forking processes and making systems calls for I/O. You will write a "shell" program in C that accepts input commands from a user, forks a child process, and executes the user's command in the child process. The shell program will be run on a Unix system (borg).

 

The Shell

 

The shell program must print a prompt, consisting of your student number, and then accept input commands (one at a time) typed by the user. The format of an input command is "command [parameters]" where [parameters] is zero or more command parameters to be passed to the program named by "command".

 

When the user types the command in response to the prompt, your shell will fork a child process, which will then use the execvp system call to replace the forked process image with a new process image running the program requested by the user. The child process must communicate the user's parameters to the requested program, so that it will run in the same way as if the user had typed the same command to one of the standard shells. This means that you will have to parse the command line parameters and pass them to execvp in the form of an array of null-terminated strings (which is used to provide the argv argument to the main function of the program to be executed). The last element of this array must be a null pointer.  If the needed executable is not found, the shell should print a message to the user indicating that the command could not be found.

 

While the child is running the user's command, the parent will do a waitpid to wait for the child process to complete. After the child completes, the parent shell process will then loop back to its command prompt, ready to accept and process another command from the user. If the user types the special command "exit", the shell process should exit.

 

The shell program should be called "myshell", and its source code should be in a file "myshell.c". Your program should be commented appropriately so the reader of the soruce code (the course T.A.) will be able to understand what the program is doing. Put your name and student number in comment lines at the top of every file.

 

Also note that you should do all input/output in your shell using low-level system calls (open, close, read, write, etc)

 

One word of warning: when testing a program like your shell which forks new processes, it is not hard for the program to get "out of control" and fork more processes than you think, or to leave sub-processes running that you are not aware of. During testing, periodically use the "ps" command to look at how many processes you have on the go, and if you find an alarming number of them, use the "kill" command to get rid of them (type "man kill" to see how to do this...) Students in this class in the past have brought borg to its knees on more than one occasion, so the tech support staff are always nervous when they know we are doing an assignment like this(!)

 

Resources

The GNU C library documentation is available on the Web, and makes an excellent reference source. In particular, the section on child processes will take you to an information page that gives a good explanation of the system calls accessible from C for forking processes and related operations. The other functions mentioned above can be found via the function index you will find at the bottom of the contents page. Another good source of information on Unix in general can be found at http://www.geek-girl.com/unix.html.

 

The GNU documentation goes beyond simple description of each system call, and contains well written explanations of how things work, complete with helpful examples. For purposes of this programming project, you will find useful information especially in the following sections:

 

Child Processes, (subsections on Creating a Process and Executing a File)

Low-Level Input/Output 

 

You will also provide your own "child' program, mychild.c.  This program should take a command line argument that is the name of a file.  It will open the disk file indicated in the argument vector and will read from the file and write to the screen.  Your shell should be able to work with this child program and should also be able to invoke standard Unix programs like "ls", "cat", "sort", and "ps", for example.

 

The code provided will go a long way towards giving you the required functionality of this assignment, but it is up to you to comment the programs fully, getting rid of any "magic numbers", naming variables with meaningful names, commenting code fragments, and fully commenting the program header itself so that the full functionality of the program is described.  This commenting is how you will be showing the marker that you understand what all the code is doing.  As you can see from marking scheme, this is an important part of the process.

 

Directions for submitting your program electronically will be given closer to the submit date.  You should pass in hard copies of your programs: myshell.h, myshell.c, mychild.h, mychild.c, makefile, README file, and a test text file (test.txt) at beginning of class on the due date.  Even if you don't get it finished, pass in your partial work and demonstrate as much as possible that you understand what you should have been doing, even if you don't quite get it working.

 

Reminder:  Late assignments will not be accepted.  In order to ensure that you have no problems with the electronic submission, it is recommended that you submit the day before.  The electronic submission will assume that all your files are correctly named (see above) and able to compile - don't leave it to the last minute to find out that you forgot something. 

 

 

Marking scheme:

 Readability: documentation(internal, README file), makefile, coding style, modularity (header files)

 10

 Robustness:  attention to error codes and memory management

 5

Assignment requirements: Correct handling of command lines, argv[] vector passing, etc.

 5

 Total

 20