Question: Readers-Writers Problem Another classic synchronization problem Consists of a set of threads accessing some shared data Readers - threads that only reads the data
Readers-Writers Problem Another classic synchronization problem Consists of a set of threads accessing some shared data Readers - threads that only reads the data o Writers threads that modifies the data Typical example Database access, linked list update and lookup Synchronization requirements Many readers can perform reading concurrently Reading is prohibited while a writer is updating Only one writer can perform updating at any time This is a solution (pseudocode) to the Readers-Writers problem given in the class. void Reader (void) { sem_t semMutex, readMutex; int readcount; void init (void) { } sem_init(&semMutex, 0, 1); sem init (&readMutex, 0, 1); readcount = 0; void Writer (void) { } sem_wait (&semMutex); do_writing ( ) ; sem_post(&semMutex); } sem_wait (&readMutex); readcount++; if (readcount == 1) sem_wait (&semMutex); sem_post (&readMutex); do_reading (); sem_wait (&readMutex); readcount--; if (readcount == 0) sem_post(&semMutex); sem_post(&readMutex); The above solution is implemented by using semaphores. Design another solution of the Readers- Writers problem by using condition variables and mutex locks. Write down your pseudo code to implement the writer () and Reader () functions. Please include declaration and initialization of necessary variables, if any.
Step by Step Solution
There are 3 Steps involved in it
The ReadersWriters Problem is a classic example of a situation that requires synchronization in multithreaded programming to avoid the simultaneous ac... View full answer
Get step-by-step solutions from verified subject matter experts
