Readers Writers Problem in Operating System (OS)
About Multithreading in OS
On this page, we will learn the concepts of various multi-threading models in operating system. In multi-threading, multiple user threads can be created as many as required and the corresponding kernel threads run in parallel over multiprocessor machine.We will learn about the concepts and various types of multithreading.
Readers Writers Problem in Operating System
This is a synchronisation problem which is used to test newly proposed synchronisation scheme. The problem statement is, if a database or file is to be shared among several concurrent process, there can be broadly 2 types of users –
- Readers – Reader are those processes/users which only read the data
- Writers – Writers are those processes which also write, that is, they change the data .
It is allowed for 2 or more readers to access shared data, simultaneously as they are not making any change and even after the reading the file format remains the same.
But if one writer(Say w1) is editing or writing the file then it should locked and no other writer(Say w2) can make any changes until w1 has finished writing the file.
Writers are given to be exclusive access to shared database while writing to database. This is called Reader’s writer problem.
Highlights
- If writer W1 has begun writing process then
- No additional writer can perform write function
- No reader is allowed to read
- If 1 or more readers are reading then
- Other readers may read as well
- No writer may perform write function until all readers have finished reading
Explanations
In simple terms understand this as unlimited number of readers can read simultaneously. Only one writer can write at a time.
When a writer is writing no other writer can write to the file. A writer can not write when there are one or more than one readers reading. That is writer can only write when there is no readers or no writers accessing the resource.
Solution
Variables used –
- Mutex – mutex (used for mutual exclusion, when readcount is changed)
- initialised as 1
- Semaphore – wrt (used by both readers and writers)
- initialised as 1
- readers_count – Counter of number of people reading the file
- initialised as 0
Functions –
There are two functions –
- wait() – performs as –, which basically decrements value of semaphore
- signal() – performs as ++. which basically increments value of semaphore
How does Wait and Signal work
To understand how readers and writers work we first need to understand that how atomic operations wait and signal work
Readers Writers Wait and Signal Implementation.
wait (Semaphore s) { while (s == 0); s = s - 1; } signal (Semaphore s) { s = s + 1; }
Writers problem
while(TRUE) { //if wait(wrt)returns true value,
//then can enter critical section
//if not allowed then, it keeps on waiting wait(wrt); /* writer does some write operation */ //increments w value again to 1
//so other writers can writer signal(wrt); }
- Writer wants the access to critical section i.e. wants to perform the writing
- First wait(wrt) value is checked
- if returns true then gets access
- does the write process
- performs signal(wrt) and increments value
- if returns false then
- doesn’t get write access.
- if returns true then gets access
Readers Problem
while(TRUE) { //a reader wishes to enter critical section
//this mutex is used for mutual exclusion before readcount is changed wait(mutex); //now since he has entered increase the readers_count by 1 readers_count++; // readers_count value now should be greater than or equal to 1 // used ==1 not >=1 as we want to perform this only once. if(readers_count == 1) // decrementing w value so no writer can enter writer section
// as readers are reading // MCQ Question Fact -
// Readers have more priority then writers. wait(wrt); //this will ensure that now, other readers can enter critical section signal(mutex); /* perform the reading operation */ // a reader wants to leave after reading process wait(mutex); readers_count--; if(readers_count == 0) // if readers_count is 0 we must restore w value to 1 so writers can write signal(wrt); // reader has now left signal(mutex); }
Writer –
- Mutex Semaphore ensure mutual exclusion when read count is updated.
- Read_count -> tracks how many processes are currently reading the object.
- mutex -> Functions as a mutual exclusion semaphore for writers. Also used by first or last reader that enters or exits the critical section.
- If writer is in critical section and n readers are waiting, 1 reader is queued as mutex. And n-1 readers are queued or mutex.
- When signal ( mutex ) is executed, then one of 2 thing can happen -> Either waiting readers execute or 1 single writing process is executed. This is needed by scheduler.
The above solution to reader writer problem can be generalized to provide reader- writer lock on some system. To acquire a reader writer lock, one must specify the mode of lock- either read or write access.
If a process wanting to only read shared data , request reader-writer lock in read mode. A process waiting to only write on shared data must request the lock in write mode.
This reader writer lock is effective in –
- The application where reading and writing process are easily identifiable.
- Application having more readers than writers. As reader-writer locks require more overhead to establish than semaphore than semaphore or mutual exclusion locks. So, with the help of multiple readers, it compensates for the overhead of setting up readers-writer lock.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment