CS 1550: Introduction to Operating Systems

Programming Assignment #1: Writing a Shell

Due Date: Sunday, September 16, 2007, 11:59 PM

The Basics

The goal of this assignment is to get everyone up to speed on system programming and to gain some familiarity with the system call interface. A secondary goal is to use some of the programming tools provided in the Unix environment. In this assignment you are to implement a Unix shell program. A shell is simply a program that conveniently allows you to run other programs. Read up on your favorite shell to see what it does.

The requirements for completing this assignment successfully are described under the Details section below. We’ve provided some code to help with parsing the input. Remember that this is meant to be your own work; please do not use any code that you didn’t write or we didn’t provide for you to use.

The Rest

You are provided with files called lex.c and myshell.c which contains some code that uses getline(), a function provided by lex.c to get and parse a line of input. getline() returns an array of pointers to character strings. Each string is either a word containing the letters, numbers, ., and /, or a single character string containing one of the special characters: ( ) < > | & ;.

To compile lex.c, you have to use flex: "flex lex.c". This will produce a file called lex.yy.c. lex.yy.c and myshell.c must then be compiled and linked in order to get a running program. In the link step you also have to use "-lfl" to get everything to work properly. Use gcc for the compilation and linking.

The Details                         

Your shell must support the following:

1.       The internal shell command "exit" which terminates the shell.

Concepts: shell commands, exiting the shell

System calls: exit()

 

2.       A command with no arguments
Example: ls
Details: Your shell must block until the command completes and, if the return code is abnormal, print out a message to that effect.
Concepts: Forking a child process, waiting for it to complete, synchronous execution
System calls: fork(), execvp(), exit(), wait()

 

3.       A command with arguments
Example: ls –l
Details: Argument 0 is the name of the command
Concepts: Command-line parameters

4.       A command, with or without arguments, whose output is redirected to a file
Example: ls -l > foo
Details: This takes the output of the command and put it in the named file
Concepts: File operations, output redirection
System calls: freopen()

 

5.       A command, with or without arguments, whose output is piped to the input of another command.
Example: ls -l | more
Details: This takes the output of the first command and makes it the input to the second command
Concepts: Pipes, synchronous operation
System calls: pipe(), freopen() or dup2()

Note: You must check and correctly handle all return values. This means that you need to read the man pages for each function to figure out what the possible return values are, what errors they indicate, and what you must do when you get that error.

You do not need to handle multiple pipes or redirections combined with pipes on the same commandline. You do, however, need to handle arguments for both processes on the pipe commandline.

The Environment

We’ve set up a machine to do the term projects on, which can be reached by ssh at thot.cs.pitt.edu. You can log in with your Pitt account.

The Submission

When you’re done, create a gzipped tarball of your source and compiled shell executable and copy them to:

 ~jrmst106/submit/

Make sure you name the file with your username, and that you have your name in the comments of your source file.

Note that this directory is insert-only, you may not delete or modify your submissions once in the directory. If you’ve made a mistake before the deadline, resubmit with a number suffix like abc123_1.tar.gz

The highest numbered file before the deadline will be the one that is graded, however for simplicity, please make sure you’ve done all the work and included all necessary files before you submit.