0% found this document useful (0 votes)
5 views5 pages

Lecture 19 Reader Writer Solution Problem

The Readers-Writers Problem is a synchronization challenge in operating systems that manages access to shared data by multiple processes, allowing simultaneous reading while ensuring exclusive writing. It highlights the need for synchronization to prevent data corruption and balance efficiency with data integrity. Solutions include prioritizing readers or writers, with the Readers Preference Solution optimizing for reader access while maintaining data integrity.

Uploaded by

Ayush Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Lecture 19 Reader Writer Solution Problem

The Readers-Writers Problem is a synchronization challenge in operating systems that manages access to shared data by multiple processes, allowing simultaneous reading while ensuring exclusive writing. It highlights the need for synchronization to prevent data corruption and balance efficiency with data integrity. Solutions include prioritizing readers or writers, with the Readers Preference Solution optimizing for reader access while maintaining data integrity.

Uploaded by

Ayush Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Readers-Writers Problem

Introduction to the Readers-Writers Problem


• The Readers-Writers Problem is a classical synchronization challenge
in operating systems.

• It focuses on managing access to shared data by multiple threads or


processes.

Key Goals
• Allow multiple readers to access the shared resource simultaneously.

• Ensure only one writer can modify the shared resource at a time.

Significance
• Prevents data corruption by ensuring no concurrent writes or simul-
taneous reads and writes.

• Demonstrates the importance of safe access to shared resources in


concurrent programming.

• Highlights the need for synchronization to balance efficiency and data


integrity.

Problem Definition
• Readers can read simultaneously without causing issues since they
don’t modify the data.

• Writers must work in isolation, as multiple writers or simultaneous


read-write operations could corrupt data.

1
Challenges
• Multiple Readers: Readers should be able to read together, provided
no writer is active.

• Exclusion for Writers: If one writer is writing, no reader or writer


should access the shared resource.

Solutions
• Readers Preference:

– Gives higher priority to readers.


– Writers can access the resource only when no reader is active.

• Writers Preference:

– Gives higher priority to writers.


– Ensures writers can proceed immediately, even if readers are wait-
ing.

Readers Preference Explained


• Objective: Prioritize readers to minimize their waiting time and im-
prove efficiency.

• Priority: Ensures readers never have to wait if the resource is free for
reading.

Problem Parameters
• Shared data is accessed by multiple processes.

• Only one writer can write at a time.

• No reader can read if a writer is active.

• Readers only read, never modify the shared resource.

2
Four Possible Cases
• Case 1: Two writers attempt to write simultaneously. Not Allowed.

• Case 2: One writer writes while a reader reads. Not Allowed.

• Case 3: A writer writes while another writer tries to write. Not


Allowed.

• Case 4: Multiple readers read simultaneously. Allowed.

Key Variables
• Semaphores:

– mutex: Ensures mutual exclusion when updating the number of


readers.
– wrt: Synchronizes access to the shared resource between readers
and writers.

• readcnt: Tracks the number of active readers in the critical section.

Semaphore Functions
• wait(): Decrements the semaphore value, blocking the process if the
value is 0.

• signal(): Increments the semaphore value, allowing blocked processes


to proceed.

Writer Process Logic


• Writers use the wrt semaphore to ensure exclusive access.

Steps:
1. Call wait(wrt) to request access.

2. Write to the shared resource.

3. Call signal(wrt) to release access.

3
Reader Process Logic
• Readers use mutex and wrt to synchronize access.

Steps:
1. Increment the reader count (readcnt).

2. If the first reader, lock wrt to prevent writers from entering.

3. Perform reading.

4. Decrement readcnt.

5. If no readers are left, unlock wrt.

Implementation Pseudocode
Reader:
1 wait ( mutex ) ;
2 readcnt ++;
3 if ( readcnt == 1) wait ( wrt ) ; // First reader locks
writers
4 signal ( mutex ) ;
5 // Perform reading
6 wait ( mutex ) ;
7 readcnt - -;
8 if ( readcnt == 0) signal ( wrt ) ; // Last reader
unlocks writers
9 signal ( mutex ) ;

Writer:
1 wait ( wrt ) ; // Request access
2 // Perform writing
3 signal ( wrt ) ; // Release access

4
Readers Preference Solution
• Ensures readers get priority.

• Writers wait if readers are reading, even if the writer has already re-
quested access.

Conclusion
• The Readers-Writers Problem illustrates how to synchronize shared
resources efficiently.

• The Readers Preference Solution optimizes for readers, ensuring


no unnecessary delay while maintaining data integrity.

• Understanding this problem is critical for robust concurrent system


design.

You might also like