Assignment 11, due Apr. 17

Part of the homework for 22C:112, Spring 2009
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

Always, on every assignment, please write your name legibly as it appears on your University ID and on the class list! All assignments will be due at the start of class on the day indicated (usually a Friday). The only exceptions to this rule will be by advance arrangement unless there is what insurance companies call "an act of God" - something outside your control. Homework must be turned in on paper and in class! Late work may be turned in to the teaching assistant's mailbox, but see the late work policy. Never push late work under someone's door!

  1. Background:

    The file http://homepage.cs.uiowa.edu/~dwjones/opsys/threads/source.txt contains the source code for a thread manager written in C. This is documented in http://homepage.cs.uiowa.edu/~dwjones/opsys/threads/. Note that this is fragile code, not written to be portable and it may not work under the current crop of C compilers. This question does not depend on the code working.

    Note that in c, setjmp(&b) saves the current program counter and activation record pointer in b a jmp_buf (jump buffer), and longjmp(b,v) restores the program counter and activation record pointer from the jump buffer, which means it returns to the return point of the setjmp() call that initialized that jump buffer. When setjmp() returns from its own call, it returns zero. When it returns as a result of call to longjmp(b,v), it returns the value of the parameter v, which should be nonzero.

    Historically, longjmp() and its companions were included in C to allow for jumps to exception handlers.

    a) Explain how thread_relinquish() in the thread package transfers control from one thread to another. (0.5 points)

    b) Explain why thread_exit() cannot simply say free(current). Instead, it does some elaborate foolishness with mem_to_free and longjmp. (0.5 points)

    c) Suppose you wanted thread_signal() to immediately transfer control to the thread that the signal awakened instead of continuing to run in the calling thread until it relinquishes. Modify thread_signal to do this. (1.0 points)

  2. Background: Consider a driver for a serial output port. The driver includes an interrupt service routine, a queue of characters to be output, and a user interface routine that enqueues characters in the queue. There may be multiple processes competing to enqueue output to this printer.

    a) When the queue is empty, interrupts are disabled; when a character is enqueued, the user interface routine enables interrupts. When the queue is full, the user interface routine uses a semaphore to wait for space in the queue. Explain this semaphore: What range of values does its counter take on? Exactly where in the driver is the code to wait on this semaphore and where is the code to signal it? (0.5 points)

    b) If two or more processes try to output data to the device at the same time, there are potential problems. Explain how a semaphore can be incorporated into the driver to solve this problem. Where is the wait on this semaphore? Where is the signal? What range of values does its counter take on? (0.5 points)