Question: Synchronization : Reader Writer problem 1) First readers-writers problem (favors readers) -No reader should be kept waiting unless a writer has already been granted permission
Synchronization : Reader Writer problem
1) First readers-writers problem (favors readers)
-No reader should be kept waiting unless a writer has already been granted permission to use the object.
-A reader that arrives after a waiting writer gets priority over the writer.
=> coding (solution):
int readcnt; /* Initially 0 */
sem_t mutex, w; /* Both initially 1 */
void reader(void)
{
while (1) {
P(&mutex);
readcnt++;
if (readcnt == 1) /* First in */
P(&w);
V(&mutex);
/* Reading happens here */
P(&mutex);
readcnt--;
if (readcnt == 0) /* Last out */
V(&w);
V(&mutex);
}
}
void writer(void)
{
while (1) {
P(&w);
/* Writing here */
V(&w);
}
}
2) Second readers-writers problem (favors writers)
-Once a writer is ready to write, it performs its write as soon as possible
-A reader that arrives after a writer must wait, even if the writer is also waiting.
=> coding...
How to implement second reader-writers problems (favors writer)? (reference solution one)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
