![]() ![]() In particular, what sort of rules should we set around shared memory access, so that actions are safe but also efficient? In concurrency, one of the common challenges is balancing who has access to a shared variable. ![]() In fact, the solutions implied by both problem statements can result in starvation - the first one may starve writers in the queue, and the second one may starve readers.4 Want to Learn More? Readers-Writers Problem Overview Their sole purpose is to avoid race conditions on the readers and writers while they are in their entry or exit sections. The rmutex and wmutex are used in exactly the same way as in the first solution. All subsequent readers will hang up at the readtry semaphore waiting for the writers to be finished with the resource and to open the gate by releasing readtry. It will then take control over the resource as soon as the current reader is done reading and lock all future readers out. They are only able to do so after first locking the readtry semaphore, which can only be done by one of them at a time. The resource semaphore can be locked by both the writer and the reader in their entry section. However, the writer will not be able to access the resource until the current reader has released the resource, which only occurs after the reader is finished with the resource in the critical section. So the writer will wait for the reader to release the readtry and then the writer will immediately lock it for itself and all subsequent writers. On the other hand, if a particular reader has locked the readtry semaphore, this will indicate to any potential concurrent writer that there is a reader in the entry section. The reader must wait for the last writer to unlock the resource and readtry semaphores. No reader can engage in the entry section if the readtry semaphore has been set by a writer previously. The very last writer must release the readtry semaphore, thus opening the gate for readers to try reading. Only the first writer will lock the readtry and then all subsequent writers can simply use the resource as it gets freed by the previous writer. The writers on the other hand don't need to lock it individually. This is accomplished by forcing every reader to lock and release the readtry semaphore individually. In this solution, preference is given to the writers. ![]() Semaphore resource = 1 semaphore rmutex = 1 readcount = 0 /* resource.P() is equivalent to wait(resource) resource.V() is equivalent to signal(resource) rmutex.P() is equivalent to wait(rmutex) rmutex.V() is equivalent to signal(rmutex) */ writer () This is also called readers-preference, with its solution: This is the motivation for the first readers–writers problem, in which the constraint is added that no reader shall be kept waiting if the share is currently opened for reading. It would be foolish for R 2 to wait until R 1 was done before starting its own read operation instead, R 2 should be allowed to read the resource alongside R 1 because reads don't modify data, so concurrent reads are safe. However, this solution is sub-optimal, because it is possible that a reader R 1 might have the lock, and then another reader R 2 requests access. It is possible to protect the shared data behind a mutual exclusion mutex, in which case no two threads can access the data at the same time. ![]() Suppose we have a shared memory area (critical section) with the basic constraints detailed above. The basic reader–writers problem was first formulated and solved by Courtois et al. A readers–writer lock is a data structure that solves one or more of the readers–writers problems. (In particular, we want to prevent more than one thread modifying the shared resource simultaneously and allow for two or more readers to access the shared resource at the same time). Some threads may read and some may write, with the constraint that no thread may access the shared resource for either reading or writing while another thread is in the act of writing to it. There are at least three variations of the problems, which deal with situations in which many concurrent threads of execution try to access the same shared resource at one time. In computer science, the readers–writers problems are examples of a common computing problem in concurrency. ( Learn how and when to remove this template message) ( November 2016) ( Learn how and when to remove this template message) Please help improve it to make it understandable to non-experts, without removing the technical details. This article may be too technical for most readers to understand. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |