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

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:

Challenges of the Reader-Writer Problem

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

Solution of the Reader-Writer Problem

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

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

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.

Case Process 1 Process 2 Allowed/Not Allowed
Case 1 Writing Writing Not Allowed
Case 2 Writing Reading Not Allowed
Case 3 Reading Writing Not Allowed
Case 4 Reading Reading Allowed

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:

Writer Process

do < // writer requests for critical section wait(wrt); // performs the write // leaves the critical section signal(wrt); >while(true);

Reader Process