SlideShare a Scribd company logo
DEADLOCK PREVENTION &
RECOVERY
I am S.RAJAPRIYA
I am here because I love to give a fright.
You can find me at rajapriya@vvvcollege.org
DEADLOCK
2
✘ The Deadlock Problem
✘ System Model
✘ Deadlock Characterization
✘ Methods for Handling
Deadlocks
✘ Deadlock Prevention
✘ Deadlock Avoidance
✘ Deadlock Detection
✘ Recovery from Deadlock
The Deadlock Problem
A set of blocked processes each holding a resource and
waiting to acquire a resource held by another process in
the set
Example
System has 2 disk drives
P1 and P2 each hold one disk drive and each needs
another one
3
“A Deadlock is a permanent blocking of a
set of threads”
4
SYSTEM MODEL
✘ Resources typesR1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
✘ Each resource type Ri has Wi instances.
✘ Each process utilizes a resource as follows:
✗ request
✗ use
✗ release
5
Mutual exclusion :
only one process at a time can
use a resource. If another process request
that resource, the requesting process must
be delayed until the resource has been
released
Hold and wait :
a process holding at least one
resource is waiting to acquire additional
resources held by other processes
Deadlock Characterization
No preemption :
a resource can be released only
voluntarily by the process holding it, after
that process has completed its task.
Circular wait :
there exists a set {P0, P1, …, P0}
of waiting processes such that P0 is
waiting for a resource that is held by P1,
P1 is waiting for a resource that is held by
P2, …, Pn–1 is waiting for a resource that is
held by
Pn, and P0 is waiting for a resource that is
held by P0.
6
RESOURCE ALLOCATION GRAPH
Deadlock can be described more precisely in terms of a directed
graph called a system resource allocation graph.
A set of vertices V and a set of edges E.
V is partitioned into two types:
• P = {P1, P2, …, Pn}, the set consisting of all the processes in the
system
• R = {R1, R2, …, Rm}, the set consisting of all resource types in the
system
• request edge – directed edge P1  Rj
• assignment edge – directed edge Rj  Pi
Example
7
Resource-Allocation Graph
✘ Pictorially, we represent
✘ Process
✘ Resource Type with 4 instances
✘ Pi requests instance of Rj
✘ Pi is holding an instance of Rj
8
Pi
Pi
Example of a Resource Allocation Graph
• The sets P,R,E:
o P={p1,p2,p3}
o R={R1,R2,R3,R4}
o E={P1->R1,P2->R3,R1->P2,
R2->P2,R2->P1,R3->P3}
9
Resource Allocation Graph with Deadlock
• Suppose that process P3 request an
instance of resource type R3.
• Since no resource is currently
available, a request edge P3->R2 is
added to the graph.
• At this point two cycles exist in the
system:
✘ P1->R1->P2->R3->P3->R2->P1
✘ P2->R3->P3->R2->P2
• Processes P1,P2,P3 are deadlocked.
10
Graph with a Cycle but No Deadlock
• In this example we also have a
cycle
✘ P1->R1->P3->R2->P1
• However, there is no cycle.
• Observe that process P4 may
release its resource R2.That
resource can then be allocated to
P3,breaking the cycle.
11
✘ If graph contains no cycles  no deadlock
✘ If graph contains a cycle 
✗ if only one instance per resource type, then deadlock
✗ if several instances per resource type, possibility of
deadlock
12
DEADLOCK PREVENTION
✘ By ensuring that at least one of these conditions cannot hold,
We can prevent the occurrence of a deadlock
13
Eliminate Mutual Exclusion –
not required for sharable
resources, must hold for non-sharable
resources. For example, a printer cannot
be shared by several processes. Read
only files are good example of sharable
resource
Eliminate Hold and Wait –
must guarantee that
whenever a process requests a
resource, it does not hold any other
resources.
Deadlock Prevention
✗ Allocate all required resources to the
process before the start of its
execution, this way hold and wait
condition is eliminated but it will
lead to low device utilization.
✗ For example, if a process requires
printer at a later time and we have
allocated printer before the start of
its execution printer will remain
blocked till it has completed its
execution.
✗ The process will make a new
request for resources after releasing
the current set of resources. This
solution may lead to starvation.
14
Eliminate No Preemption –
✗ If a process that is holding
some resources requests
another resource that cannot
be immediately allocated to it,
then all resources currently
being held are released.
✗ Preempted resources are
added to the list of resources
for which the process is
waiting.
Deadlock Prevention
✘ Process will be restarted
only when it can regain its
old resources, as well as the
new ones that it is
requesting.
15
Deadlock Prevention
Eliminate Circular Wait –
• Each resource will be assigned
with a numerical number. A
process can request the
resources increasing/decreasing
order of numbering.
• For Example, if P1 process is
allocated R5 resources, now next
time if P1 ask for R4, R3 lesser than
R5 such request will not be granted,
only request for resources more
than R5 will be granted.
16
A picture is worth a thousand words
17
Safe State!When a process requests an available resource, system must decide if
immediate allocation leaves the system in a safe state.
System is in safe state if there exists a safe sequence of all processes.
Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources that Pi
can still request can be satisfied by currently available resources
+ resources held by all the Pj, with j<I.
✗ If Pi resource needs are not immediately available, then Pi can wait
until all Pj have finished.
✗ When Pj is finished, Pi can obtain needed resources, execute,
return allocated resources, and terminate.
✗ When Pi terminates, Pi+1 can obtain its needed resources, and so
on.
18
19
Safe, Unsafe and Deadlock State
Place your screenshot here
Recovery from Deadlock
20
Process Termination
Resource Preemption
Process Termination
Abort all deadlocked
processes
Abort one
process at a
time until the
deadlock
cycle is
eliminated
In which
order should
we choose to
abort?
21
Resource Preemption
Selecting a victim
Starvation –
same process
may always be
picked as victim,
include number of
rollback in cost
factor
Rollback – return
to some safe
state, restart
process for that
state.
22
Thankyou
23

More Related Content

Similar to Deadlock

Os unit 4
Os unit 4Os unit 4
Os unit 4
Krupali Mistry
 
deadlock in OS.pptx
deadlock in OS.pptxdeadlock in OS.pptx
deadlock in OS.pptx
Monirul Islam
 
Mch7 deadlock
Mch7 deadlockMch7 deadlock
Mch7 deadlock
wahab13
 
Deadlocks 160928121516-160928183232
Deadlocks 160928121516-160928183232Deadlocks 160928121516-160928183232
Deadlocks 160928121516-160928183232
marangburu42
 
Sucet os module_3_notes
Sucet os module_3_notesSucet os module_3_notes
Sucet os module_3_notes
SRINIVASUNIVERSITYEN
 
Deadlock
DeadlockDeadlock
Deadlock
Mayuri Verma
 
Deadlock in Operating System
Deadlock in Operating SystemDeadlock in Operating System
Deadlock in Operating System
AUST
 
OS deadlock.pptx
OS deadlock.pptxOS deadlock.pptx
OS deadlock.pptx
rpbbhalerao2000
 
7 Deadlocks
7 Deadlocks7 Deadlocks
7 Deadlocks
Dr. Loganathan R
 
Ch07 deadlocks
Ch07 deadlocksCh07 deadlocks
Ch07 deadlocks
Nazir Ahmed
 
Os module 2 d
Os module 2 dOs module 2 d
Os module 2 d
Gichelle Amon
 
Ch7 deadlocks
Ch7   deadlocksCh7   deadlocks
Ch7 deadlocks
Welly Dian Astika
 
CH07.pdf
CH07.pdfCH07.pdf
CH07.pdf
ImranKhan880955
 
Deadlock.ppt
Deadlock.pptDeadlock.ppt
Deadlock.ppt
JeelBhanderi4
 
Mca ii os u-3 dead lock & io systems
Mca  ii  os u-3 dead lock & io systemsMca  ii  os u-3 dead lock & io systems
Mca ii os u-3 dead lock & io systems
Rai University
 
Os5
Os5Os5
Deadlocks Part- I.pdf
Deadlocks Part- I.pdfDeadlocks Part- I.pdf
Deadlocks Part- I.pdf
Harika Pudugosula
 
Ice
IceIce
Gp1242 007 oer ppt
Gp1242 007 oer pptGp1242 007 oer ppt
Gp1242 007 oer ppt
Nivedita Kasturi
 
Chapter 7 - Deadlocks
Chapter 7 - DeadlocksChapter 7 - Deadlocks
Chapter 7 - Deadlocks
Wayne Jones Jnr
 

Similar to Deadlock (20)

Os unit 4
Os unit 4Os unit 4
Os unit 4
 
deadlock in OS.pptx
deadlock in OS.pptxdeadlock in OS.pptx
deadlock in OS.pptx
 
Mch7 deadlock
Mch7 deadlockMch7 deadlock
Mch7 deadlock
 
Deadlocks 160928121516-160928183232
Deadlocks 160928121516-160928183232Deadlocks 160928121516-160928183232
Deadlocks 160928121516-160928183232
 
Sucet os module_3_notes
Sucet os module_3_notesSucet os module_3_notes
Sucet os module_3_notes
 
Deadlock
DeadlockDeadlock
Deadlock
 
Deadlock in Operating System
Deadlock in Operating SystemDeadlock in Operating System
Deadlock in Operating System
 
OS deadlock.pptx
OS deadlock.pptxOS deadlock.pptx
OS deadlock.pptx
 
7 Deadlocks
7 Deadlocks7 Deadlocks
7 Deadlocks
 
Ch07 deadlocks
Ch07 deadlocksCh07 deadlocks
Ch07 deadlocks
 
Os module 2 d
Os module 2 dOs module 2 d
Os module 2 d
 
Ch7 deadlocks
Ch7   deadlocksCh7   deadlocks
Ch7 deadlocks
 
CH07.pdf
CH07.pdfCH07.pdf
CH07.pdf
 
Deadlock.ppt
Deadlock.pptDeadlock.ppt
Deadlock.ppt
 
Mca ii os u-3 dead lock & io systems
Mca  ii  os u-3 dead lock & io systemsMca  ii  os u-3 dead lock & io systems
Mca ii os u-3 dead lock & io systems
 
Os5
Os5Os5
Os5
 
Deadlocks Part- I.pdf
Deadlocks Part- I.pdfDeadlocks Part- I.pdf
Deadlocks Part- I.pdf
 
Ice
IceIce
Ice
 
Gp1242 007 oer ppt
Gp1242 007 oer pptGp1242 007 oer ppt
Gp1242 007 oer ppt
 
Chapter 7 - Deadlocks
Chapter 7 - DeadlocksChapter 7 - Deadlocks
Chapter 7 - Deadlocks
 

More from Rajapriya82

Applications of IoTs in Home,City,Health
Applications of IoTs in Home,City,HealthApplications of IoTs in Home,City,Health
Applications of IoTs in Home,City,Health
Rajapriya82
 
MD5 ALGORITHM.pptx
MD5 ALGORITHM.pptxMD5 ALGORITHM.pptx
MD5 ALGORITHM.pptx
Rajapriya82
 
contiguous memory allocation.pptx
contiguous memory allocation.pptxcontiguous memory allocation.pptx
contiguous memory allocation.pptx
Rajapriya82
 
SHA 1 Algorithm.ppt
SHA 1 Algorithm.pptSHA 1 Algorithm.ppt
SHA 1 Algorithm.ppt
Rajapriya82
 
File System Implementation.pptx
File System Implementation.pptxFile System Implementation.pptx
File System Implementation.pptx
Rajapriya82
 
Consensus Algorithms.pptx
Consensus Algorithms.pptxConsensus Algorithms.pptx
Consensus Algorithms.pptx
Rajapriya82
 
Security services and mechanisms
Security services and mechanismsSecurity services and mechanisms
Security services and mechanisms
Rajapriya82
 
Wireless transmission
Wireless transmissionWireless transmission
Wireless transmission
Rajapriya82
 
Guided Transmission Media
Guided  Transmission MediaGuided  Transmission Media
Guided Transmission Media
Rajapriya82
 
Input of graphical data
Input of graphical dataInput of graphical data
Input of graphical data
Rajapriya82
 
Clipping
ClippingClipping
Clipping
Rajapriya82
 
Heaptree
HeaptreeHeaptree
Heaptree
Rajapriya82
 
Deadlock
DeadlockDeadlock
Deadlock
Rajapriya82
 

More from Rajapriya82 (13)

Applications of IoTs in Home,City,Health
Applications of IoTs in Home,City,HealthApplications of IoTs in Home,City,Health
Applications of IoTs in Home,City,Health
 
MD5 ALGORITHM.pptx
MD5 ALGORITHM.pptxMD5 ALGORITHM.pptx
MD5 ALGORITHM.pptx
 
contiguous memory allocation.pptx
contiguous memory allocation.pptxcontiguous memory allocation.pptx
contiguous memory allocation.pptx
 
SHA 1 Algorithm.ppt
SHA 1 Algorithm.pptSHA 1 Algorithm.ppt
SHA 1 Algorithm.ppt
 
File System Implementation.pptx
File System Implementation.pptxFile System Implementation.pptx
File System Implementation.pptx
 
Consensus Algorithms.pptx
Consensus Algorithms.pptxConsensus Algorithms.pptx
Consensus Algorithms.pptx
 
Security services and mechanisms
Security services and mechanismsSecurity services and mechanisms
Security services and mechanisms
 
Wireless transmission
Wireless transmissionWireless transmission
Wireless transmission
 
Guided Transmission Media
Guided  Transmission MediaGuided  Transmission Media
Guided Transmission Media
 
Input of graphical data
Input of graphical dataInput of graphical data
Input of graphical data
 
Clipping
ClippingClipping
Clipping
 
Heaptree
HeaptreeHeaptree
Heaptree
 
Deadlock
DeadlockDeadlock
Deadlock
 

Recently uploaded

Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 

Recently uploaded (20)

Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 

Deadlock

  • 1. DEADLOCK PREVENTION & RECOVERY I am S.RAJAPRIYA I am here because I love to give a fright. You can find me at rajapriya@vvvcollege.org
  • 2. DEADLOCK 2 ✘ The Deadlock Problem ✘ System Model ✘ Deadlock Characterization ✘ Methods for Handling Deadlocks ✘ Deadlock Prevention ✘ Deadlock Avoidance ✘ Deadlock Detection ✘ Recovery from Deadlock
  • 3. The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set Example System has 2 disk drives P1 and P2 each hold one disk drive and each needs another one 3
  • 4. “A Deadlock is a permanent blocking of a set of threads” 4
  • 5. SYSTEM MODEL ✘ Resources typesR1, R2, . . ., Rm CPU cycles, memory space, I/O devices ✘ Each resource type Ri has Wi instances. ✘ Each process utilizes a resource as follows: ✗ request ✗ use ✗ release 5
  • 6. Mutual exclusion : only one process at a time can use a resource. If another process request that resource, the requesting process must be delayed until the resource has been released Hold and wait : a process holding at least one resource is waiting to acquire additional resources held by other processes Deadlock Characterization No preemption : a resource can be released only voluntarily by the process holding it, after that process has completed its task. Circular wait : there exists a set {P0, P1, …, P0} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and P0 is waiting for a resource that is held by P0. 6
  • 7. RESOURCE ALLOCATION GRAPH Deadlock can be described more precisely in terms of a directed graph called a system resource allocation graph. A set of vertices V and a set of edges E. V is partitioned into two types: • P = {P1, P2, …, Pn}, the set consisting of all the processes in the system • R = {R1, R2, …, Rm}, the set consisting of all resource types in the system • request edge – directed edge P1  Rj • assignment edge – directed edge Rj  Pi Example 7
  • 8. Resource-Allocation Graph ✘ Pictorially, we represent ✘ Process ✘ Resource Type with 4 instances ✘ Pi requests instance of Rj ✘ Pi is holding an instance of Rj 8 Pi Pi
  • 9. Example of a Resource Allocation Graph • The sets P,R,E: o P={p1,p2,p3} o R={R1,R2,R3,R4} o E={P1->R1,P2->R3,R1->P2, R2->P2,R2->P1,R3->P3} 9
  • 10. Resource Allocation Graph with Deadlock • Suppose that process P3 request an instance of resource type R3. • Since no resource is currently available, a request edge P3->R2 is added to the graph. • At this point two cycles exist in the system: ✘ P1->R1->P2->R3->P3->R2->P1 ✘ P2->R3->P3->R2->P2 • Processes P1,P2,P3 are deadlocked. 10
  • 11. Graph with a Cycle but No Deadlock • In this example we also have a cycle ✘ P1->R1->P3->R2->P1 • However, there is no cycle. • Observe that process P4 may release its resource R2.That resource can then be allocated to P3,breaking the cycle. 11
  • 12. ✘ If graph contains no cycles  no deadlock ✘ If graph contains a cycle  ✗ if only one instance per resource type, then deadlock ✗ if several instances per resource type, possibility of deadlock 12
  • 13. DEADLOCK PREVENTION ✘ By ensuring that at least one of these conditions cannot hold, We can prevent the occurrence of a deadlock 13
  • 14. Eliminate Mutual Exclusion – not required for sharable resources, must hold for non-sharable resources. For example, a printer cannot be shared by several processes. Read only files are good example of sharable resource Eliminate Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. Deadlock Prevention ✗ Allocate all required resources to the process before the start of its execution, this way hold and wait condition is eliminated but it will lead to low device utilization. ✗ For example, if a process requires printer at a later time and we have allocated printer before the start of its execution printer will remain blocked till it has completed its execution. ✗ The process will make a new request for resources after releasing the current set of resources. This solution may lead to starvation. 14
  • 15. Eliminate No Preemption – ✗ If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. ✗ Preempted resources are added to the list of resources for which the process is waiting. Deadlock Prevention ✘ Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. 15
  • 16. Deadlock Prevention Eliminate Circular Wait – • Each resource will be assigned with a numerical number. A process can request the resources increasing/decreasing order of numbering. • For Example, if P1 process is allocated R5 resources, now next time if P1 ask for R4, R3 lesser than R5 such request will not be granted, only request for resources more than R5 will be granted. 16
  • 17. A picture is worth a thousand words 17
  • 18. Safe State!When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. System is in safe state if there exists a safe sequence of all processes. Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j<I. ✗ If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished. ✗ When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate. ✗ When Pi terminates, Pi+1 can obtain its needed resources, and so on. 18
  • 19. 19 Safe, Unsafe and Deadlock State Place your screenshot here
  • 20. Recovery from Deadlock 20 Process Termination Resource Preemption
  • 21. Process Termination Abort all deadlocked processes Abort one process at a time until the deadlock cycle is eliminated In which order should we choose to abort? 21
  • 22. Resource Preemption Selecting a victim Starvation – same process may always be picked as victim, include number of rollback in cost factor Rollback – return to some safe state, restart process for that state. 22