KEMBAR78
Reader/writer problem | PPTX
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
I. 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.
1) wait(S) = S – 1
2) Signal(S) = S + 1
Here we have used three semaphore variables for solving this
problem:
1)Mutex initialize to 1 (Binary Semaphore)
2)Wrt initialize to 1 (Binary Semaphore)
3)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 == 0)
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

Reader/writer problem

  • 1.
    Reader/Writer problem Name :Rinku Monani Subject : Operating Systems
  • 2.
    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.
  • 3.
    SHARING OF DATA •Readers:only read the data set they do not perform any updates •Writers: can both read and write
  • 4.
  • 5.
  • 6.
    I. First readers/writersproblem (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
  • 7.
    SEMAPHORE Definition: Semaphore is non-negativeinteger 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. 1) wait(S) = S – 1 2) Signal(S) = S + 1
  • 8.
    Here we haveused three semaphore variables for solving this problem: 1)Mutex initialize to 1 (Binary Semaphore) 2)Wrt initialize to 1 (Binary Semaphore) 3)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.
  • 9.
    STRUCTURE OF READERPROCESS wait (mutex); readcount ++; if (readcount == 1) wait (wrt); signal(mutex); . .READ THE OBJECT . . wait(mutex); readcount --; if readcount == 0) 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.
  • 10.
    STRUCTURE OF WRITERPROCESS • 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);
  • 11.