d) The following code tries to solve the critical section problem of Readers and
ID: 3918994 • Letter: D
Question
d) The following code tries to solve the critical section problem of Readers and Writers. The condition is such that a reader cannot read while the writer writes the data and vice versa. reader) ( while (TRUE) t writer i while(TRUE Kother computing wait (mutex2) wait (RD) writeCounttti wait (mutexl) if (writeCount 1) readcount++ wait (RD) 1/3 if ( readCount??1 ) signal (mutex2) wait (WRT) wait (WRT signal (mutexl) access { resourc ) ; signal (WRT) wait (mutex2) signal (RD) writeCount-- if (wr i t eCount wait (mutex1) ?) mm r eadCount-- if(readCountO) signal (RD)//4 signal (mutex2): signal (WRT) signal (mutexl); semaphore mut x1=1, mutex2=1; semaphore RD=1.wRT :1 (i) Explain the use of each numbered statement. (2 marks) (ii) Assume that a Reader 1 is reading; Writer 1 is waiting to write and Reader 2 comes. What condition prevents the Reader 2 from reading the data? (1 mark) (iii) Assume that a writer l is writing. What condition prevents the Writer 2 from writing the data? (1 mark) (iv) What is the strategy used by the Readers and Writers in the algorithm? (1 mark)Explanation / Answer
Dear Student,
i)
1. It enters the critical section so waits the other reader threads
2. It decrements the reader count. This is basically used to check if its the first reader.
3. If its the first writer then make the other readers wait.
4. It signal the reader, after all the writers are done writing.
ii) Condition 3. As soon as the reader 1 is done reading, it signals the writer.
So writter would block the readers using condition 3 such that reader 2 cant read.
iii)wait(WRT) condition prevents the other writers from writing when the current writer is accessing the critical section.
iv) Three variables are used: mutex, wrt, readcnt to implement solution
semaphore mutex, wrt; // semaphore mutex is used to ensure mutual exclusion when readcnt is updated i.e. when any reader enters or exit from the critical section and semaphore wrt is used by both readers and writers
int readcnt; // readcnt tells the number of processes performing read in the critical section, initially 0
Writer process:
Writer requests the entry to critical section.
If allowed i.e. wait() gives a true value, it enters and performs the write. If not allowed, it keeps on waiting.
It exits the critical section.
Reader process:
Reader requests the entry to critical section.
If allowed:
it increments the count of number of readers inside the critical section. If this reader is the first reader entering, it locks the wrt semaphore to restrict the entry of writers if any reader is inside.
It then, signals mutex as any other reader is allowed to enter while others are already reading.
After performing reading, it exits the critical section. When exiting, it checks if no more reader is inside, it signals the semaphore “wrt” as now, writer can enter the critical section.
If not allowed, it keeps on waiting.
If you like this answer, give a thumbs up! If you have any doubt feel free to revert back, would be happy to help! Happy Learning
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.