Programming Assignment: Ada

Due: April 21

The solution to the readers-writers problem discussed in class suffers from the defect that it suffers from starvation on writers. That is, if there is an endless stream of requests from readers, where reading requests keep before the previous one finishes, then a writer is never allowed to proceed. An analogy is traffic across a one-lane bridge; if there is an endless stream of traffic in one direction, then cars going in the opposite direction can never proceed.

Part I: (2 points out of 10)

Construct a driver, analogous to runreader.adb which demonstrates this form of starvation for the code in and readerwriter.adb . Specifically, you should create five concurrent tasks A, B, C, D, and E. A and B do 20 reads each; C, D, and E do 1 write. Using delay statements, arrange things E does its write first; then A and B start reading; then C and D try to write while A and B are performing their first read, with C's attempt coming before D's attempt; and so that, whenever A or B finishes one read, it starts the next read before the other one completes. For this part of the assignment, the code in readerwriter.adb must not be modified.

Part II: (8 points out of 10)

Modify the code for readerwriter.adb so as to fix the starvation problem. A process that wants to write should "announce" this by setting a Boolean flag, which is unset when it finishes writing. If the flag is set, no new reading request is allowed to start. Moreover, writers must be handled in first-come, first-served order.

For instance in the example of part I, what you want to guarantee is that (a) C gets to write before A and B start their second read and before D gets to write; (b) D gets to write before A and B start their third read.

There are a number of different possibilities for what should happen when C finishes its write:

Any of these is OK, but (iii) is probably the easiest to code.

The modified code should run correctly for the driver that you wrote in part I.

Submitting the assignment

Email new versions of runreader.adb and readerwriter.adb to Samantha.