CMPS 111: Introduction to Operating Systems

Programming Assignment #1: Writing a Shell

Due Thursday, April 17, at midnight.

Remember: your programming assignment must be turned in online.

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.

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. To compile, do "gcc -c lex.yy.c" and "gcc -c myshell.c". To link, use this command, "gcc -o myshell lex.yy.o myshell.o -lfl". Try it now with my versions of the files.


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, executed in the background using &.
    For simplicity, assume that if present the & is always the last thing on the line.
    Example: xemacs &
    Details: In this case, your shell must execute the command and return immediately, not blocking until the command finishes.
    Concepts: Background execution, signals, signal handlers, processes, asynchronous execution
    System calls: sigset()

  5. 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()

  6. A command, with or without arguments, whose input is redirected from a file
    Example: sort < testfile
    Details: This takes the named file as input to the command
    Concepts: Input redirection, more file operations
    System calls: freopen()

  7. [Extra credit] 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()
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.


What to turn in

A compressed tar file of your project directory, including your design document. You must do "make clean" before creating the tar file. In addition, include a README file to explain anything unusual to the TA — testing procedures, etc. Your code and other associated files must be in a single directory so they'll build properly in the submit directory.

REMEMBER: Do not submit object files, assembler files, or executables. Every file in the submit directory that could be generated automatically by the compiler or assembler will result in a 5 point deduction from your programming assignment grade.


sbrandt@cse.ucsc.edu