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:
- initialize()
- begin_reading()
- end_reading()
- begin_writing()
- 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:
- If no thread is modifying the repository, any number of reader
threads may be reading from the repository.
- Only one thread may modify the repository at a time.
- If a thread is modifying the repository, no threads may read from it.
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.
- mon_t mon_create() -- creates and initializes a monitor.
- cv_t cv_create(mon_t m) -- creates and initializes a condition
variable.
- mon_enter(mon_t m), mon_exit(mon_t m) -- enter and exit
a monitor.
- cv_wait(cv_t cv), cv_signal(cv_t cv) -- block on or
signal a condition variable.
- gsem_t gsem_create(int val) -- create a general semaphore.
- gsem_P(gsem_t g),
gsem_V(gsem_t g) -- standard P and V.
- bsem_t bsem_create() -- create a binary semaphore with an
initial value of one.
- bsem_P(bsem_t g),
bsem_V(bsem_t g) -- standard P and V.
Hint: For part 1, I used monitors and condition variables.