CS 1550: Introduction to Operating Systems

Programming Assignment #1: Writing a Shell

Due Date: Friday June 10th

Part-1 of this project is for you to practice with processes and the UNIX shell.

Part-2 below is what you need to do for this project. The requirements are listed in part-2.


I. Part-1: Practice--Not required

The goal of this part of the 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 simply the first two points in described under Details. If you want to experiment further, try implementing all functions, using the skeleton code provided. For completing the basics it is sufficient to read input lines from a loop and to take the first word written.

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. Try it now with my versions of the files.

A Makefile for this assignment is provided. (Download this file and try not to cut and paste as this could have an adverse effect on the formatting of the file).


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

Your shell can also support the following:

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

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

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

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

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

 

Experiment with this assignment and get familiar with system calls and programming.


A Sample Solution for part-1:

After you have attempted to write your own shell, or if you want a sample to help understand a problem you ran into, then you may download this version of the myshell.c (TBA soon) source file.

This part-1 of the assignment and solution was provided by Prof. Scott A. Brandt 

Also from Profs. Daniel Mosse, Ahmad Amer, & Jose Brustoloni.


II. Part-2: required part of this project. The specifications and requirements are listed below:

This part of the assignment is to familiarize you with the concept of processes. Using the UNIX environment, you will create/modify a shell program. This shell will launch processes, allow them to run in the background, and permit a user to monitor the processes that have been run.

It is based on part1 which extends it. You should familiarize yourself with the requirements of part1, and download, build, and run the solution sample of part1. Your task is to extend the solution provided to allow for the monitoring of processes. Your shell should provide the following commands based on the csh/sh/tcsh/bash shells. You should type "man jobs" or other related command to see a description of the command. The timex command takes an application (sys call) as an argument. The jobs command takes no argument. The other commands take one argument, a job id number.

Files are available here: lex.yy.c myshell.c. Be sure to save the files as text files. You can use the following command line to build an executable called myshell. gcc -o myshell myshell.c lex.yy.c -lfl

jobs - lists the jobs (more feedback1, 2)
wait - hold until all jobs are done (feedback)
notify - wait until job is done (feedback)
stop - cancels the job
fg - turns background job into foreground job
suspend - turns active background job into a suspended background job
bg - turns suspended background job into active background job
timex - reports time usage of process

UNIX-Shells Commands & feedback

Your grade will be based on the successful execution of the tasks.

  • D (65%) jobs
  • C (75%) jobs + wait + notify
  • B (85%) jobs + wait + notify + stop
  • A (95%) jobs + wait + notify + stop + fg + bg + suspend
  • A+ (105%) jobs + wait + notify + stop + fg + bg + suspend + timex  (timex is extra credit--5 points=105).

    You are to hand in a HARD COPY OUTPUT of your programs, any scripts of your program execution, and any sample data files you used. These should all be submitted at the beginning of the first lecture after the electronic submission due date. That is at the start of lecture on Monday June 13th.

     

  • Due date: Friday June 10th by 11:59pm (5-bonus-points if project gets 95% or above).

                        Submission on Tuesday June 13th by 11:59pm is accepted without late penalty.

    What to turn in:

    1. Short description (less than a page) of: the main challenge of this project and what was most beneficial to your learning.
    2. Pack all the project files to a FirstName_LastNameProj1.zip file, using WinZip or a similar application.
    3. Submit the zipped package by using the computer science department's anonymous FTP server (cs.pitt.edu/incoming/CS1550/khalifa or ftp://ftp.cs.pitt.edu/incoming/CS1550/khalifa/). Please make sure to submit to the project number directory.

      Submission must be before the due date, otherwise, it will be considered late. Once you submit, you can't take the file back! Therefore, please submit your final program and only one time.

    Good luck.



  • This part reference (M. Biggrig).