reader-writer problem

The Dining Philosophers problem is useful for modeling processes such as competing problems with mutually exclusive access to limited resources, such as I/O devices. Another well-known problem is the reader-writer problem (Courtois et al., 1971), which establishes a model for database access. For example, imagine an airline reservation system in which there are many competing processes trying to read and write data. It is acceptable for multiple processes to read the database at the same time, but if one process is updating (writing) the database, all other processes cannot access the database, not even a read operation. The question here is how to program readers and writers? Figure 2-47 shows one solution.

In this solution, the first reader performs a down operation on the semaphore db . Subsequent readers simply increment a counter rc. When readers leave , they decrement this counter, and the last reader performs an up on the semaphore , allowing a blocked writer (if one exists) to access the database.

In this solution, there is an implicit condition that needs to be annotated. Suppose a reader is using the database and another reader comes. There is no problem with having two readers at the same time, the second reader is allowed in. The same is allowed if a third and more readers come.

Now, suppose a writer arrives. Since the writer's access is exclusive, the writer cannot be allowed to enter the database and can only be suspended . As long as there is one more reader active, subsequent readers are allowed in. The result of this strategy is that if there is a steady stream of readers, those readers will be allowed in as they arrive. The writer is always suspended until there are no readers . If new readers come in, say, every 2 seconds, and each reader takes 5 seconds to complete its work, the writer never has a chance.

To avoid this, the program can be written slightly differently: when a reader arrives and a writer is waiting, the reader is suspended after the writer, rather than being allowed in immediately . In this way, if a writer arrives with a working reader, the writer only has to wait for the reader to finish, not for the readers that come after it. The disadvantage of this solution is that it is less concurrent and efficient. A writer-first solution is given by Courtois et al. See his paper for details.

 

Figure 2-47 A solution to the reader-writer problem



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325766688&siteId=291194637