Homework 3: Write your own shell

The programming part

For this assignment you are to write a simple program that will act as a shell. The program shall:

  • Display a command prompt and read in a command line from the user(the prompt must be CS361 >, otherwise it cannot be detected by the test script)
  • Parse the command line into arguments, creating an array of character pointers, where array[0] points to the actual command and rest of the array elements point to the arguments to the command (Similar to main()’s argv[])
  • Fork off a child and have the child load the requested program by passing the argument vector created in step 2 to exec() family of system calls. The parent should report the PID of the child before proceeding to the next step.(report means you are required to return strings contain “pid” and “status”, e.g. pid:11111 status:0)
  • Wait for the child to complete executing and report why it ended (exited or uncaught signal) and its exit value if available.
  • Repeat for first step forever till user enters the command exit
  • Your shell should also support basic I/O redirection line the unix shell.
    • $ command > filename Redirects the output of command to filename. The existing contents of filename are overwritten.
    • $ command >> filename` Redirects the output of command to filename. The output from command is appendend to contents of filename. Existing contents are not overwritten.
    • $ command < filename Command reads its input from filename instead of from stdin.
  • Your shell should handle the following signals:
  • SIGINT - Generated by Ctrl-C. This signal allows a user to teminate a running program. Your shell should not exit when user presses Ctrl-C but simply report that SIGINT signal has been received by the shell.(you are required to return strings containing “catch sigint”)
  • SIGTSTP - Generated by Ctrl-Z. Your shell should not exit when user presses Ctrl-Z but simply report that SIGTSTP signal has been received by the shell.(you are required to return strings containing “catch sigtstp”)
  • The shell need not support background processes or running more than one child at a time.

your personal repository

Note that there is no skeleton code for the homework. Going to lab sections is highly advisable, as the TA has been and will be explaining basics of getting started with a shell. Both the lab section code, as well as code found in the book or book slides, are “fair game” from which to begin your coding. Previous solutions, or other students’ work, are off limits and any cheating will be prosecuted to the fullest extent of university rules. There are solutions on the Internet. If we find you using them, we will give you an F in the class. Don’t look at them, don’t use them. It is not worth it.

Template

WHen you turn in the assignment, the TA should be able to compile your program by running gcc -o hw3 hw3.c in the hw3 directory(Case sensitive). You are free to include any files e.g. hw3.h, it just has to compile using the above command.

Grading

Grading will be done automatically using a script. We will make a version of this script available one week before the deadline: if you wait to start until then, you probably won’t be able to get it done, even with access to the script.

Due Date

This assignment is due Friday, February 19, at 12:30 PM. See the syllabus for the late turnin policy. This assignment is worth just as much as every other homework, and they will only get harder from here, so getting as much credit on it as possible is important (don’t turn it in late!).