Question 3 -- 20 points


Part 1

Assume that you are working with a preemptive or multiprocessor thread system. You are writing a program with a team of programmers, and your job is to write five procedures:
  1. initialize()
  2. begin_reading()
  3. end_reading()
  4. begin_writing()
  5. end_writing()
The way they work is as follows. When the system is initialized, initialize() is called. There is a global repository of shared data. Whenever a thread wants to read from the repository, it first calls begin_reading(). When it is finishes, it calls end_reading(). Whenever a thread wants to modify the repository, it first calls begin_writing(), and when it finishes, it calls end_writing(). Write the code for these procedures so that: Your code does not have to prevent starvation but it must not deadlock.

Part 2

Describe how you would alter your code so that no more than 10 readers may access the data at a time. This should only take a few modifications -- say what those are and where in the code they should go.

Part 3

Discuss starvation in your code -- name all the ways in which threads may starve. Make no assumptions about how threads are unblocked from the synchronization operations.
You may use any of the following subroutines. Note that these are not continuation-based thread operations -- simply regular thread operations.

You will obviously have to use global variables.


Hint: For part 1, I used monitors and condition variables.