SlideShare a Scribd company logo
1 of 5
Download to read offline
Deadlock
Emin Gun Sirer
22/19/2001
Deadlock
• Deadlock is a problem that can exist when a group of processes
compete for access to fixed resources.
• Def: deadlock exists among a set of processes if every process is
waiting for an event that can be caused only by another process in the
set.
• Example: two processes share 2 resources that they must request
(before using) and release (after using). Request either gives access
or causes the proc. to block until the resource is available.
Proc1: Proc2:
request tape request printer
request printer request tape
… <use them> … <use them>
release printer release tape
release tape release printer
32/19/2001
4 Conditions for Deadlock
• Deadlock can exist if and only if 4 conditions hold
simultaneously:
1. mutual exclusion: at least one resource must be held in a non-
sharable mode.
2. hold and wait: there must be a process holding one resource and
waiting for another.
3. no preemption: resources cannot be preempted.
4. circular wait: there must exist a set of processes
[p1, p2, …, pn] such that p1 is waiting for p2, p2 for p3, and so on
and pn waits for p1….
42/19/2001
Resource Allocation Graph
• Deadlock can be described through a resource allocation
graph.
• The RAG consists of a set of vertices P={P1,P2 ,…,Pn} of
processes and R={R1,R2,…,Rm} of resources.
• A directed edge from a processes to a resource, Pi->Rj, implies
that Pi has requested Rj.
• A directed edge from a resource to a process, Rj->Pi, implies
that Rj has been allocated by Pi.
• If the graph has no cycles, deadlock cannot exist. If the graph
has a cycle, deadlock may exist.
52/19/2001
Resource Allocation Graph Example
.
.
.
.
.
.
.
.
.
.
.
. . . .
R1 R3 R3
R4
R2
P3P2P1
R1
P1 P2 P3
P4
R2 R4
There are two cycles here: P1 -R1-P2-R3-P3-R2-P1
and P2 -R3-P3-R2-P2, and there isdeadlock.
Same cycles, but no deadlock.
62/19/2001
Dealing with Deadlocks
• Deadlock Prevention & Avoidance: Ensure
that the system will never enter a deadlock
state
• Deadlock Detection & Recovery: Detect that a
deadlock has occurred and recover
• Deadlock Ignorance: Pretend that deadlocks
will never occur
72/19/2001
Deadlock Prevention
• Deadlock Prevention: ensure that at least one of
the necessary conditions cannot exist.
– Mutual exclusion: make resources shareable
– Not possible for some resources
– Hold and wait: guarantee that a process cannot hold a
resource when it requests another, or, make processes
request all needed resources at once, or, make it release all
resources before requesting a new set
– Low utilization, starvation
– Preemption: take resources back if there is contention
– Not always possible, hard model to write applications for
– Circular wait: impose an ordering (numbering) on the
resources and request them in order
82/19/2001
Deadlock Prevention
• Most real systems use deadlock prevention through
resource ordering
• The resource order is a convention that the OS
designers must know and follow
– These conventions complicate system programming
• E.g. must always acquire a buffer cache lock before
acquiring the file system lock, before acquiring the
disk lock
• What happens when you get a page fault ?
92/19/2001
Problems with Deadlock Prevention
• Prevention works by restraining how requests are
made
• Might yield low utilization and low throughput
– Certain resource request sequences are not allowed, limiting
functionality
• With sufficient information about future behavior, we
could allow any process to perform any set of
resource accesses
• As long as their actions would not lead to a deadlock
in the future
102/19/2001
Deadlock Avoidance
• Deadlock Avoidance
– general idea: provide info in advance about what resources will be needed
by processes to guarantee that deadlock will not exist.
• E.g., define a set of processes < P1, P2,... Pn> as safe if
for each Pi, the resources that Pi can still request can be
satisfied by the currently available resources plus the
resources held by all Pj, where j < i.
– this avoids circular waiting
– when a process requests a resource, the system grants or forces it to wait,
depending on whether this would be an unsafe state
• All deadlock states are unsafe. An unsafe state may lead to
deadlock. By avoiding unsafe states, we avoid deadlock
112/19/2001
Example:
• Processes p0, p1, and p2 compete for 12 tape drives
max need current usage could ask for
p0 10 5 5
p1 4 2 2
p2 9 2 7
3 drives remain
• current state is safe because a safe sequence exists:
<p1,p0,p2>
p1 can complete with current resources
p0 can complete with current+p1
p2 can complete with current +p1+p0
• if p2 requests 1 drive, then it must wait because that
state would be unsafe.
122/19/2001
The Banker’s Algorithm
• Banker’s algorithm decides whether to grant a resource
request. Define data structures.
n: integer # of processes
m: integer # of resources
available[1..m] avail[i] is # of avail resources of type i
max[1..n,1..m] max demand of each Pi for each Ri
allocation[1..n,1..m] current allocation of resource Rj to Pi
need[1..n,1..m] max # of resource Rj that Pi may still request
let request[i] be a vector of the # of instances of resource Rj that Process Pi
wants.
132/19/2001
The Basic Algorithm
1. If request[i] > need[i] then error (asked for too much)
2. If request[i] > available[i] then wait (can’t supply it now)
3. Resources are available to satisfy the request:
Let’s assume that we satisfy the request. Then we would
have:
available = available - request[i]
allocation[i] = allocation [i] + request[i]
need[i] = need [i] - request [i]
Now, check if this would leave us in a safe state; if yes, grant
the request, if no, then leave the state as is and cause
process to wait.
142/19/2001
Safety Check
1. free[1..m] = available ; how many resources are available
finish[1..n] = false (for all i) ; none finished yet
2. Find an i s.t. finish[i]=false and need[i] <= work
(find a proc that can complete its request now)
if no such i exists, go to step 4 (we’re done)
3. Found an i:
finish [i] = true ; done with this process
free = free + allocation [i] (assume this process were to finish,
and its allocation back to the available list)
go to step 2
4. If finish[i] = true for all i, the system is safe.
152/19/2001
Deadlock Detection
• If there is neither deadlock prevention nor avoidance, then
deadlock may occur.
• In this case, we must have:
– an algorithm that determines whether a deadlock has occurred
– an algorithm to recover from the deadlock
• This is doable, but it’s costly
162/19/2001
Deadlock Detection Algorithm
available[1..m] ; # of available resources
allocation[1..n,1..m] ;# of resource of each Ri allocated to Pj
request[1..n,1..m] ; # of resources of each Ri requested by Pj
1. work=available
for all i < n, if allocation [i] not 0
then finish[i]=false else finish[i]=true
2. find an index i such that:
finish[i]=false;
request[i]<=work
if no such i exists, go to 4.
3. work=work+allocation[i]
finish[i] = true, go to 2
4. if finish[i] = false for some i, then system is deadlocked with Pi in deadlock
172/19/2001
Deadlock
• Deadlock detection algorithm is expensive. How often we
invoke it depends on:
– how often or likely is deadlock
– how many processes are likely to be affected when deadlock occurs
• Running the deadlock detection algorithm often will catch
deadlock cycles early
– Few processes will be affected
– Note: there is no single process that caused the deadlock
– May incur large overhead
182/19/2001
Deadlock Recovery
• Once a deadlock is detected, there are two choices:
1. abort all deadlocked processes (which will cause some
computations to be repeated)
2. abort one process at a time until cycle is eliminated (which requires
re-running the detection algorithm after each abort)
• Or, could do process preemption: release resources until
system can continue. Issues:
– selecting the victim (could be clever based on resources allocated)
– rollback (must rollback the victim to a previous state, may require a
transactional programming model, or functional apps)
– starvation (must not always pick same victim)

More Related Content

What's hot

Deadlock- Operating System
Deadlock- Operating SystemDeadlock- Operating System
Deadlock- Operating SystemRajan Shah
 
Deadlock in Operating System
Deadlock in Operating SystemDeadlock in Operating System
Deadlock in Operating SystemAUST
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating systemSara Ali
 
DeadLock in Operating-Systems
DeadLock in Operating-SystemsDeadLock in Operating-Systems
DeadLock in Operating-SystemsVenkata Sreeram
 
Deadlock detection and recovery by saad symbian
Deadlock detection and recovery by saad symbianDeadlock detection and recovery by saad symbian
Deadlock detection and recovery by saad symbiansaad symbian
 
Building resilient applications
Building resilient applicationsBuilding resilient applications
Building resilient applicationsNuno Caneco
 
Data Mining with Splunk
Data Mining with SplunkData Mining with Splunk
Data Mining with SplunkDavid Carasso
 
OpenStack Summit Tokyo - Know-how of Challlenging Deploy/Operation NTT DOCOMO...
OpenStack Summit Tokyo - Know-how of Challlenging Deploy/Operation NTT DOCOMO...OpenStack Summit Tokyo - Know-how of Challlenging Deploy/Operation NTT DOCOMO...
OpenStack Summit Tokyo - Know-how of Challlenging Deploy/Operation NTT DOCOMO...Masaaki Nakagawa
 
Grehack2013-RuoAndo-Unraveling large scale geographical distribution of vulne...
Grehack2013-RuoAndo-Unraveling large scale geographical distribution of vulne...Grehack2013-RuoAndo-Unraveling large scale geographical distribution of vulne...
Grehack2013-RuoAndo-Unraveling large scale geographical distribution of vulne...Ruo Ando
 
Анализ телеметрии при масштабировании, Theo Schlossnagle (Circonus)
Анализ телеметрии при масштабировании, Theo Schlossnagle (Circonus)Анализ телеметрии при масштабировании, Theo Schlossnagle (Circonus)
Анализ телеметрии при масштабировании, Theo Schlossnagle (Circonus)Ontico
 

What's hot (17)

Deadlock- Operating System
Deadlock- Operating SystemDeadlock- Operating System
Deadlock- Operating System
 
Operating system Deadlock
Operating system DeadlockOperating system Deadlock
Operating system Deadlock
 
Deadlock
DeadlockDeadlock
Deadlock
 
Deadlock in Operating System
Deadlock in Operating SystemDeadlock in Operating System
Deadlock in Operating System
 
Deadlock Avoidance in Operating System
Deadlock Avoidance in Operating SystemDeadlock Avoidance in Operating System
Deadlock Avoidance in Operating System
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
 
Operating system - Deadlock
Operating system - DeadlockOperating system - Deadlock
Operating system - Deadlock
 
Deadlock Presentation
Deadlock PresentationDeadlock Presentation
Deadlock Presentation
 
DeadLock in Operating-Systems
DeadLock in Operating-SystemsDeadLock in Operating-Systems
DeadLock in Operating-Systems
 
deadlock
deadlockdeadlock
deadlock
 
Deadlock detection and recovery by saad symbian
Deadlock detection and recovery by saad symbianDeadlock detection and recovery by saad symbian
Deadlock detection and recovery by saad symbian
 
Building resilient applications
Building resilient applicationsBuilding resilient applications
Building resilient applications
 
Data Mining with Splunk
Data Mining with SplunkData Mining with Splunk
Data Mining with Splunk
 
OpenStack Summit Tokyo - Know-how of Challlenging Deploy/Operation NTT DOCOMO...
OpenStack Summit Tokyo - Know-how of Challlenging Deploy/Operation NTT DOCOMO...OpenStack Summit Tokyo - Know-how of Challlenging Deploy/Operation NTT DOCOMO...
OpenStack Summit Tokyo - Know-how of Challlenging Deploy/Operation NTT DOCOMO...
 
Grehack2013-RuoAndo-Unraveling large scale geographical distribution of vulne...
Grehack2013-RuoAndo-Unraveling large scale geographical distribution of vulne...Grehack2013-RuoAndo-Unraveling large scale geographical distribution of vulne...
Grehack2013-RuoAndo-Unraveling large scale geographical distribution of vulne...
 
Анализ телеметрии при масштабировании, Theo Schlossnagle (Circonus)
Анализ телеметрии при масштабировании, Theo Schlossnagle (Circonus)Анализ телеметрии при масштабировании, Theo Schlossnagle (Circonus)
Анализ телеметрии при масштабировании, Theo Schlossnagle (Circonus)
 
Hystrix 介绍
Hystrix 介绍Hystrix 介绍
Hystrix 介绍
 

Similar to 9 deadlock

Similar to 9 deadlock (20)

7308346-Deadlock.pptx
7308346-Deadlock.pptx7308346-Deadlock.pptx
7308346-Deadlock.pptx
 
Section07-Deadlocks (1).ppt
Section07-Deadlocks (1).pptSection07-Deadlocks (1).ppt
Section07-Deadlocks (1).ppt
 
Section07-Deadlocks.pdf
Section07-Deadlocks.pdfSection07-Deadlocks.pdf
Section07-Deadlocks.pdf
 
deadlock avoidance
deadlock avoidancedeadlock avoidance
deadlock avoidance
 
Deadlock avoidance and prevention .. computer networking
Deadlock avoidance and prevention .. computer networkingDeadlock avoidance and prevention .. computer networking
Deadlock avoidance and prevention .. computer networking
 
OS Presentation 1 (1).pptx
OS Presentation 1 (1).pptxOS Presentation 1 (1).pptx
OS Presentation 1 (1).pptx
 
DeadlockMar21.ppt
DeadlockMar21.pptDeadlockMar21.ppt
DeadlockMar21.ppt
 
Gp1242 007 oer ppt
Gp1242 007 oer pptGp1242 007 oer ppt
Gp1242 007 oer ppt
 
Dead Lock
Dead LockDead Lock
Dead Lock
 
deadlocks.pptx
deadlocks.pptxdeadlocks.pptx
deadlocks.pptx
 
Deadlock (1).ppt
Deadlock (1).pptDeadlock (1).ppt
Deadlock (1).ppt
 
Bankers
BankersBankers
Bankers
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
 
deadlock in OS.pptx
deadlock in OS.pptxdeadlock in OS.pptx
deadlock in OS.pptx
 
Module 3 Deadlocks.pptx
Module 3 Deadlocks.pptxModule 3 Deadlocks.pptx
Module 3 Deadlocks.pptx
 
Deadlock
DeadlockDeadlock
Deadlock
 
24 to 25 deadlockprevention
24 to 25 deadlockprevention24 to 25 deadlockprevention
24 to 25 deadlockprevention
 
Os module 2 d
Os module 2 dOs module 2 d
Os module 2 d
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
chapter06-new.pptx
chapter06-new.pptxchapter06-new.pptx
chapter06-new.pptx
 

More from GRajendra

Weka tutorial
Weka tutorialWeka tutorial
Weka tutorialGRajendra
 
9 normalization
9 normalization9 normalization
9 normalizationGRajendra
 
Unix.system.calls
Unix.system.callsUnix.system.calls
Unix.system.callsGRajendra
 
Mca cloud-storage-report
Mca cloud-storage-reportMca cloud-storage-report
Mca cloud-storage-reportGRajendra
 
Cpu scheduling
Cpu schedulingCpu scheduling
Cpu schedulingGRajendra
 
Ch01 introduction
Ch01 introductionCh01 introduction
Ch01 introductionGRajendra
 
Lec18 pipeline
Lec18 pipelineLec18 pipeline
Lec18 pipelineGRajendra
 

More from GRajendra (10)

Weka tutorial
Weka tutorialWeka tutorial
Weka tutorial
 
9 normalization
9 normalization9 normalization
9 normalization
 
Unix.system.calls
Unix.system.callsUnix.system.calls
Unix.system.calls
 
Mca cloud-storage-report
Mca cloud-storage-reportMca cloud-storage-report
Mca cloud-storage-report
 
Unit 1 ppt
Unit 1 pptUnit 1 ppt
Unit 1 ppt
 
Cpu scheduling
Cpu schedulingCpu scheduling
Cpu scheduling
 
Bca 10
Bca 10Bca 10
Bca 10
 
Ch01 introduction
Ch01 introductionCh01 introduction
Ch01 introduction
 
Chapter 01
Chapter 01Chapter 01
Chapter 01
 
Lec18 pipeline
Lec18 pipelineLec18 pipeline
Lec18 pipeline
 

Recently uploaded

Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 

Recently uploaded (20)

Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 

9 deadlock

  • 1. Deadlock Emin Gun Sirer 22/19/2001 Deadlock • Deadlock is a problem that can exist when a group of processes compete for access to fixed resources. • Def: deadlock exists among a set of processes if every process is waiting for an event that can be caused only by another process in the set. • Example: two processes share 2 resources that they must request (before using) and release (after using). Request either gives access or causes the proc. to block until the resource is available. Proc1: Proc2: request tape request printer request printer request tape … <use them> … <use them> release printer release tape release tape release printer 32/19/2001 4 Conditions for Deadlock • Deadlock can exist if and only if 4 conditions hold simultaneously: 1. mutual exclusion: at least one resource must be held in a non- sharable mode. 2. hold and wait: there must be a process holding one resource and waiting for another. 3. no preemption: resources cannot be preempted. 4. circular wait: there must exist a set of processes [p1, p2, …, pn] such that p1 is waiting for p2, p2 for p3, and so on and pn waits for p1…. 42/19/2001 Resource Allocation Graph • Deadlock can be described through a resource allocation graph. • The RAG consists of a set of vertices P={P1,P2 ,…,Pn} of processes and R={R1,R2,…,Rm} of resources. • A directed edge from a processes to a resource, Pi->Rj, implies that Pi has requested Rj. • A directed edge from a resource to a process, Rj->Pi, implies that Rj has been allocated by Pi. • If the graph has no cycles, deadlock cannot exist. If the graph has a cycle, deadlock may exist.
  • 2. 52/19/2001 Resource Allocation Graph Example . . . . . . . . . . . . . . . R1 R3 R3 R4 R2 P3P2P1 R1 P1 P2 P3 P4 R2 R4 There are two cycles here: P1 -R1-P2-R3-P3-R2-P1 and P2 -R3-P3-R2-P2, and there isdeadlock. Same cycles, but no deadlock. 62/19/2001 Dealing with Deadlocks • Deadlock Prevention & Avoidance: Ensure that the system will never enter a deadlock state • Deadlock Detection & Recovery: Detect that a deadlock has occurred and recover • Deadlock Ignorance: Pretend that deadlocks will never occur 72/19/2001 Deadlock Prevention • Deadlock Prevention: ensure that at least one of the necessary conditions cannot exist. – Mutual exclusion: make resources shareable – Not possible for some resources – Hold and wait: guarantee that a process cannot hold a resource when it requests another, or, make processes request all needed resources at once, or, make it release all resources before requesting a new set – Low utilization, starvation – Preemption: take resources back if there is contention – Not always possible, hard model to write applications for – Circular wait: impose an ordering (numbering) on the resources and request them in order 82/19/2001 Deadlock Prevention • Most real systems use deadlock prevention through resource ordering • The resource order is a convention that the OS designers must know and follow – These conventions complicate system programming • E.g. must always acquire a buffer cache lock before acquiring the file system lock, before acquiring the disk lock • What happens when you get a page fault ?
  • 3. 92/19/2001 Problems with Deadlock Prevention • Prevention works by restraining how requests are made • Might yield low utilization and low throughput – Certain resource request sequences are not allowed, limiting functionality • With sufficient information about future behavior, we could allow any process to perform any set of resource accesses • As long as their actions would not lead to a deadlock in the future 102/19/2001 Deadlock Avoidance • Deadlock Avoidance – general idea: provide info in advance about what resources will be needed by processes to guarantee that deadlock will not exist. • E.g., define a set of processes < P1, P2,... Pn> as safe if for each Pi, the resources that Pi can still request can be satisfied by the currently available resources plus the resources held by all Pj, where j < i. – this avoids circular waiting – when a process requests a resource, the system grants or forces it to wait, depending on whether this would be an unsafe state • All deadlock states are unsafe. An unsafe state may lead to deadlock. By avoiding unsafe states, we avoid deadlock 112/19/2001 Example: • Processes p0, p1, and p2 compete for 12 tape drives max need current usage could ask for p0 10 5 5 p1 4 2 2 p2 9 2 7 3 drives remain • current state is safe because a safe sequence exists: <p1,p0,p2> p1 can complete with current resources p0 can complete with current+p1 p2 can complete with current +p1+p0 • if p2 requests 1 drive, then it must wait because that state would be unsafe. 122/19/2001 The Banker’s Algorithm • Banker’s algorithm decides whether to grant a resource request. Define data structures. n: integer # of processes m: integer # of resources available[1..m] avail[i] is # of avail resources of type i max[1..n,1..m] max demand of each Pi for each Ri allocation[1..n,1..m] current allocation of resource Rj to Pi need[1..n,1..m] max # of resource Rj that Pi may still request let request[i] be a vector of the # of instances of resource Rj that Process Pi wants.
  • 4. 132/19/2001 The Basic Algorithm 1. If request[i] > need[i] then error (asked for too much) 2. If request[i] > available[i] then wait (can’t supply it now) 3. Resources are available to satisfy the request: Let’s assume that we satisfy the request. Then we would have: available = available - request[i] allocation[i] = allocation [i] + request[i] need[i] = need [i] - request [i] Now, check if this would leave us in a safe state; if yes, grant the request, if no, then leave the state as is and cause process to wait. 142/19/2001 Safety Check 1. free[1..m] = available ; how many resources are available finish[1..n] = false (for all i) ; none finished yet 2. Find an i s.t. finish[i]=false and need[i] <= work (find a proc that can complete its request now) if no such i exists, go to step 4 (we’re done) 3. Found an i: finish [i] = true ; done with this process free = free + allocation [i] (assume this process were to finish, and its allocation back to the available list) go to step 2 4. If finish[i] = true for all i, the system is safe. 152/19/2001 Deadlock Detection • If there is neither deadlock prevention nor avoidance, then deadlock may occur. • In this case, we must have: – an algorithm that determines whether a deadlock has occurred – an algorithm to recover from the deadlock • This is doable, but it’s costly 162/19/2001 Deadlock Detection Algorithm available[1..m] ; # of available resources allocation[1..n,1..m] ;# of resource of each Ri allocated to Pj request[1..n,1..m] ; # of resources of each Ri requested by Pj 1. work=available for all i < n, if allocation [i] not 0 then finish[i]=false else finish[i]=true 2. find an index i such that: finish[i]=false; request[i]<=work if no such i exists, go to 4. 3. work=work+allocation[i] finish[i] = true, go to 2 4. if finish[i] = false for some i, then system is deadlocked with Pi in deadlock
  • 5. 172/19/2001 Deadlock • Deadlock detection algorithm is expensive. How often we invoke it depends on: – how often or likely is deadlock – how many processes are likely to be affected when deadlock occurs • Running the deadlock detection algorithm often will catch deadlock cycles early – Few processes will be affected – Note: there is no single process that caused the deadlock – May incur large overhead 182/19/2001 Deadlock Recovery • Once a deadlock is detected, there are two choices: 1. abort all deadlocked processes (which will cause some computations to be repeated) 2. abort one process at a time until cycle is eliminated (which requires re-running the detection algorithm after each abort) • Or, could do process preemption: release resources until system can continue. Issues: – selecting the victim (could be clever based on resources allocated) – rollback (must rollback the victim to a previous state, may require a transactional programming model, or functional apps) – starvation (must not always pick same victim)