Read Write problem
What is the read-write problem?
Threads Involved:
- Reader Threads: Read the shared resource without modifying it.
- Writer Threads: Modify or update the shared resource.
Rules for Safe Access:
- Multiple Readers: Multiple reader threads can read the resource simultaneously without causing inconsistencies, as long as no writer is updating the resource.
- Single Writer: Only one writer thread should be allowed to modify the resource at a time. During this period, no other readers or writers should access the resource.
File Example:
- Multiple Writers: If two writers, W1 and W2, are writing to the same file simultaneously, they can corrupt or overwrite each other's changes, leading to data inconsistency. This is because the file might end up with mixed or incorrect data if both writers modify it at the same time.
- Reader and Writer Interaction (Concurrent Reading and Writing):
- If a reader, R1, reads from a file while a writer, W1, is updating it, the reader might see a partially updated or inconsistent state of the file.
- For instance: Writer W1: Writes "ABCD" to the file. Reader R1: Reads the file and sees "ABCD".
- Before R1 finishes: Writer W1 updates the file to "ABCE".
- R1's Read: If R1’s read operation is not synchronized with W1’s write operation, R1 might have read outdated data or a mix of old and new data, leading to inconsistency.
Why Synchronization is Needed:
- Inconsistency: Without proper synchronization, the file could end up with inconsistent data due to simultaneous writes or incorrect reads when the file is being modified.
- Race Conditions: Occur when multiple threads access or modify shared data without proper coordination, leading to unpredictable outcomes.
The solution to this problem
Semaphores Used:
- Binary Semaphore (wrt): This semaphore controls access to the critical section where the shared resource is modified. It ensures that only one writer can access the resource at a time, and it also ensures that no readers are accessing the resource while a writer is writing.
- Count Variable (read_count): This variable keeps track of the number of readers currently accessing the shared resource.
Writer Thread:
Before Entering the Critical Section: wait(wrt), the writer performs a wait operation on the wrt semaphore. If wrt is 1 (available), the writer can enter the critical section. If wrt is 0 (indicating a writer is currently in the critical section), the writer will be blocked and wait until wrt becomes available.
Writing Operation: The writer performs the write operation to modify the shared resource.
After Exiting Critical Section: signal(wrt), after finishing the write operation, the writer performs a signal operation on the wrt semaphore. This operation releases the semaphore, allowing other writers or readers to enter the critical section.
Reader Thread:- Entering Critical Section:
- wait(mutex): The reader acquires the mutex to safely access and modify readcount.
- Update readcount: Increment the readcount variable to reflect that a new reader is entering. If readcount was 0 before, this indicates that no readers were in the critical section, so the reader needs to check if a writer is currently active:
- wait(wrt): If readcount was 1 (i.e., the first reader), this operation blocks any writers from entering the critical section, ensuring that readers can access the resource without interference.
- signal(mutex): Release the mutex after updating readcount to allow other readers or threads to access readcount.
- Reading Operation: The reader performs the read operation.
- wait(mutex): The reader acquires the mutex again to safely update readcount as it leaves.
- Update readcount: Decrement the readcount variable. If readcount becomes 0 (indicating no more readers are in the critical section):
- signal(wrt): Release the wrt semaphore to allow any waiting writers to enter the critical section.
- signal(mutex): Release the mutex after updating readcount.

Comments
Post a Comment