SlideShare a Scribd company logo
1 of 11
Download to read offline
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

More Related Content

Similar to readerwriter-190410082551.pdf

Similar to readerwriter-190410082551.pdf (20)

IPC
IPCIPC
IPC
 
IPC
IPCIPC
IPC
 
Ch5 process synchronization
Ch5   process synchronizationCh5   process synchronization
Ch5 process synchronization
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
 
Sync, async and multithreading
Sync, async and multithreadingSync, async and multithreading
Sync, async and multithreading
 
Semaphores and Monitors
 Semaphores and Monitors Semaphores and Monitors
Semaphores and Monitors
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
 
parallel Questions & answers
parallel Questions & answersparallel Questions & answers
parallel Questions & answers
 
UNIT III Process Synchronization.docx
UNIT III Process Synchronization.docxUNIT III Process Synchronization.docx
UNIT III Process Synchronization.docx
 
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
Locks, Blocks, and Snapshots: Maximizing Database Concurrency (PASS DBA Virtu...
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Java
JavaJava
Java
 
multithreading
multithreadingmultithreading
multithreading
 
Multiprocessing -Interprocessing communication and process sunchronization,se...
Multiprocessing -Interprocessing communication and process sunchronization,se...Multiprocessing -Interprocessing communication and process sunchronization,se...
Multiprocessing -Interprocessing communication and process sunchronization,se...
 
Operating System Slides 1.pptx
Operating System Slides 1.pptxOperating System Slides 1.pptx
Operating System Slides 1.pptx
 
Types of Operating System
Types of Operating SystemTypes of Operating System
Types of Operating System
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
 
An Introduction to Locks in Go
An Introduction to Locks in GoAn Introduction to Locks in Go
An Introduction to Locks in Go
 

More from RohanThota3 (6)

Aluminium and its alloy study about aluminium
Aluminium and its alloy study about aluminiumAluminium and its alloy study about aluminium
Aluminium and its alloy study about aluminium
 
Arithmetic operators
Arithmetic operatorsArithmetic operators
Arithmetic operators
 
22R01A66C1.pptx
22R01A66C1.pptx22R01A66C1.pptx
22R01A66C1.pptx
 
Binary Search Tree In Python.pptx
Binary Search Tree In Python.pptxBinary Search Tree In Python.pptx
Binary Search Tree In Python.pptx
 
22R01A67C1.pptx
22R01A67C1.pptx22R01A67C1.pptx
22R01A67C1.pptx
 
M.pranavi[22R01A6735] maths^.pptx
M.pranavi[22R01A6735] maths^.pptxM.pranavi[22R01A6735] maths^.pptx
M.pranavi[22R01A6735] maths^.pptx
 

Recently uploaded

SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 

Recently uploaded (20)

An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 

readerwriter-190410082551.pdf

  • 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
  • 6. 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
  • 7. 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
  • 8. 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.
  • 9. 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.
  • 10. 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);