Figure 2.19:
A solution to the readers and writers problem.
|
- Models access to a database; many competing processes wishing to read and write
- It is acceptable to have multiple processes reading the database at the same time, but if one process is updating (writing) the database, no other process may have access to the database, not even readers
- Write a program for the readres and writers
- the solution presented in Fig. 2.19, the first reader to get access to the database does a down on the semaphore db
- Subsequent readers increment a counter, rc
- As readers leave, they decrement the counter and the last one does an up on the semaphore, allowing a blocked writer
- bug:
- As long as at least one reader is still active, subsequent readers is admitted
- As a consequence of this strategy, as long as there is a steady supply of readers. they will all get in as soon as they arrive
- The writer will be kept suspended until no reader is present
- The solution is that when a reader arrives and a write is waiting, the reader is suspended behind the writer instead of being admitted immediately (less concurrency, lower performance)
2004-05-25