Open In App

Readers-Writers Problem | Set 1 (Introduction and Readers Preference Solution)

Last Updated : 22 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The readers-writer problem in operating systems is about managing access to shared data. It allows multiple readers to read data at the same time without issues but ensures that only one writer can write at a time, and no one can read while writing is happening. This helps prevent data corruption and ensures smooth operation in multi-user systems. Probably the most fundamental problem in concurrent programming is to provide safe access to shared resources. A classic problem used to illustrate this issue is Readers-Writers. It is a significant problem for showing how data structures might be synchronized such that consistency is guaranteed and efficiency is ensured. The Readers-Writers problem refers specifically to situations where a number of processes or threads may possibly have access to some common resource, like a database or a file. It also gives rise to the need for good synchronization mechanisms so as not to bias the requirement of readers against that of writers in access to the resource, considering the integrity of data.

What is The Readers-Writers Problem?

The Readers-Writers Problem is a classic synchronization issue in operating systems that involves managing access to shared data by multiple threads or processes. The problem addresses the scenario where:

  • Readers: Multiple readers can access the shared data simultaneously without causing any issues because they are only reading and not modifying the data.
  • Writers: Only one writer can access the shared data at a time to ensure data integrity, as writers modify the data, and concurrent modifications could lead to data corruption or inconsistencies.

Challenges of the Reader-Writer Problem

The challenge now becomes how to create a synchronization scheme such that the following is supported:

  • Multiple Readers: A number of readers may access simultaneously if no writer is presently writing.
  • Exclusion for Writers: If one writer is writing, no other reader or writer may access the common resource.

Solution of the Reader-Writer Problem

There are two fundamental solutions to the Readers-Writers problem:

  • Readers Preference: In this solution, readers are given preference over writers. That means that till readers are reading, writers will have to wait. The Writers can access the resource only when no reader is accessing it.
  • Writer's Preference: Preference is given to the writers. It simply means that, after arrival, the writers can go ahead with their operations; though perhaps there are readers currently accessing the resource.

Focus on the solution Readers Preference in this paper. The purpose of the Readers Preference solution is to give a higher priority to the readers to decrease the waiting time of the readers and to make the access of resource more effective for readers.

Problem Parameters

  • 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 

Solution When Reader Has The Priority Over Writer

Here priority means, no reader should wait if the share is currently open for reading. There are four types of cases that could happen here.

CaseProcess 1Process 2Allowed/Not Allowed
Case 1WritingWritingNot Allowed
Case 2WritingReadingNot Allowed
Case 3ReadingWritingNot Allowed
Case 4ReadingReadingAllowed

Three variables are used: mutex, wrt, readcnt to implement a solution.

  1. semaphore mutex, wrt; // semaphore mutex is used to ensure mutual exclusion when readcnt is updated i.e. when any reader enters or exits from the critical section, and semaphore wrt is used by both readers and writers
  2. int readcnt;  //readcnt tells the number of processes performing read in the critical section, initially 0

Functions for Semaphore

Semaphores are synchronization tools used in operating systems to manage access to shared resources by multiple threads or processes. They use simple integer values and two main operations to control access:

  • wait() : decrements the semaphore value. 
  • signal() : increments the semaphore value. 

Writer Process

  • Writer requests the entry to critical section.
  • If allowed i.e. wait() gives a true value, it enters and performs the write. If not allowed, it keeps on waiting.
  • It exits the critical section.
do {
    // writer requests for critical section
    wait(wrt);  
   
    // performs the write

    // leaves the critical section
    signal(wrt);

} while(true);

Reader Process

  • Reader requests the entry to critical section.
  • If allowed: 
    • it increments the count of number of readers inside the critical section. If this reader is the first reader entering, it locks the wrt semaphore to restrict the entry of writers if any reader is inside.
    • It then, signals mutex as any other reader is allowed to enter while others are already reading.
    • After performing reading, it exits the critical section. When exiting, it checks if no more reader is inside, it signals the semaphore "wrt" as now, writer can enter the critical section.
  • If not allowed, it keeps on waiting.
do {
    
   // Reader wants to enter the critical section
   wait(mutex);

   // The number of readers has now increased by 1
   readcnt++;                          

   // If this is the first reader, lock wrt to block writers
   // this ensure no writer can enter if there is even one reader
   // thus we give preference to readers here
   if (readcnt==1)     
      wait(wrt);                    

   //Allow other readers to enter by unlocking the mutex
   // other readers can enter while this current reader is inside the critical section
   signal(mutex);                   

   // current reader performs reading here
   // a reader wants to leave
   //Lock the mutex to update readcnt
   wait(mutex);   

   //The number of readers has now decreased by 1
   readcnt--;

   // If, no reader is left in the critical section, unlock wrt to allow writers
   if (readcnt == 0) 
       signal(wrt);         // writers can enter

   //Allow other readers or writers to proceed by unlocking the mutex
   signal(mutex); 

} while(true);

Thus, the semaphore 'wrt' is queued on both readers and writers in a manner such that preference is given to readers if writers are also there. Thus, no reader is waiting simply because a writer has requested to enter the critical section

Readers Preference Solution

int rc=0
semaphore mutex=1;
semaphore D/B=1;
void Reader(void)
  {
        while(true)
     {
        down(mutex);
        rc=rc+1;
        if(rc==1) then down(D/B);
        up(mutex);
        DB
        down(mutex)
        rc=rc-1;
        if(rc==0) then up(D/B);
        up(mutex)
        process data
      }
  }
void write(void)
  {
      while(true)
     {
       down(D/B);
       DB
       up(D/B);
      }
}

Conclusion

The Readers-Writers problem is one of the central themes in computing concurrency, trying to explain how shared resources are challenged with respect to synchronization. Basically, the readers-preference solution is a strategy ensuring that, whenever possible, readers get priority over writers for performing read operations to increase efficiency while maintaining the integrity of the shared resource. Understanding the efficient solutions to the Readers-Writers problem is central to a good, robust concurrent system that makes sure no reader or writer will ever be delayed needlessly or suffer data corruption.


Next Article

Similar Reads