- OS - Home
- OS - Needs
- OS - Overview
- OS - History
- OS - Components
- OS - Structure
- OS - Architecture
- OS - Services
- OS - Properties
- Process Management
- Operating System Processes
- Process Control Block
- Operations on Processes
- Inter Process Communication
- Context Switching
- Multi-threading
- Scheduling Algorithms
- Process Scheduling
- Preemptive and Non-Preemptive Scheduling
- Scheduling Algorithms Overview
- FCFS Scheduling Algorithm
- SJF Scheduling Algorithm
- Round Robin Scheduling Algorithm
- HRRN Scheduling Algorithm
- Priority Scheduling Algorithm
- Multilevel Queue Scheduling
- Lottery Scheduling Algorithm
- Turn Around Time & Waiting Time
- Burst Time in SJF Scheduling
- Process Synchronization
- Process Synchronization
- Critical Section Problem
- Critical Section Synchronization
- Mutual Exclusion Synchronization
- Semaphores
- Counting Semaphores
- Mutex
- Turn Variable
- Bounded Buffer Problem
- Reader Writer Locks
- Test and Set Lock
- Peterson's Solution
- Monitors
- Sleep and Wake
- Race Condition
- OS Deadlock
- Introduction to Deadlock
- Conditions for Deadlock
- Deadlock Handling
- Deadlock Prevention
- Deadlock Avoidance (Banker's Algorithm)
- Deadlock Detection and Recovery
- Deadlock Ignorance
- Memory Management
- Memory Management
- Contiguous Memory Allocation
- Non-Contiguous Memory Allocation
- First Fit Algorithm
- Next Fit Algorithm
- Best Fit Algorithm
- Worst Fit Algorithm
- Fragmentation
- Virtual Memory
- Segmentation
- Buddy System
- Slab Allocation
- Overlays
- Paging and Page Replacement
- Paging
- Demand Paging
- Page Table
- Page Replacement Algorithms
- Optimal Page Replacement Algorithm
- Belady's Anomaly
- Thrashing
- Storage and File Management
- File Systems
- File Attributes
- Structures of Directory
- Linked Index Allocation
- Indexed Allocation
- Disk Scheduling Algorithms
- FCFS Disk Scheduling
- SSTF Disk Scheduling
- SCAN Disk Scheduling
- LOOK Disk Scheduling
- I/O Systems
- I/O Hardware
- I/O Software
- OS Types
- OS - Types
- OS - Batch Processing
- OS - Multiprocessing
- OS - Hybrid
- OS - Monolithic
- OS - Zephyr
- OS - Nix
- OS - Linux
- OS - Blackberry
- OS - Garuda
- OS - Tails
- OS - Clustered
- OS - Haiku
- OS - AIX
- OS - Solus
- OS - Tizen
- OS - Bharat
- OS - Fire
- OS - Bliss
- OS - VxWorks
- OS - Embedded
- OS - Single User
- Miscellaneous Topics
- OS - Security
- OS Questions Answers
- OS - Questions Answers
- OS Useful Resources
- OS - Quick Guide
- OS - Useful Resources
- OS - Discussion
Operating System - Reader Writer Locks in Process Synchronization
Reader-Writer Locks
The reader-writer problem in operating systems deals with managing access to shared data. It allows multiple readers to access the data simultaneously but ensures that only one writer can write at time, with no readers allowed to read during the writing the process.
This approach helps solve the fundamental issues in concurrent programming: providing safe access to shared resources.
Illustrating the reader-writer issues is important for demonstrating how data structures can be synchronized with guaranteed consistency and efficiency. The reader-writer problem describes situations where multiple processes or threads may need access to common resources, such as database or file.
This scenario highlights the necessity for effective synchronization mechanisms to balance the needs of readers and writers accessing the resources.
Concept of Readers and Writers
The Reader-Writer Problem is a synchronization issue in operating system that manages access to shared data by multiple threads or processes.
Readers: Processes that only read from the database.
Writers: Processes that read and modify(write) the database.
Challenges in Synchronization
The following are the challenges associated with the reader-writer problem in synchronization.
Concurrent Access: Multiple processes may want to either read or read and write to the database.
Reader-Reader Access: If two readers access the shared data simultaneously, there is no issue as they only read.
Writer Access: Problems arise when a writer and another process access the database at the same time.
If a writer is updating the data while a reader is reading, the reader might not read consistent data.
If two writers access the database simultaneously, conflicting changes can occur.
Solution of the Reader-Writer Problem
There are three fundamental solutions to the Readers-Writers problem:
Writer's Preference: Preference is given to the writers. This means writers arrive, they can proceed with their operations even if readers are currently accessing the resource.
Goal: The writers have exclusive access to the database,i.e., when a writer is accessing the data, no other process can access it.
Readers Preference: Preference is given to readers. This means that writers have to wait until readers have finished reading. Writers can only access the resource when no readers are accessing it.
Synchronization Using Semaphores
Semaphores solve the Reader's Writer's Problem, we use two semaphores and an integer variable:
Mutex semaphores: Used for mutual exclusion when updating the readcount variable.
Write Semaphore: Initialized to 1, shared between both readers and writers, and used to manage access for writing.
Readcount Variable: A normal integer variable that tracks the number of readers currently accessing the database.
Writer process
The writer process requests the write semaphore, and if it is available, it gains access to perform the write operation. During this time, no other process, whether a reader or another writer, can access the database while the writer holds the semaphore. Once the writing operation is completed, the writer signals the semaphore, releasing it for other process to use.
Reader Process
The reader process follows a series of steps to safely access the shared database.
Wait on the Mutex semaphore: No other process modifies the readcount variable while it's updated.
Increment the readcount: Each reader increments this variable when it starts reading.
First Reader: If the readcount equals, it waits on the write semaphore to prevent writers from accessing the database while reading.
Signal the Mutes Semaphore: After updating the readcount, the reader signals the mutex semaphore, allowing other readers to increment the readcount and enter the critical section.
Reading Operation: The reader reads the data.
Decrement the readcount: When the reader finishes, it decrements the readcount.
Last Reader: If readcount equals 0, the reader signals the write semaphore to allow a writer to access the database.
Problem Parameters
Problem parameters for the Reader-Writer process −
One set of data is shared among a number of processes.
Once a writer is ready, it performs its write. Only one writer may write at a time.
If a process is writing, no other process can read it.
If at least one reader is reading, no other process can write.
Readers may not write and only read.
Main Features of Solution
Following are the key concepts of the solution −
Writer Access: The writer can only access the database of no other reader or writer is using it, ensured by the write semaphore.
Reader Priority: Priority is given to readers. If one reader is accessing the database, no writer can modify it until all readers have finished.
Concurrency: Multiple readers can access the database simultaneously without any issues, but when a writer accesses the database, exclusive access is required.
Conclusion
This solution achieves synchronization between readers and writers through the use of semaphores, effectively preventing race conditions and ensuring the consistency of data in the shared database.