Reader/writer problem

RinkuMonani 5,664 views 11 slides Apr 10, 2019
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

This ppt covers the reader/writer problem in process synchronization in Operating systems.


Slide Content

Reader/Writer problem Name : Rinku Monani Subject : Operating Systems

Introduction to problem Multiple reader/writer activities can run simultaneously. At any time, a reader activity may wish to read data. Or a writer activity may wish to write or modify the data at any instant. Simultaneous reading and writing to the data can cause inconsistency. Therefore, to avoid this situation, a set of rules is followed, Any number of readers can access the data simultaneously. But during the time a writer is writing, no other reader or writer may access the shared data.

Sharing of data •Readers: only read the data set they do not perform any updates •Writers: can both read and write

Active Readers

Active Writers

First readers/writers problem (reader priority): No reader will wait (for other readers to finish) even if a writer is waiting Writer starvation possible Semaphore solution II. Second reader/writers problem (writer priority): No new readers allowed once a writer has asked for access Solution can be implemented using monitors

semaphore Definition: Semaphore is non-negative integer variable (It’s denoted by ‘S’). Types of semaphore: 1) Binary Semaphore(value between 0 & 1) 2) Counting Semaphore(value are 1,2,3…..) Functions are used to modify the value of semaphore variable. wait(S) = S – 1 2) Signal(S) = S + 1

Here we have used three semaphore variables for solving this problem: Mutex initialize to 1 (Binary Semaphore) Wrt initialize to 1 (Binary Semaphore) Readcount initialize to 0 (Counting Semaphore Solution using semaphore Mutex: Used to lock critical section for both readers and writers. Wrt : Used to block the writers from entering the critical section. Read count : Helps counting the number of readers in the critical section and permits the writer to enter when it become zero.

Structure of reader process wait ( mutex ); readcount ++; if ( readcount == 1 ) wait ( wrt ); signal ( mutex ); . . READ THE OBJECT . . wait ( mutex ); readcount --; if readcount == ) signal ( wrt ); s signal ( mutex ); The mutex semaphore ensures mutual exclusion and wrt handles the writing mechanism and is common to the reader and writer process code. As soon as readcount becomes 1, wait operation is used on wrt . This means that a writer cannot access the object anymore. After the read operation is done, readcount is decremented. When readcount becomes 0, signal operation is used on wrt . So a writer can access the object now.

Structure of writer process If a writer wants to access the object, wait operation is performed on wrt . After that no other writer can access the object. When a writer is done writing into the object, signal operation is performed on wrt . do { // writer requests for critical section wait( wrt ); // performs the write // leaves the critical section signal( wrt ); }while(true);

Thank You