CSCI 3323 (Principles of Operating Systems), Fall 2020:
Homework 1

Credit:
25 points.

Reading

Be sure you have read, or at least skimmed, Chapter 1.

Programming Problems

Do the following programming problems. You will end up with at least one code file per problem. Submit your program source (and any other needed files) by sending mail to my TMail address with each file as an attachment. Please use a subject line that mentions the course and the assignment (e.g., “csci 3323 hw 1” or “O/S hw 1”). You can develop your programs on any system that provides the needed functionality, but I will test them on one of the department's Linux machines, so you should probably make sure they work in that environment before turning them in. (For this assignment, “system that provides the needed functionality” means something UNIX-like.)

  1. (25 points) Figure 1-19 in chapter 1 of the textbook presents pseudocode for a simple command shell. Your mission for this problem is to turn this into a C or C++ program that runs on a Linux system. Your program should repeatedly prompt the user for a command and command-line arguments and then run the given command with the given arguments. (Do not start writing code until you read the whole assignment. I'm providing starter code that takes care of the parts that are tedious to program in C.) You can require that the user give the full path for the command (this is easier to implement and reasonable in context), and you don't have to do sophisticated parsing of the command-line arguments (such as wildcard expansion, recognition of environment variables, etc., etc.). The program, however, should do something sensible (such as displaying an error message) if it cannot run the command, and it should stop on reaching EOF on standard input so that it can accept commands from either a file or the keyboard (where pressing control-D signals EOF). Here is a sample execution:


    $ ./shellsketch 
    next command?
    /bin/ls
    Makefile     shellsketch-starter.c  test-input.txt
    shellsketch  shellsketch.c	    typescript
    
    next command?
    /bin/echo ab cd ef gh
    ab cd ef gh
    
    next command?
    junk
    cannot execute command: No such file or directory
    
    next command?
    /bin/ls junk
    /bin/ls: cannot access junk: No such file or directory
    
    next command?
    

    Turning the pseudocode into code mostly involves defining appropriate data structures for the variables in the pseudocode and replacing the type_prompt and read_command functions with appropriate real code. You may recall that anything dealing with text strings is apt to be tedious and messy in C, but my starter code takes care of most of that for you, including some debug prints to track what it is doing:

    simple-shell.c.

    If you compile with the default version of gcc, you may need the -std=c99 flag.

    Your first step should probably be to read the man page for execve -- carefully -- to see what arguments it expects, and then figure out what you need to do to turn what the starter code produces (an array of pointers to strings) into suitable input to execve. (You should not need to do much.) Recall (or note) that man pages for functions tell you what if any #include directives you need to include in your code.

    For extra credit (up to 5 points), you can add more functionality (searching a path for the command, doing more sophisticated parsing of inputs, exiting when the user types “exit”, etc.). If you do, add something to the comments in the code describing your added functionality. If you insist, you can even rewrite any or all of the starter code in C++. Whatever changes you make, however, be sure your program will still work with input that is valid for the starter code. (E.g., you could implement some sort of search path, but if you, be sure the program still accepts a full path for the command.)

    C tip: Get in the habit of compiling with the -Wall flag and paying attention to warning messages. Sometimes warning messages really are just warnings you can ignore, but often they are signs of problems you should fix. Code that produces warnings with compiled with -std=c99 -Wall -pedantic -O will lose points.

Pledge

Include the Honor Code pledge or just the word “pledged”, plus at least one of the following about collaboration and help (as many as apply).1Text in italics is explanatory or something for you to fill in. For programming assignments, this should go in the body of the e-mail or in a plain-text file pledge.txt (no word-processor files please).

Essay

Include a brief essay (a sentence or two is fine, though you can write as much as you like) telling me what if anything you think you learned from the assignment, and what if anything you found found interesting, difficult, or otherwise noteworthy. For programming assignments, it should go in the body of the e-mail or in a plain-text file essay.txt (no word-processor files please).



Footnotes

... apply).1
Credit where credit is due: I based the wording of this list on a posting to a SIGCSE mailing list. SIGCSE is the ACM's Special Interest Group on CS Education.



2020-09-16