Readers–Writers Problem with Reader Priority Using Semaphores
Readers–Writers Problem Using Semaphores
1. AIM
To implement the Readers–Writers synchronization problem using semaphores, allowing multiple readers to access shared data simultaneously while writers get exclusive access.
2. OBJECTIVES
-
Understand the challenges of concurrent access to shared resources.
-
Learn how to use binary and counting semaphores.
-
Implement synchronization logic to ensure:
-
Multiple readers can read concurrently.
-
Only one writer can write at a time.
-
No reader should read while writer is writing.
-
-
Demonstrate classical process synchronization in Operating Systems.
3. THEORY
Readers–Writers Problem
A shared resource (database, file, variable) is accessed by two types of processes:
✔ Readers
-
Only read the data.
-
Do not modify it.
-
Many readers can read simultaneously.
✔ Writers
-
Modify the data.
-
Must have exclusive access.
-
No other reader or writer can access the resource while a writer is writing.
The main goal is to ensure correctness and consistency of shared data.
4. CONSTRAINTS OF THE PROBLEM
-
Multiple readers can read at the same time.
-
Only one writer can write at a time.
-
No reader should read while writer is writing.
-
No two writers should write at the same time.
5. SEMAPHORES USED
| Semaphore | Type | Purpose |
|---|---|---|
mutex | Binary | Protects read_count variable |
rw_mutex | Binary | Controls access to shared resource (writers or first/last reader) |
Variable
Keeps track of number of active readers.
6. ALGORITHM
Reader Algorithm
Writer Algorithm
This is the Reader-Priority solution (standard first readers-writers implementation).
7. C PROGRAM IMPLEMENTATION (POSIX Threads + Semaphores)
8. SAMPLE OUTPUT
9. RESULT
The Readers–Writers problem was successfully implemented using semaphores.
Multiple readers were able to read concurrently while writers obtained exclusive access, satisfying the problem’s constraints.
Comments
Post a Comment