Homework 4

22C:116, Fall 2001

Due Monday Sep 17, 2001, in class

Douglas W. Jones

Always, on every assignment, please write your name legibly as it appears on your University ID and on the class list!

  1. Background There is an example thread manager in http://homepage.cs.uiowa.edu/~dwjones/opsys/threads/ This assignment asks you to extend the second example given there, an example that demonstrates producer-consumer synchronization.

    As given, the queue is represented by a pair of semaphores that count free space and data items in the queue. There is no actual queue because no actual data is moved. There are, however, two producer threads and two consumer threads.

    Part A: When you run this example, it outputs the message "Thread manager terminated, possible deadlock!" Explain the nature of the problem with the example code.

    Part B: Explain why the example code outputs the termination message before it outputs the text produced during the execution of the program prior to termination.

    Part C: How should the thread manager itself be modified in order to produce its output in the correct order?

    Part D: Modify the program so that, in addition to the semaphores, there is a single global queue of integers, with head and tail pointers, where the capacity of the queue is the same as the initial value of the free semaphore.

    Part E: Modify the program so that the producer and consumer threads call your enqueue and dequeue functions. The producers should enqueue the successive values of their loop control variable, and each consumer should count the number of items it consumes and report this.

    For parts D and E, turn in a listing of the code for your solution combining both parts.