OS Menu9>
- OS Home
- Introduction
- CPU Scheduling
- What is Process?
- Process Lifecyle
- Process Control Block
- Process Scheduling
- Context Switching
- CPU Scheduling
- FCFS Scheduling
- SJF (non-preemptive)
- SJF (Preemptive - SRTF)
- Round Robin
- Priority Scheduling
- Convoy Effect
- Scheduler Vs Dispatcher
- Preemptive Vs non
- Preemptive scheduling
- Non preemptive scheduling
- Process Synchronization
- Deadlock
- Popular Algorithms
- Memory Management
- Memory Management Introduction
- Partition Allocation Method
- First Fit
- First Fit (Intro)
- First Fit in C
- First Fit in C++
- First Fit in Python
- First Fit in Java
- Best Fit
- Best Fit (Intro)
- Best Fit in C
- Best Fit in C++
- Best Fit in Java
- Worst Fit
- Worst Fit (Intro)
- Worst Fit in C++
- Worst Fit in C
- Worst Fit in Java
- Worst Fit in Python
- Next Fit
- First fit best fit worst fit (Example)
- Memory Management 2
- Memory Management 3
- Page Replacement Algorithms
- LRU (Intro)
- LRU in C++
- LRU in Java
- LRU in Python
- FIFO
- Optimal Page Replacement algorithm
- Optimal Page Replacement (Intro)
- Optimal Page Replacement Algo in C
- Optimal Page Replacement Algo in C++
- Optimal Page Replacement Algo in Java
- Optimal Page Replacement Algo in Python
- Thrashing
- Belady’s Anomaly
- Static vs Dynamic Loading
- Static vs Dynamic Linking
- Swapping
- Translational Look Aside Buffer
- Process Address Space
- Difference between Segmentation and Paging
- File System
- Off-campus Drive Updates
- Get Hiring Updates
- Contact us
PREPINSTA PRIME
Readers Writers Problem in Operating System (OS)
About Readers Writers Problem in OS
Readers Writers Problem in Operating System (OS) involves managing access to a shared resource where multiple readers can access it concurrently, but writers require exclusive access. The goal is to ensure that no writer is interrupted while writing, and readers can access the resource simultaneously without conflict.
On this page, we will learn the concepts of synchronizing access using semaphores, mutexes, and condition variables, and explore solutions that handle issues like starvation and fairness. Proper synchronization ensures efficient and safe data access for all processes involved.
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;}
Prepare for Interview with us:
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.
Findings
The Readers Writers Problem in Operating Systems involves efficiently managing access to a shared resource where multiple readers can access it simultaneously, while writers require exclusive access. Proper synchronization techniques like semaphores, mutexes, and condition variables are crucial in ensuring fair access and preventing starvation. The solutions aim to balance reader and writer access, ensuring system stability and preventing conflicts. By applying these synchronization methods, operating systems can effectively handle concurrent processes while ensuring safe data manipulation.
FAQs
No, a reader cannot access the resource if a writer is waiting. Writers are given priority once all readers have finished, to avoid writer starvation.
If a writer is writing, no readers are allowed to access the resource until the writer finishes, ensuring the writer has exclusive access.
Starvation occurs when one group (readers or writers) is continuously blocked by the other. Proper synchronization ensures fairness and avoids this issue.
Reader writer locks can be optimized for scenarios with many readers, reducing overhead by allowing simultaneous access for multiple readers while blocking writers until necessary.
Login/Signup to comment