CS 332: Operating Systems

Takehome exam, due on paper at 8:30AM Friday, May 23, 2008.

This is a an exam. You may consult your notes, any book, and the Internet. You may not speak with any person other than Jeff Ondich, electronically or otherwise, about the content of this exam. If you obtain relevant information from any source other than yourself, cite your sources clearly. Justify your answers. (Note that "justify your answers" implies "show your work.") Have fun.

  1. Threads (10 points)

    The sample program threads.c illustrates the use of POSIX threads, also known as pthreads. This particular sample uses pthread mutex variables (binary semaphores) to force strict alternation between two threads. Note that sharing memory between pthreads via global variables is considerably simpler than doing so using shmget and its pals.

    Take a look at another sample program that consists of kbcolors.c, kbhit.c, and kbhit.h. If you compile this program with "gcc -Wall -o kbcolors kbcolors.c kbhit.c -lpthread", you'll end up with a program that prints a color name every second or two. If you type a letter (r for red, b for blue, g for green, y for yellow, o for orange, or q for quit), the message changes to the new color (or the program quits if you type q). Note that the kbhit() and readch() functions are designed to enable your program to obtain keyboard input without requiring the user to hit the Enter key. If you wish to study kbhit.c, you may, but I won't be asking you to understand how it works--just how to use it.

    With these programs in hand, your job is to write a two-threaded program. One thread, which would typically be characterized as a "worker thread," should generate prime numbers until it's time to quit. The other thread should keep an eye on the keyboard, and as soon as the user types a q, shut down the program, including both threads and the parent process.

    This separation between computation and user communication makes both threads simpler, and thus easier to write, understand, and maintain.

    Call your program threads.c and submit it in the usual way. You don't need to submit copies of kbhit.h and kbhit.c.

  2. Memory (14 points)

    1. Suppose a 32-bit OS uses a paged virtual memory system with page size 32KB. Suppose the paging is managed by a two-level page table system whose top-level table has 512 entries. Suppose further that each page table entry (top-level or second-level) consists of 32 bits, and that the top-level page table is loaded into physical memory at the time the program is launched.

      • How many entries are in each second-level page table?
      • How much memory does each second-level page table require?
      • If a program uses addresses 0x20000000 through 0x20FFFFFF and addresses 0xB0000000 through 0xB0FFFFFF during its run, how many second-level page tables will need to be used, and how much total memory will those second-level page tables require?
    2. Consider the strange little program addressreport.c. This program includes local variables, string literals, dynamic memory allocation via malloc, and a recursive function whose stack frames are made artificially large by a big fat array of characters. The purpose of the program is to give you an idea of where in virtual memory all of these sorts of objects are placed by the compiler and run-time system on whatever computer you use to test the program. For this exercise, study, compile, and run the program on a Unix-based system (e.g. Linux or Mac), and then:

      • Draw a picture of this program's virtual address space, marking the regions where the various types of objects are stored (functions, literals, the stack, etc.). Feel free to add experimental code of your own if you want to clarify any points on your picture.
      • Assume that your OS uses paged virtual memory with pages of size 4KB, and that no pages have been loaded at the time addressreport is executed. Create an annotated list of the page faults that will occur as the program runs, explaining why each page fault occurs, and when. (Your annotations don't have to be especially verbose. You're just trying to tell the page-fault story clearly enough that I can follow the steps.)
  3. Easy points (3 points)

    Please tell me a joke.

  4. Files (12 points)

    1. On a Unix system, create a file called aaa. Then create a hard link bbb to aaa (using the command "ln aaa bbb"), create a soft link ccc to aaa ("ln -s aaa ccc"), and finally delete aaa ("rm aaa"). Then:

      • What happens if you execute "cat bbb"?
      • What happens if you execute "cat ccc"?
      • Using what you know about inodes and directories in inode-based file systems, explain why "cat bbb" and "cat ccc" did what they did.
    2. Based on the description of the Unix V7 i-node as described on page 322 of Tanenbaum, what is the maximum file size? Explain your answer.

  5. Deadlock (12 points)

    1. Do problem 18 on p.464 of Tanenbaum.

    2. Do problem 22 on p.464 of Tanenbaum.

  6. Therac-25: something you should know about (10 points)

    The Therac-25 incident is arguably the worst disaster in the history of software. You can learn all about it in An Investigation of the Therac-25 Accidents, Nancy Leveson and Clark S. Turner, IEEE Computer, Vol. 26, No. 7, July 1993, pp. 18-41. (Here's another link to the same paper.) If you wish, you may also look for other information on Therac-25 in books or on the Web.

    1. What was the Therac-25?
    2. How many people were killed by the Therac-25? How many were injured?
    3. Briefly list the design flaws in the Therac-25 that caused the injuries and deaths.
    4. One of the design flaws involved a race condition. As best you can from the information available to you, list the sequence of events that were required to cause the race, and explain the practical results of the race.