There is 1 thread that writes to a file, and a number of other threads that can
ID: 3810582 • Letter: T
Question
There is 1 thread that writes to a file, and a number of other threads that can simultaneously read the file, but never the readers and the write together (e.g., at one time, 3 reader threads are running, at another time the writer thread is running). Write pseudo code using semaphores for both the Reader() and Writer() functions. Remember that there is no data dependency between the readers and the writer (i.e., it is not a producer-consumer problem), the only condition is that they do not happen at the same time. Hint: The first reader to get access waits until the ongoing writer (if any) finishes, performs the read operation and leaves. The last reader, just before leaving, should give control/access back to the writer (if any) waiting. Think about the initial values of the semaphores
Explanation / Answer
Three variables will be used to implement the problem: mutex, wrt, readCount. Semaphore mutex is used to ensure mutual exclusion when readCount is updated. Semaphore wrt is used by both readers and writers. And, readCount is used to tell the number of processes performing read operation in the critical section, initially its value will be 0.
Writer:
- It requests to enter in critical section.
- If request granted, i.e. wait() gives a true value, it enters and performs the write. If not granted, it keeps on waiting.
- It exits the critical section.
Pseudo Code for writer is:
do {
// writer requests for critical section
wait(wrt);
// performs the write
<critical section>
// leaves the critical section
signal(wrt);
} while(true);
Reader:
- It requests to enter in critical section.
- If request granted : it increments the countReader inside the critical section. If the entered reader is the first reader, it locks the 'wrt' semaphore to restrict any writer to enter inside critical section. It then signals mutex as any other reader is allowed to enter while others are already reading. After performing reading opration, it exits the critical section. While exiting, it checks if no more reader is inside i.e. readCount is 0 again, it signals the semaphore 'wrt' that writer can enter the critical section for performing write operation.
- If not granted then it keeps on waiting.
Psedo Code fore reader is:
do {
// reader requests for critical section
wait(mutex);
// number of readers will be increased by 1
readCount++;
// there is atleast one reader in the critical section, this ensure no writer can write if there is even one reader
if (readCount==1)
wait(wrt);
// other readers can enter this critical section while the current reader is inside or reading
signal(mutex);
// current reader performing reading operation
wait(mutex);
// a reader wants to leave
readCount--;
// if all readers are done with reading operation then readCount will become 0 and now writer can enter into critical section.
if (readCount == 0)
signal(wrt);
// reader leaves the critical section.
signal(mutex);
} while(true);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.