Due Date: Monday Oct. 27

1. Producer/Consumer problem. In class solutions were presented for the producer/consumer problem assuming a single producer and a single consumer. Implement a solution that supports multiple producers and multiple consumers sharing a common buffer . The program should satisfy the following constraints.

A. Consumers should block if there is no data available, i.e. the buffer is empty.
B. Producers should block if the buffer is full.
C. Data must not be lost by simultaneous execution of producers.
D. Each data item must be read by exactly one consumer. It is an error for simultaneously executing consumers to read the same data item.

The producer threads should each generate a sequence of non-negative random numbers and write them to the buffer. The consumer threads should read the random numbers and accumulate a total. When all of the data has been processed by the consumers, the totals for each thread should be output and a grand total reported. For debugging purposes, each producer thread should also compute total of the numbers it produces, so that the grand total may be verified. The program should take an argument specifying the length of the random number sequences generated by the producer threads. Note that you will have to come up with some method for the consumers to discover that the producers have finished producing data.

2. The Reader/Writer locks discussed in class are a UI Threads feature but are not part of the POSIX Threads standard. One reason for this is that Reader/Writer locks can be implemented using the POSIX synchronization primitives. Create such an implementation and a test program to verify that your implementation functions correctly.

Recall that Reader/Writer locks satisfy the following conditions:

A. Any number of threads may simultaneously hold the lock for reading as long as no thread holds it for writing.

B. Only one thread may hold the lock for writing and this prevents other threads from acquiring it for reading.

C. If a lock is held for reading and a thread is blocked attempting to acquire it for writing, then other threads attempting to acquire it for reading will block. That is, writers have priority over readers.

Be sure that your test program verifies that your implementation satisfies the last condition.