Consider a situation where we have a file shared between many people.
Precisely in OS we call this situation as the readers-writers problem
Problem parameters:
Solution when Reader has the Priority over Writer
Here priority means, no reader should wait if the share is currently opened for reading.
Three variables are used: mutex, wrt, readcnt to implement solution
Functions for sempahore:
Writer process:
// Program
do {
// writer requests for critical section
wait(wrt);
// performs the write
// leaves the critical section
signal(wrt);
} while(true);
Reader process:
// Reader wants to enter the critical section
wait(mutex);
// The number of readers has now increased by 1
readcnt++;
// there is atleast one reader in the critical section
// this ensure no writer can enter if there is even one reader
// thus we give preference to readers here
if (readcnt==1)
wait(wrt);
// other readers can enter while this current reader is inside
// the critical section
signal(mutex);
// current reader performs reading here
wait(mutex); // a reader wants to leave
readcnt--;
// that is, no reader is left in the critical section,
if (readcnt == 0)
signal(wrt); // writers can enter
signal(mutex); // reader leaves
} while(true);
Thus, the semaphore ‘wrt‘ is queued on both readers and writers in a manner such that preference is given to readers if writers are also there. Thus, no reader is waiting simply because a writer has requested to enter the critical section.
Considering a shared Database our objectives are:
Basic structure of a solution
Now let’s suppose that a writer is active and a mixture of readers and writers now show up.
Who should get in next?
Or suppose that a writer is waiting and an endless of stream of readers keep showing up.
Would it be fair for them to become active?
So we’ll implement a kind of back-and-forth form of fairness:
Implementation of the solution using monitors:
10 videos|99 docs|33 tests
|
1. What is the Readers-Writers problem in computer science engineering? |
2. What is the difference between reader and writer processes in the Readers-Writers problem? |
3. How does the Readers-Writers problem impact the performance of a system? |
4. What are the possible solutions to the Readers-Writers problem? |
5. What are the potential challenges in implementing a solution to the Readers-Writers problem? |
|
Explore Courses for Computer Science Engineering (CSE) exam
|