SlideShare une entreprise Scribd logo
1  sur  19
OPERATING SYSTEM
Classical problems of Synchronization
Dr. Gopika S ,
Department of CS, KJC
Classical problems of Synchronization
• There are a number of classical problems of synchronization as examples of a
large class of concurrency-control problems. semaphores are used as solution for
synchronization, since that is the traditional way to present such solutions.
• However, actual implementations of these solutions could use mutex locks in
place of binary semaphores.
• These problems are used for testing nearly every newly proposed synchronization
scheme. The following problems of synchronization are considered as classical
problems:
1. Bounded-buffer (or Producer-Consumer) Problem,
2. Dining-Philosophers Problem,
3. Readers and Writers Problem,
Bounded-buffer (or Producer-Consumer) Problem:
• Bounded Buffer problem is also called producer consumer problem. This problem
is generalized in terms of the Producer-Consumer problem.
• There is one Producer in the producer-consumer problem, Producer is producing
some items, whereas there is one Consumer that is consuming the items
produced by the Producer. The same memory buffer is shared by both producers
and consumers which is of fixed-size.
• The task of the Producer is to produce the item, put it into the memory buffer,
and again start producing items. Whereas the task of the Consumer is to
consume the item from the memory buffer.
• Solution to this problem is, creating two counting semaphores “full” and “empty”
to keep track of the current number of full and empty buffers respectively.
Producers produce a product and consumers consume the product, but both use
of one of the containers each time.
Producer Code
Consumer Code
• The producer should produce data only when the buffer is not full. In case it is
found that the buffer is full, the producer is not allowed to store any data into the
memory buffer.
• Data can only be consumed by the consumer if and only if the memory buffer is
not empty. In case it is found that the buffer is empty, the consumer is not
allowed to use any data from the memory buffer.
• Accessing memory buffer should not be allowed to producer and consumer at the
same time.
• If the producer is attempting to add an item at the same moment the
consumer is removing one, the outcome depends on whether the
producer’s check for available space happens before or after the
consumer decrements the counter.
• A similar race condition arises if the queue is empty and both
functions are called. This race condition could be fixed by wrapping
the accesses to the counter with a mutex.
• Dining-Philosophers Problem:
• The Dining Philosopher Problem states that K philosophers seated
around a circular table with one chopstick between each pair of
philosophers. There is one chopstick between each philosopher. A
philosopher may eat if he can pickup the two chopsticks adjacent to
him. One chopstick may be picked up by any one of its adjacent
followers but not both. This problem involves the allocation of limited
resources to a group of processes in a deadlock-free and starvation-
free manner.
Readers-writers problem
Consider a situation where we have a file shared between many people.
If one of the person tries editing the file, no other person should be reading or
writing at the same time, otherwise changes will not be visible to him/her.
However if some person is reading the file, then others may read it at the same
time.
Precisely in OS we call this situation as the readers-writers problem
• Problem parameters:
• One set of data is shared among a number of processes
• Once a writer is ready, it performs its write. Only one writer may write at a time
• If a process is writing, no other process can read it
• If at least one reader is reading, no other process can write
• Readers may not write and only read
Solution
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
There are four Types of cases could happen here.
Here priority means, no reader should wait if the share is currently opened for reading.
Three variables are used: mutex, wrt, readcnt to implement solution
Writer process:
• Writer requests the entry to critical section.
• If allowed i.e. wait() gives a true value, it enters and performs
the write. If not allowed, it keeps on waiting.
• It exits the critical section.
Reader process:
1.Reader requests the entry to critical section.
2.If allowed:
1. it increments the count of number of readers inside the critical
section. If this reader is the first reader entering, it locks the wrt
semaphore to restrict the entry of writers if any reader is inside.
2. It then, signals mutex as any other reader is allowed to enter
while others are already reading.
3. After performing reading, it exits the critical section. When
exiting, it checks if no more reader is inside, it signals the
semaphore “wrt” as now, writer can enter the critical section.
3.If not allowed, it keeps on waiting.
The Dining Philosopher Problem –
• The Dining Philosopher Problem states that K philosophers seated around a circular table
with one chopstick between each pair of philosophers. There is one chopstick between each
philosopher. A philosopher may eat if he can pick up the two chopsticks adjacent to him. One
chopstick may be picked up by any one of its adjacent followers but not both.
Semaphore Solution to Dining Philosopher –
Each philosopher is represented by the following pseudocode:
process P[i]
while true do
{
THINK;
PICKUP(CHOPSTICK[i], CHOPSTICK[i+1 mod 5]);
EAT;
PUTDOWN(CHOPSTICK[i], CHOPSTICK[i+1 mod 5])
}
There are three states of the philosopher: THINKING, HUNGRY, and EATING.
The dining philosopher demonstrates a large class of
concurrency control problems hence it's a classic
synchronization problem.
1. Suppose Philosopher P0 wants to eat, it will enter in Philosopher() function, and execute Wait( take_chopstickC[i] ); by doing
this it holds C0 chopstick and reduces semaphore C0 to 0, after that it execute Wait( take_chopstickC[(i+1) % 5] ); by doing this
it holds C1 chopstick( since i =0, therefore (0 + 1) % 5 = 1) and reduces semaphore C1 to 0
2. Similarly, suppose now Philosopher P1 wants to eat, it will enter in Philosopher() function, and execute Wait(
take_chopstickC[i] ); by doing this it will try to hold C1 chopstick but will not be able to do that, since the value of semaphore
C1 has already been set to 0 by philosopher P0, therefore it will enter into an infinite loop because of which philosopher P1 will
not be able to pick chopstick C1 whereas if Philosopher P2 wants to eat, it will enter in Philosopher() function, and execute
Wait( take_chopstickC[i] ); by doing this it holds C2 chopstick and reduces semaphore C2 to 0, after that, it executes Wait(
take_chopstickC[(i+1) % 5] ); by doing this it holds C3 chopstick( since i =2, therefore (2 + 1) % 5 = 3) and reduces semaphore C3
to 0.
• The drawback of the above solution of the dining philosopher
problem
• From the above solution of the dining philosopher problem, we have
proved that no two neighboring philosophers can eat at the same
point in time. The drawback of the above solution is that this solution
can lead to a deadlock condition. This situation happens if all the
philosophers pick their left chopstick at the same time, which leads to
the condition of deadlock and none of the philosophers can eat.
What is Deadlock in Operating System (OS)?
Every process needs some resources to complete its execution. However, the
resource is granted in a sequential order.
The process requests for some resource.
OS grant the resource if it is available otherwise let the process waits.
The process uses it and release on the completion.
A Deadlock is a situation where each of the computer process waits for a resource
which is being assigned to some another process. In this situation, none of the
process gets executed since the resource it needs, is held by some other process
which is also waiting for some other resource to be released.
Let us assume that there are three processes P1,
P2 and P3. There are three different resources R1,
R2 and R3. R1 is assigned to P1, R2 is assigned to
P2 and R3 is assigned to P3.
After some time, P1 demands for R1 which is being used by P2. P1
halts its execution since it can't complete without R2. P2 also
demands for R3 which is being used by P3. P2 also stops its
execution because it can't continue without R3. P3 also demands for
R1 which is being used by P1 therefore P3 also stops its execution.
In this scenario, a cycle is being formed among
the three processes.
None of the process is progressing and they are
all waiting.
The computer becomes unresponsive since all
the processes got blocked.
Difference between Starvation and Deadlock
Sr. Deadlock Starvation
1
Deadlock is a situation where no process
proceeds
Starvation is a situation where the low priority
process got blocked and the high priority
processes proceed.
2 Deadlock is an infinite waiting. Starvation is a long waiting but not infinite.
3 Every Deadlock is always a starvation. Every starvation need not be deadlock.
4
The requested resource is blocked by the
other process.
The requested resource is continuously be used
by the higher priority processes.
5
Deadlock happens when Mutual exclusion,
hold and wait, No preemption and circular
wait occurs simultaneously.
It occurs due to the uncontrolled priority and
resource management.
Necessary conditions for Deadlocks
Mutual Exclusion
A resource can only be shared in mutually exclusive manner. It implies, if two process cannot use
the same resource at the same time.
Hold and Wait
A process waits for some resources while holding another resource at the same time.
No preemption
The process which once scheduled will be executed till the completion. No other process can be
scheduled by the scheduler meanwhile.
Circular Wait
All the processes must be waiting for the resources in a cyclic manner so that the last process is
waiting for the resource which is being held by the first process.

Contenu connexe

Similaire à Deadlock _Classic problems.pptx

Lecture 5 process synchronization
Lecture 5 process synchronizationLecture 5 process synchronization
Lecture 5 process synchronizationKlintonChhun
 
Operating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronizationOperating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronizationVaibhav Khanna
 
Dining Philosopher's Problem
Dining Philosopher's ProblemDining Philosopher's Problem
Dining Philosopher's ProblemYash Mittal
 
Classical problems of process synchronization
Classical problems of process synchronizationClassical problems of process synchronization
Classical problems of process synchronizationsangrampatil81
 
Dining philosopher problem operating system
Dining philosopher problem operating system Dining philosopher problem operating system
Dining philosopher problem operating system anushkashastri
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.pptjayverma27
 
Operating systems
Operating systemsOperating systems
Operating systemsAbraham
 
Operating systems
Operating systemsOperating systems
Operating systemsAbraham
 
Operating System Process Synchronization
Operating System Process SynchronizationOperating System Process Synchronization
Operating System Process SynchronizationHaziq Naeem
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronizationlodhran-hayat
 
4.3 Deadlock [Autosaved].pptx
4.3 Deadlock [Autosaved].pptx4.3 Deadlock [Autosaved].pptx
4.3 Deadlock [Autosaved].pptxDishaDwivedi2
 

Similaire à Deadlock _Classic problems.pptx (20)

12 deadlock concept
12 deadlock concept12 deadlock concept
12 deadlock concept
 
Lecture 5 process synchronization
Lecture 5 process synchronizationLecture 5 process synchronization
Lecture 5 process synchronization
 
Critical section operating system
Critical section  operating systemCritical section  operating system
Critical section operating system
 
Operating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronizationOperating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronization
 
Dining Philosopher's Problem
Dining Philosopher's ProblemDining Philosopher's Problem
Dining Philosopher's Problem
 
Classical problems of process synchronization
Classical problems of process synchronizationClassical problems of process synchronization
Classical problems of process synchronization
 
Dining philosopher problem operating system
Dining philosopher problem operating system Dining philosopher problem operating system
Dining philosopher problem operating system
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.ppt
 
Operating systems
Operating systemsOperating systems
Operating systems
 
Operating systems
Operating systemsOperating systems
Operating systems
 
Operating System Process Synchronization
Operating System Process SynchronizationOperating System Process Synchronization
Operating System Process Synchronization
 
Process coordination
Process coordinationProcess coordination
Process coordination
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
 
chapter06-new.pptx
chapter06-new.pptxchapter06-new.pptx
chapter06-new.pptx
 
Synchronization problems
Synchronization problemsSynchronization problems
Synchronization problems
 
Chapter06.ppt
Chapter06.pptChapter06.ppt
Chapter06.ppt
 
Adsa u4 ver 1.0
Adsa u4 ver 1.0Adsa u4 ver 1.0
Adsa u4 ver 1.0
 
4.3 Deadlock [Autosaved].pptx
4.3 Deadlock [Autosaved].pptx4.3 Deadlock [Autosaved].pptx
4.3 Deadlock [Autosaved].pptx
 
Synchronization
SynchronizationSynchronization
Synchronization
 
1 Synchronization.pptx
1 Synchronization.pptx1 Synchronization.pptx
1 Synchronization.pptx
 

Plus de GopikaS12

Introduction to Tree .pptx
Introduction to Tree .pptxIntroduction to Tree .pptx
Introduction to Tree .pptxGopikaS12
 
C Programming language - introduction
C Programming  language - introduction  C Programming  language - introduction
C Programming language - introduction GopikaS12
 
C formatted and unformatted input and output constructs
C  formatted and unformatted input and output constructsC  formatted and unformatted input and output constructs
C formatted and unformatted input and output constructsGopikaS12
 
Heap creation from elements - Step by step Approach
Heap creation  from elements - Step by step Approach Heap creation  from elements - Step by step Approach
Heap creation from elements - Step by step Approach GopikaS12
 
Data Structures: Stack Operations
Data Structures:    Stack OperationsData Structures:    Stack Operations
Data Structures: Stack OperationsGopikaS12
 
Net content in computer architecture
Net content in computer  architectureNet content in computer  architecture
Net content in computer architectureGopikaS12
 

Plus de GopikaS12 (7)

Introduction to Tree .pptx
Introduction to Tree .pptxIntroduction to Tree .pptx
Introduction to Tree .pptx
 
C Programming language - introduction
C Programming  language - introduction  C Programming  language - introduction
C Programming language - introduction
 
C formatted and unformatted input and output constructs
C  formatted and unformatted input and output constructsC  formatted and unformatted input and output constructs
C formatted and unformatted input and output constructs
 
Heap creation from elements - Step by step Approach
Heap creation  from elements - Step by step Approach Heap creation  from elements - Step by step Approach
Heap creation from elements - Step by step Approach
 
Data Structures: Stack Operations
Data Structures:    Stack OperationsData Structures:    Stack Operations
Data Structures: Stack Operations
 
Net content in computer architecture
Net content in computer  architectureNet content in computer  architecture
Net content in computer architecture
 
It unit 1
It   unit 1It   unit 1
It unit 1
 

Dernier

Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...ranjana rawat
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 

Dernier (20)

Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 

Deadlock _Classic problems.pptx

  • 1. OPERATING SYSTEM Classical problems of Synchronization Dr. Gopika S , Department of CS, KJC
  • 2. Classical problems of Synchronization • There are a number of classical problems of synchronization as examples of a large class of concurrency-control problems. semaphores are used as solution for synchronization, since that is the traditional way to present such solutions. • However, actual implementations of these solutions could use mutex locks in place of binary semaphores. • These problems are used for testing nearly every newly proposed synchronization scheme. The following problems of synchronization are considered as classical problems: 1. Bounded-buffer (or Producer-Consumer) Problem, 2. Dining-Philosophers Problem, 3. Readers and Writers Problem,
  • 3. Bounded-buffer (or Producer-Consumer) Problem: • Bounded Buffer problem is also called producer consumer problem. This problem is generalized in terms of the Producer-Consumer problem. • There is one Producer in the producer-consumer problem, Producer is producing some items, whereas there is one Consumer that is consuming the items produced by the Producer. The same memory buffer is shared by both producers and consumers which is of fixed-size. • The task of the Producer is to produce the item, put it into the memory buffer, and again start producing items. Whereas the task of the Consumer is to consume the item from the memory buffer. • Solution to this problem is, creating two counting semaphores “full” and “empty” to keep track of the current number of full and empty buffers respectively. Producers produce a product and consumers consume the product, but both use of one of the containers each time.
  • 5. • The producer should produce data only when the buffer is not full. In case it is found that the buffer is full, the producer is not allowed to store any data into the memory buffer. • Data can only be consumed by the consumer if and only if the memory buffer is not empty. In case it is found that the buffer is empty, the consumer is not allowed to use any data from the memory buffer. • Accessing memory buffer should not be allowed to producer and consumer at the same time.
  • 6. • If the producer is attempting to add an item at the same moment the consumer is removing one, the outcome depends on whether the producer’s check for available space happens before or after the consumer decrements the counter. • A similar race condition arises if the queue is empty and both functions are called. This race condition could be fixed by wrapping the accesses to the counter with a mutex.
  • 7. • Dining-Philosophers Problem: • The Dining Philosopher Problem states that K philosophers seated around a circular table with one chopstick between each pair of philosophers. There is one chopstick between each philosopher. A philosopher may eat if he can pickup the two chopsticks adjacent to him. One chopstick may be picked up by any one of its adjacent followers but not both. This problem involves the allocation of limited resources to a group of processes in a deadlock-free and starvation- free manner.
  • 8. Readers-writers problem Consider a situation where we have a file shared between many people. If one of the person tries editing the file, no other person should be reading or writing at the same time, otherwise changes will not be visible to him/her. However if some person is reading the file, then others may read it at the same time. Precisely in OS we call this situation as the readers-writers problem • Problem parameters: • One set of data is shared among a number of processes • Once a writer is ready, it performs its write. Only one writer may write at a time • If a process is writing, no other process can read it • If at least one reader is reading, no other process can write • Readers may not write and only read
  • 9. Solution 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 There are four Types of cases could happen here. Here priority means, no reader should wait if the share is currently opened for reading. Three variables are used: mutex, wrt, readcnt to implement solution
  • 10. Writer process: • Writer requests the entry to critical section. • If allowed i.e. wait() gives a true value, it enters and performs the write. If not allowed, it keeps on waiting. • It exits the critical section. Reader process: 1.Reader requests the entry to critical section. 2.If allowed: 1. it increments the count of number of readers inside the critical section. If this reader is the first reader entering, it locks the wrt semaphore to restrict the entry of writers if any reader is inside. 2. It then, signals mutex as any other reader is allowed to enter while others are already reading. 3. After performing reading, it exits the critical section. When exiting, it checks if no more reader is inside, it signals the semaphore “wrt” as now, writer can enter the critical section. 3.If not allowed, it keeps on waiting.
  • 11. The Dining Philosopher Problem – • The Dining Philosopher Problem states that K philosophers seated around a circular table with one chopstick between each pair of philosophers. There is one chopstick between each philosopher. A philosopher may eat if he can pick up the two chopsticks adjacent to him. One chopstick may be picked up by any one of its adjacent followers but not both.
  • 12. Semaphore Solution to Dining Philosopher – Each philosopher is represented by the following pseudocode: process P[i] while true do { THINK; PICKUP(CHOPSTICK[i], CHOPSTICK[i+1 mod 5]); EAT; PUTDOWN(CHOPSTICK[i], CHOPSTICK[i+1 mod 5]) } There are three states of the philosopher: THINKING, HUNGRY, and EATING. The dining philosopher demonstrates a large class of concurrency control problems hence it's a classic synchronization problem.
  • 13. 1. Suppose Philosopher P0 wants to eat, it will enter in Philosopher() function, and execute Wait( take_chopstickC[i] ); by doing this it holds C0 chopstick and reduces semaphore C0 to 0, after that it execute Wait( take_chopstickC[(i+1) % 5] ); by doing this it holds C1 chopstick( since i =0, therefore (0 + 1) % 5 = 1) and reduces semaphore C1 to 0 2. Similarly, suppose now Philosopher P1 wants to eat, it will enter in Philosopher() function, and execute Wait( take_chopstickC[i] ); by doing this it will try to hold C1 chopstick but will not be able to do that, since the value of semaphore C1 has already been set to 0 by philosopher P0, therefore it will enter into an infinite loop because of which philosopher P1 will not be able to pick chopstick C1 whereas if Philosopher P2 wants to eat, it will enter in Philosopher() function, and execute Wait( take_chopstickC[i] ); by doing this it holds C2 chopstick and reduces semaphore C2 to 0, after that, it executes Wait( take_chopstickC[(i+1) % 5] ); by doing this it holds C3 chopstick( since i =2, therefore (2 + 1) % 5 = 3) and reduces semaphore C3 to 0.
  • 14. • The drawback of the above solution of the dining philosopher problem • From the above solution of the dining philosopher problem, we have proved that no two neighboring philosophers can eat at the same point in time. The drawback of the above solution is that this solution can lead to a deadlock condition. This situation happens if all the philosophers pick their left chopstick at the same time, which leads to the condition of deadlock and none of the philosophers can eat.
  • 15.
  • 16. What is Deadlock in Operating System (OS)? Every process needs some resources to complete its execution. However, the resource is granted in a sequential order. The process requests for some resource. OS grant the resource if it is available otherwise let the process waits. The process uses it and release on the completion. A Deadlock is a situation where each of the computer process waits for a resource which is being assigned to some another process. In this situation, none of the process gets executed since the resource it needs, is held by some other process which is also waiting for some other resource to be released.
  • 17. Let us assume that there are three processes P1, P2 and P3. There are three different resources R1, R2 and R3. R1 is assigned to P1, R2 is assigned to P2 and R3 is assigned to P3. After some time, P1 demands for R1 which is being used by P2. P1 halts its execution since it can't complete without R2. P2 also demands for R3 which is being used by P3. P2 also stops its execution because it can't continue without R3. P3 also demands for R1 which is being used by P1 therefore P3 also stops its execution. In this scenario, a cycle is being formed among the three processes. None of the process is progressing and they are all waiting. The computer becomes unresponsive since all the processes got blocked.
  • 18. Difference between Starvation and Deadlock Sr. Deadlock Starvation 1 Deadlock is a situation where no process proceeds Starvation is a situation where the low priority process got blocked and the high priority processes proceed. 2 Deadlock is an infinite waiting. Starvation is a long waiting but not infinite. 3 Every Deadlock is always a starvation. Every starvation need not be deadlock. 4 The requested resource is blocked by the other process. The requested resource is continuously be used by the higher priority processes. 5 Deadlock happens when Mutual exclusion, hold and wait, No preemption and circular wait occurs simultaneously. It occurs due to the uncontrolled priority and resource management.
  • 19. Necessary conditions for Deadlocks Mutual Exclusion A resource can only be shared in mutually exclusive manner. It implies, if two process cannot use the same resource at the same time. Hold and Wait A process waits for some resources while holding another resource at the same time. No preemption The process which once scheduled will be executed till the completion. No other process can be scheduled by the scheduler meanwhile. Circular Wait All the processes must be waiting for the resources in a cyclic manner so that the last process is waiting for the resource which is being held by the first process.