OS Week-8 ASSIGNMENT
OS Week-8 ASSIGNMENT
such as a database, that can be accessed concurrently by multiple threads. The problem is divided
into two categories:
1. Readers: These threads can read from the shared resource, but they do not modify it.
2. Writers: These threads modify the shared resource, and while doing so, no other writer or
reader can access the resource.
Multiple readers can read the shared resource at the same time, as long as no writer is
writing to it.
Only one writer can access the shared resource at a time, and no readers can read while a
writer is writing.
Writers should not be starved, and they must eventually get access to the resource.
We will discuss a general solution for the Reader-Writer problem, focusing on fairness and
preventing starvation.
The most common approach is to use semaphores for synchronization. Semaphores are signaling
mechanisms used to control access to shared resources. We can use them to handle the mutual
exclusion and coordination between readers and writers.
Data Structures:
1. mutex (binary semaphore): This semaphore is used to ensure mutual exclusion when
accessing shared variables. It prevents race conditions while modifying or reading the shared
resource.
2. rw_mutex (binary semaphore): This semaphore ensures mutual exclusion for the writers,
ensuring that only one writer can access the resource at a time.
4. read_count (integer variable): This keeps track of how many readers are currently reading
the shared resource.
Algorithm Explanation:
Initialization:
mutex = 1: The mutex semaphore starts as 1, allowing access to the critical section.
rw_mutex = 1: The rw_mutex semaphore starts as 1, meaning only one writer can write at a
time.
Reader Process:
4. Check if it is the first reader: If the current reader is the first one (i.e., read_count == 1),
acquire the rw_mutex to prevent writers from accessing the resource.
5. Read the shared resource: Perform the read operation on the shared resource.
9. Check if it is the last reader: If the current reader is the last one (i.e., read_count == 0),
release rw_mutex to allow writers to write.
Writer Process:
1. Acquire rw_mutex: Before writing, a writer must acquire the rw_mutex semaphore to
ensure mutual exclusion with other writers.
2. Write to the shared resource: Perform the write operation on the shared resource.
3. Release rw_mutex: After writing, the writer releases rw_mutex to allow other writers or
readers to access the resource.
# Initialization
mutex = Semaphore(1)
rw_mutex = Semaphore(1)
read_count_mutex = Semaphore(1)
read_count = 0
# Reader Process
def reader():
global read_count
# Entry Section
if read_count == 1:
# Exit Section
if read_count == 0:
# Writer Process
def writer():
# Entry Section
# Exit Section
1. Readers:
o A reader process first increments the read_count and checks if it is the first reader. If
so, it locks the rw_mutex to prevent writers from accessing the resource.
2. Writers:
o A writer process acquires the rw_mutex semaphore, ensuring that no other writer or
reader is accessing the resource.
o After writing, the writer releases the rw_mutex semaphore to allow other processes
to access the resource.
Key Points:
1. Mutual Exclusion: Ensures that only one writer or one group of readers can access the
resource at any given time.
2. Starvation Avoidance: The solution ensures that neither readers nor writers are starved. The
priority can be adjusted based on which process should be prioritized.
3. Concurrency: Multiple readers can access the shared resource concurrently, but only one
writer can write at a time.