6

Readers-Writers Problem in OS

 2 years ago
source link: https://www.geeksforgeeks.org/videos/readers-writers-problem-in-os/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Readers-Writers Problem in OS

Readers-Writers Problem in OS
Hello everyone. Welcome to Geeks for geeks.

In this video, we will be covering the readers & writers problem in operating systems. 

The readers' writer's problem is a classical problem in process synchronization. In the reader's writer's problem, there is a single file shared between multiple people. Some of them will only read the file and they will be known as readers whereas some of them will write in the file and they will be called writers

There are a few constraints applied to the file in order to maintain the integrity of the data and these constraints prevent the data from any kind of data loss. The constraints are as follows:

  • If a person is reading a file, then other people can only read the file but not write to it.
  • If a person is writing to a file, then other people are not allowed to read or write to the same file.
  • This scenario with its constraints is called the readers-writers problem.

When readers are given priority or when readers are accessing the file, they are not made to wait for reading a file. Three variables are used to implement the solution:

  • Semaphore mutex, wrt: semaphore mutex ensures mutual exclusion whereas semaphore wrt is used by both readers and writers
  • int readcnt: readcnt counts the number of processes performing read operation in the critical section. It value is initially set to 0

Two functions are used for manipulating the value of semaphore : 

  • wait(): reduces the value of semaphore by 1
  • signal(): increases the value of semaphore by 1  

Solution to the reader-writer problem:

  • First, the writer requests to enter the critical section.
  • If the writer gets the permission i.e. wait() gives a true value, it enters the critical section and performs the write. 
  • If not allowed, it waits in a loop
  • After writing, the writer exits the critical section.
  • Reader requests to enter the critical section.
  • If the reader gets permission, then it increments the number of readers inside the critical section. 
  • If this reader is the first reader entering, it locks the “wrt” semaphore to prevent writers from entering the critical section.
  • Then, the reader signals mutex to allow more readers to enter while others are already reading.
  • After completing its read, the reader exits the critical section. 
  • Before exiting, the reader checks if there are readers inside. If yes, it signals the semaphore wrt because any writer can now enter the critical section.

The semaphore ‘wrt‘ is queued on both readers and writers in a way that preference is given to readers if writers are also there. So, no reader is waiting because a writer has requested to enter the critical section.

Readers-Writers problem (readers problem): https://www.geeksforgeeks.org/readers-writers-problem-set-1-introduction-and-readers-preference-solution/

Reader-Writers problem (writer preference): https://www.geeksforgeeks.org/readers-writers-problem-writers-preference-solution/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK