SlideShare a Scribd company logo
Deadlocks
Week 6
Deadlocks
• Resource
• Introduction to deadlocks
• The ostrich algorithm
• Deadlock detection and recovery
• Deadlock avoidance
• Deadlock prevention
• Other issues
3
Resources
• Examples of computer resources
– Printers
– tape drives
– Tables (systems internal table)
– Semaphores ( critical section)
• Processes need access to resources in reasonable order
• Suppose a process holds resource A and requests
resource B
– at same time another process holds B and requests A
– both are blocked and remain so……… this situation is called
DEADLOCK
I/O device
( hardware)
4
Resources (1)
• Deadlocks occur when …
– processes are granted exclusive access to devices
– we refer to these devices generally as resources.
 Resourses :
 A resource Can be hardware device ( eg. Tape drive, printer) ,
 a piece of information ( a locked record in a database )
 For some resources several identical instances are available ( 3 tape
drive)
• A resource is any thing that must be acquired , used. And
released over the course of time
Resources (2)
• Preemptable resources
– can be taken away from a process with no ill
effects
– eg : Memory ( by swapping )
• Nonpreemptable resources
– will cause the process to fail if taken away
– eg: CD ROM ( result garbled CD – Computational failure)
In general deadlock involve with preemptable resourses can
be solved by reallocationg resourses from one process to
another .
Our discussion focus on nonpreemtable resourses
6
Backing Store
(a) Paging to static swap area
(b) Backing up pages dynamically
7
Resources (2)
• Sequence of events required to use a
resource
1. request the resource
2. use the resource
3. release the resource
• Must wait if request is denied
– requesting process may be blocked ( eg:semaphore)
– may fail with error code ( error notice)
 Request can ne placed by request system call( I/O device, tables ect.)
 For some kind of resources ( critical section resources) the user
process can manage using semaphores
8
Introduction to Deadlocks
• Formal definition :
A set of processes is deadlocked if each process in the set is waiting
for an event that only another process in the set can cause
• Usually the event is release of a currently held resource
• None of the processes can …
– run
– release resources
– be awakened
This kind of deadlock is called Resource Deadlock( this is probably
the most common kind )
9
Four Conditions for Resource Deadlock
1. Mutual exclusion condition
• each resource assigned to 1 process or is available
1. Hold and wait condition
• process holding resources can request additional
1. No preemption condition
• previously granted resources cannot forcibly taken away
1. Circular wait condition
• must be a circular chain of 2 or more processes
• each is waiting for resource held by next member of the
chain
• The four condition relates to a policy that a
system can have /not have
 Can a given resource be assigned to more than one
process at once ?
 Can a process hold a resource and ask for another?
 Can resource be preemted?
 Can circular wait exist?
11
Deadlock Modeling (1)
• Modeled with directed graphs
( 1972 Holt showed how the four condition can be modeled)
– resource R assigned to process A
– process B is requesting/waiting for resource S
– process C and D are in deadlock over resources T and U
12
Deadlock Modeling (2)
Resource allocation graphs
• Resource allocation
modeled by directed
graphs
• Example 1:
– Resource R assigned to
process A
• Example 2:
– Process B is requesting /
waiting for resource S
• Example 3:
– Process C holds T, waiting for
U
– Process D holds U, waiting
for T
R
A
S
B
U
T
DC
13How deadlock occurs
A B C
Deadlock Modeling (3)
14
Deadlock Modeling (5)
How deadlock can be avoided
(o) (p) (q)
Four Strategies dealing with dead lock
1. Just ignore the problem . May be if you ignore it, it will
ignore you
2.Detection & recovery. Let deadlock happen, detect them
and take action
3.Dynamic avoidance by careful resource allocation
4.Prevention, by structurally negating one of the four
condition.
16
The Ostrich Algorithm
• Pretend there is no problem
“Hide your head in the sand and pretend there is no problem at all “
• Reasonable if
– deadlocks occur very rarely
– cost of prevention is high
• It is a trade off between
– convenience
– correctness
2.Detection & recovery
• The system does not prevent deadlock from
occurring .Instead …………….
-it lets them occur
-tries to detect when this happens
-and take some action to recover after that.
18
Detecting deadlocks using graphs
An example : Detection with One Resource of Each Type
•Process holdings and requests in the table and in the graph
(they’re equivalent)
•Graph contains a cycle => deadlock!
– Easy to pick out by looking at it (in this case)
– Need to mechanically detect deadlock
•Not all processes are deadlocked (A, C, F not in deadlock)
R A
S
F
W
C
Process Holds Wants
A R S
B T
C S
D U S,T
E T V
F W S
G V U
ED
G
B
T
VU
19
Deadlock detection algorithm
• General idea: try to find
cycles in the resource
allocation graph
• Algorithm: depth-first
search at each node
– Mark arcs as they’re
traversed
– Build list of visited nodes
– If node to be added is
already on the list, a cycle
exists!
• Cycle == deadlock
For each node N in the graph {
Set L = empty list
unmark all arcs
Traverse (N,L)
}
If no deadlock reported by now, there isn’t any
define Traverse (C,L) {
If C in L, report deadlock!
Add C to L
For each unmarked arc from C {
Mark the arc
Set A = arc destination
/* NOTE: L is a
local variable */
Traverse (A,L)
}
}
20
Resources with multiple instances
• Previous algorithm only works if there’s one
instance of each resource
• If there are multiple instances of each
resource, we need a different method
– Track current usage and requests for each process
– To detect deadlock, try to find a scenario where
all processes can finish
– If no such scenario exists, we have deadlock
Example
•E=(4, 2, 3, 1 ) A=(2, 1, 0, 0)
( Resource in existence) ( Resource Available)
•3 processes
0 0 1 0 2 0 0 1
c= 2 0 0 1 R= 1 0 1 0
0 1 2 0 2 1 0 0
current allocation matrix Request matrix
-p3 can run and return all its resources giving A=(2 , 2, 2 ,0 )
- At this point p2 can run A( 4 2 2 1)
-Now P1 can run no dead lock in the system
Tape , plotters, scanner, Cd rom Tape , plotters, scanner, Cd rom
Chapter 3 22
Deadlock detection algorithm
A B C D
Avail 2 3 0 1
Process A B C D
1 0 3 0 0
2 1 0 1 1
3 0 2 1 0
4 2 2 3 0
Process A B C D
1 3 2 1 0
2 2 2 0 0
3 3 5 3 1
4 0 4 1 1
HoldWant
current=avail;
for (j = 0; j < N; j++) {
for (k=0; k<N; k++) {
if (finished[k])
continue;
if (want[k] < current) {
finished[k] = 1;
current += hold[k];
break;
}
if (k==N) {
printf “Deadlock!n”;
// finished[k]==0 means process is in
// the deadlock
break;
}
}
Note: want[j],hold[j],current,avail are arrays!
23
Recovery from Deadlock (1)
• Recovery through preemption
– take a resource from some other process
– depends on nature of the resource
• Recovery through rollback
– checkpoint a process periodically
– use this saved state
– restart the process if it is found deadlocked
24
Recovery from Deadlock (2)
• Recovery through killing processes
– crudest but simplest way to break a deadlock
– kill one of the processes in the deadlock cycle
– the other processes get its resources
– choose process that can be rerun from the
beginning
Dead Lock Avoidance
• Question
Is there an algorithm that can avoid deadlock
by making the right choice at right time????
Ans: Yes – we can avoid dead lock , but only if
certain information is available in advance
The main Algorithm for avoiding algorithm is
based on safe state
26
Deadlock Avoidance
Resource Trajectories
Two process resource trajectories
Safe & Unsafe Stataes
• At any Instant of time we have E, A , C, A
• A state is said to be safe if there is some scheduling order in
which every process can run to completion even if all of them
suddenly request there maximum number of resources
immediate
Example: Three processes A, B, C , & one resource type ( 10
Instances ( copies))
Safe and unsafe states
Has Max
A 3 9
B 2 4
C 2 7
Free: 3
Has Max
A 3 9
B 4 4
C 2 7
Free: 1
Has Max
A 3 9
B 0 -
C 2 7
Free: 5
Has Max
A 3 9
B 0 -
C 7 7
Free: 0
Has Max
A 3 9
B 0 -
C 0 -
Free: 7
Demonstration that the first state is safe
Has Max
A 3 9
B 2 4
C 2 7
Free: 3
Has Max
A 4 9
B 2 4
C 2 7
Free: 2
Has Max
A 4 9
B 4 4
C 2 7
Free: 0
Has Max
A 4 9
B 0 -
C 2 7
Free: 4
Demonstration that the second state is unsafe
Banker's Algorithm for a single resource
( Dijkstra
• It is an extension of deadlock detection algorithm ( One
resource of each type)
• It is modeled one the way a small time banker who is dealing
with customer to whom he has to granted credits
• Example : Four customers . To whom hes has granted a
certain number of credits ( Iunit- 1K$)
(in our case customers are process , cdret – resources ( say tape
driver) and the banker is the OS
Banker's Algorithm for a single resource ( Dijkstra)
Has Max
A 0 6
B 0 5
C 0 4
D 0 7
Free: 10
Has Max
A 1 6
B 1 5
C 2 4
D 4 7
Free: 2
Has Max
A 1 6
B 2 5
C 2 4
D 4 7
Free: 1
• Bankers’ algorithm: before granting a request, ensure that a sequence exists
that will allow all processes to complete
– Use previous methods to find such a sequence
– If a sequence exists, allow the requests
– If there’s no such sequence, deny the request
• Can be slow: must be done on each request!
Any sequence finishes C,B,A,D finishes Deadlock (unsafe state)
Process D can finish , then A or E followed by rge rest
Example of banker's algorithm with multiple resources
Banker's Algorithm for multiple
resources
C
R
Chapter 3 32CS 1550, cs.pitt.edu (originaly
modified by Ethan L. Miller and
Preventing deadlock
• Deadlock can be completely prevented!
• Ensure that at least one of the conditions for
deadlock never occurs
– Mutual exclusion
– Circular wait
– Hold & wait
– No preemption
• Not always possible…
Chapter 3 33
Eliminating mutual exclusion
• Some devices (such as printer) can be spooled
– Only the printer daemon uses printer resource
– This eliminates deadlock for printer
• Not all devices can be spooled
• Principle:
– Avoid assigning resource when not absolutely
necessary
– As few processes as possible actually claim the
resource
34
Attacking “hold and wait”
• Require processes to request resources before starting
– A process never has to wait for what it needs
• This can present problems
– A process may not know required resources at start of run
– This also ties up resources other processes could be using
• Processes will tend to be conservative and request resources
they might need
• Variation: a process must give up all resources before making a new
request
– Process is then granted all prior resources as well as the new ones
– Problem: what if someone grabs the resources in the meantime—
how can the process save its state?
Chapter 3 35CS 1550, cs.pitt.edu (originaly
modified by Ethan L. Miller and
Attacking “no preemption”
• This is not usually a viable option
• Consider a process given the printer
– Halfway through its job, take away the printer
– Confusion ensues!
• May work for some resources
– Forcibly take away memory pages, suspending the process
– Process may be able to resume with no ill effects
Chapter 3 36CS 1550, cs.pitt.edu (originaly
modified by Ethan L. Miller and
Attacking “circular wait”
• Assign an order to
resources
• Always acquire
resources in numerical
order
– Need not acquire them
all at once!
• Circular wait is
prevented
– A process holding
resource n can’t wait for
resource m
if m < n
A
1
B
C
D
23
37
Deadlock prevention: summary
• Mutual exclusion
– Spool everything
• Hold and wait
– Request all resources initially
• No preemption
– Take resources away
• Circular wait
– Order resources numerically

More Related Content

PPTX
System protection in Operating System
PDF
module view decomposition
PPTX
Confidentiality using symmetric encryption.pptx
PPTX
A presentation on software crisis
PPTX
Chapter 10 designing and producing Multimedia
PPT
Virus and Malicious Code Chapter 5
PPT
Z buffer
PPTX
Design Model & User Interface Design in Software Engineering
System protection in Operating System
module view decomposition
Confidentiality using symmetric encryption.pptx
A presentation on software crisis
Chapter 10 designing and producing Multimedia
Virus and Malicious Code Chapter 5
Z buffer
Design Model & User Interface Design in Software Engineering

What's hot (20)

PPTX
Software Engineering concept
PPT
Unit 1 - Introduction to Software Engineering.ppt
PPTX
Initial design (Game Architecture)
PPTX
Evolving role of Software
PPTX
Rsa algorithm key generation
PPTX
Context model
PDF
Software Architecture - Architecture Styles and Patterns-1.pdf
PPTX
Multimedia System & Design Ch 1, 2, 3 Multimedia
PPTX
CA_Module_1.pptx
PDF
Digital Image Processing: An Introduction
PDF
Function Point Analysis (FPA) by Dr. B. J. Mohite
PDF
Computer Vision: Correlation, Convolution, and Gradient
PPTX
Matlab Working With Images
PPTX
Software Project Management - Staffing
PPT
Architecture design in software engineering
PPT
Software Engineering (Introduction to Software Engineering)
PPT
image enhancement
PPTX
Staffing level estimation
PPT
Software Engineering (Software Process: A Generic View)
PPTX
Modes of Operation
Software Engineering concept
Unit 1 - Introduction to Software Engineering.ppt
Initial design (Game Architecture)
Evolving role of Software
Rsa algorithm key generation
Context model
Software Architecture - Architecture Styles and Patterns-1.pdf
Multimedia System & Design Ch 1, 2, 3 Multimedia
CA_Module_1.pptx
Digital Image Processing: An Introduction
Function Point Analysis (FPA) by Dr. B. J. Mohite
Computer Vision: Correlation, Convolution, and Gradient
Matlab Working With Images
Software Project Management - Staffing
Architecture design in software engineering
Software Engineering (Introduction to Software Engineering)
image enhancement
Staffing level estimation
Software Engineering (Software Process: A Generic View)
Modes of Operation
Ad

Viewers also liked (16)

PPTX
Deadlocks in operating system
PPT
Operating System Deadlock Galvin
PPT
Ch9: Memory Management
PPT
Mch7 deadlock
PPTX
Deadlock
PPT
Deadlocks in operating system
PDF
Distributed deadlock
PPT
Os Swapping, Paging, Segmentation and Virtual Memory
PPTX
Memory management
PPT
Chapter 7 - Deadlocks
PDF
Basic Computer Organization and Design
PDF
Deadlocks
PPT
Microprogram Control
PDF
Memory management
PPT
Introduction to PHP
Deadlocks in operating system
Operating System Deadlock Galvin
Ch9: Memory Management
Mch7 deadlock
Deadlock
Deadlocks in operating system
Distributed deadlock
Os Swapping, Paging, Segmentation and Virtual Memory
Memory management
Chapter 7 - Deadlocks
Basic Computer Organization and Design
Deadlocks
Microprogram Control
Memory management
Introduction to PHP
Ad

Similar to Deadlocks (20)

PDF
OS-Part-06.pdf
PPT
A ppt on deadlock in operating systems for the better explanation
PPT
3 (2).ppt
PDF
9 deadlock
PPTX
Algorithm 4Chapter Four- Deadlock (5).pptx
PPTX
Deadlock
PPT
Chapter 03
PPT
3 (1) [Autosaved].ppt
PPT
Os module 2 d
PPTX
Module 3 Deadlocks.pptx
PPTX
Gp1242 007 oer ppt
PPTX
Deadlocks
PPT
Principles of Operating system and types
PPTX
7308346-Deadlock.pptx
PPT
Dead Lock
PPTX
Chapter 4
PPTX
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
PPTX
Deadlock and memory management -- Operating System
PPT
DeadlockMar21.ppt.....................................
OS-Part-06.pdf
A ppt on deadlock in operating systems for the better explanation
3 (2).ppt
9 deadlock
Algorithm 4Chapter Four- Deadlock (5).pptx
Deadlock
Chapter 03
3 (1) [Autosaved].ppt
Os module 2 d
Module 3 Deadlocks.pptx
Gp1242 007 oer ppt
Deadlocks
Principles of Operating system and types
7308346-Deadlock.pptx
Dead Lock
Chapter 4
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
Deadlock and memory management -- Operating System
DeadlockMar21.ppt.....................................

Recently uploaded (20)

PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Basic Mud Logging Guide for educational purpose
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Complications of Minimal Access Surgery at WLH
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
01-Introduction-to-Information-Management.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Cell Types and Its function , kingdom of life
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Basic Mud Logging Guide for educational purpose
VCE English Exam - Section C Student Revision Booklet
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Module 4: Burden of Disease Tutorial Slides S2 2025
Complications of Minimal Access Surgery at WLH
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPH.pptx obstetrics and gynecology in nursing
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
01-Introduction-to-Information-Management.pdf
O7-L3 Supply Chain Operations - ICLT Program
Cell Types and Its function , kingdom of life
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
RMMM.pdf make it easy to upload and study
102 student loan defaulters named and shamed – Is someone you know on the list?
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester

Deadlocks

  • 2. Deadlocks • Resource • Introduction to deadlocks • The ostrich algorithm • Deadlock detection and recovery • Deadlock avoidance • Deadlock prevention • Other issues
  • 3. 3 Resources • Examples of computer resources – Printers – tape drives – Tables (systems internal table) – Semaphores ( critical section) • Processes need access to resources in reasonable order • Suppose a process holds resource A and requests resource B – at same time another process holds B and requests A – both are blocked and remain so……… this situation is called DEADLOCK I/O device ( hardware)
  • 4. 4 Resources (1) • Deadlocks occur when … – processes are granted exclusive access to devices – we refer to these devices generally as resources.  Resourses :  A resource Can be hardware device ( eg. Tape drive, printer) ,  a piece of information ( a locked record in a database )  For some resources several identical instances are available ( 3 tape drive) • A resource is any thing that must be acquired , used. And released over the course of time
  • 5. Resources (2) • Preemptable resources – can be taken away from a process with no ill effects – eg : Memory ( by swapping ) • Nonpreemptable resources – will cause the process to fail if taken away – eg: CD ROM ( result garbled CD – Computational failure) In general deadlock involve with preemptable resourses can be solved by reallocationg resourses from one process to another . Our discussion focus on nonpreemtable resourses
  • 6. 6 Backing Store (a) Paging to static swap area (b) Backing up pages dynamically
  • 7. 7 Resources (2) • Sequence of events required to use a resource 1. request the resource 2. use the resource 3. release the resource • Must wait if request is denied – requesting process may be blocked ( eg:semaphore) – may fail with error code ( error notice)  Request can ne placed by request system call( I/O device, tables ect.)  For some kind of resources ( critical section resources) the user process can manage using semaphores
  • 8. 8 Introduction to Deadlocks • Formal definition : A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause • Usually the event is release of a currently held resource • None of the processes can … – run – release resources – be awakened This kind of deadlock is called Resource Deadlock( this is probably the most common kind )
  • 9. 9 Four Conditions for Resource Deadlock 1. Mutual exclusion condition • each resource assigned to 1 process or is available 1. Hold and wait condition • process holding resources can request additional 1. No preemption condition • previously granted resources cannot forcibly taken away 1. Circular wait condition • must be a circular chain of 2 or more processes • each is waiting for resource held by next member of the chain
  • 10. • The four condition relates to a policy that a system can have /not have  Can a given resource be assigned to more than one process at once ?  Can a process hold a resource and ask for another?  Can resource be preemted?  Can circular wait exist?
  • 11. 11 Deadlock Modeling (1) • Modeled with directed graphs ( 1972 Holt showed how the four condition can be modeled) – resource R assigned to process A – process B is requesting/waiting for resource S – process C and D are in deadlock over resources T and U
  • 12. 12 Deadlock Modeling (2) Resource allocation graphs • Resource allocation modeled by directed graphs • Example 1: – Resource R assigned to process A • Example 2: – Process B is requesting / waiting for resource S • Example 3: – Process C holds T, waiting for U – Process D holds U, waiting for T R A S B U T DC
  • 13. 13How deadlock occurs A B C Deadlock Modeling (3)
  • 14. 14 Deadlock Modeling (5) How deadlock can be avoided (o) (p) (q)
  • 15. Four Strategies dealing with dead lock 1. Just ignore the problem . May be if you ignore it, it will ignore you 2.Detection & recovery. Let deadlock happen, detect them and take action 3.Dynamic avoidance by careful resource allocation 4.Prevention, by structurally negating one of the four condition.
  • 16. 16 The Ostrich Algorithm • Pretend there is no problem “Hide your head in the sand and pretend there is no problem at all “ • Reasonable if – deadlocks occur very rarely – cost of prevention is high • It is a trade off between – convenience – correctness
  • 17. 2.Detection & recovery • The system does not prevent deadlock from occurring .Instead ……………. -it lets them occur -tries to detect when this happens -and take some action to recover after that.
  • 18. 18 Detecting deadlocks using graphs An example : Detection with One Resource of Each Type •Process holdings and requests in the table and in the graph (they’re equivalent) •Graph contains a cycle => deadlock! – Easy to pick out by looking at it (in this case) – Need to mechanically detect deadlock •Not all processes are deadlocked (A, C, F not in deadlock) R A S F W C Process Holds Wants A R S B T C S D U S,T E T V F W S G V U ED G B T VU
  • 19. 19 Deadlock detection algorithm • General idea: try to find cycles in the resource allocation graph • Algorithm: depth-first search at each node – Mark arcs as they’re traversed – Build list of visited nodes – If node to be added is already on the list, a cycle exists! • Cycle == deadlock For each node N in the graph { Set L = empty list unmark all arcs Traverse (N,L) } If no deadlock reported by now, there isn’t any define Traverse (C,L) { If C in L, report deadlock! Add C to L For each unmarked arc from C { Mark the arc Set A = arc destination /* NOTE: L is a local variable */ Traverse (A,L) } }
  • 20. 20 Resources with multiple instances • Previous algorithm only works if there’s one instance of each resource • If there are multiple instances of each resource, we need a different method – Track current usage and requests for each process – To detect deadlock, try to find a scenario where all processes can finish – If no such scenario exists, we have deadlock
  • 21. Example •E=(4, 2, 3, 1 ) A=(2, 1, 0, 0) ( Resource in existence) ( Resource Available) •3 processes 0 0 1 0 2 0 0 1 c= 2 0 0 1 R= 1 0 1 0 0 1 2 0 2 1 0 0 current allocation matrix Request matrix -p3 can run and return all its resources giving A=(2 , 2, 2 ,0 ) - At this point p2 can run A( 4 2 2 1) -Now P1 can run no dead lock in the system Tape , plotters, scanner, Cd rom Tape , plotters, scanner, Cd rom
  • 22. Chapter 3 22 Deadlock detection algorithm A B C D Avail 2 3 0 1 Process A B C D 1 0 3 0 0 2 1 0 1 1 3 0 2 1 0 4 2 2 3 0 Process A B C D 1 3 2 1 0 2 2 2 0 0 3 3 5 3 1 4 0 4 1 1 HoldWant current=avail; for (j = 0; j < N; j++) { for (k=0; k<N; k++) { if (finished[k]) continue; if (want[k] < current) { finished[k] = 1; current += hold[k]; break; } if (k==N) { printf “Deadlock!n”; // finished[k]==0 means process is in // the deadlock break; } } Note: want[j],hold[j],current,avail are arrays!
  • 23. 23 Recovery from Deadlock (1) • Recovery through preemption – take a resource from some other process – depends on nature of the resource • Recovery through rollback – checkpoint a process periodically – use this saved state – restart the process if it is found deadlocked
  • 24. 24 Recovery from Deadlock (2) • Recovery through killing processes – crudest but simplest way to break a deadlock – kill one of the processes in the deadlock cycle – the other processes get its resources – choose process that can be rerun from the beginning
  • 25. Dead Lock Avoidance • Question Is there an algorithm that can avoid deadlock by making the right choice at right time???? Ans: Yes – we can avoid dead lock , but only if certain information is available in advance The main Algorithm for avoiding algorithm is based on safe state
  • 26. 26 Deadlock Avoidance Resource Trajectories Two process resource trajectories
  • 27. Safe & Unsafe Stataes • At any Instant of time we have E, A , C, A • A state is said to be safe if there is some scheduling order in which every process can run to completion even if all of them suddenly request there maximum number of resources immediate Example: Three processes A, B, C , & one resource type ( 10 Instances ( copies))
  • 28. Safe and unsafe states Has Max A 3 9 B 2 4 C 2 7 Free: 3 Has Max A 3 9 B 4 4 C 2 7 Free: 1 Has Max A 3 9 B 0 - C 2 7 Free: 5 Has Max A 3 9 B 0 - C 7 7 Free: 0 Has Max A 3 9 B 0 - C 0 - Free: 7 Demonstration that the first state is safe Has Max A 3 9 B 2 4 C 2 7 Free: 3 Has Max A 4 9 B 2 4 C 2 7 Free: 2 Has Max A 4 9 B 4 4 C 2 7 Free: 0 Has Max A 4 9 B 0 - C 2 7 Free: 4 Demonstration that the second state is unsafe
  • 29. Banker's Algorithm for a single resource ( Dijkstra • It is an extension of deadlock detection algorithm ( One resource of each type) • It is modeled one the way a small time banker who is dealing with customer to whom he has to granted credits • Example : Four customers . To whom hes has granted a certain number of credits ( Iunit- 1K$) (in our case customers are process , cdret – resources ( say tape driver) and the banker is the OS
  • 30. Banker's Algorithm for a single resource ( Dijkstra) Has Max A 0 6 B 0 5 C 0 4 D 0 7 Free: 10 Has Max A 1 6 B 1 5 C 2 4 D 4 7 Free: 2 Has Max A 1 6 B 2 5 C 2 4 D 4 7 Free: 1 • Bankers’ algorithm: before granting a request, ensure that a sequence exists that will allow all processes to complete – Use previous methods to find such a sequence – If a sequence exists, allow the requests – If there’s no such sequence, deny the request • Can be slow: must be done on each request! Any sequence finishes C,B,A,D finishes Deadlock (unsafe state)
  • 31. Process D can finish , then A or E followed by rge rest Example of banker's algorithm with multiple resources Banker's Algorithm for multiple resources C R
  • 32. Chapter 3 32CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Preventing deadlock • Deadlock can be completely prevented! • Ensure that at least one of the conditions for deadlock never occurs – Mutual exclusion – Circular wait – Hold & wait – No preemption • Not always possible…
  • 33. Chapter 3 33 Eliminating mutual exclusion • Some devices (such as printer) can be spooled – Only the printer daemon uses printer resource – This eliminates deadlock for printer • Not all devices can be spooled • Principle: – Avoid assigning resource when not absolutely necessary – As few processes as possible actually claim the resource
  • 34. 34 Attacking “hold and wait” • Require processes to request resources before starting – A process never has to wait for what it needs • This can present problems – A process may not know required resources at start of run – This also ties up resources other processes could be using • Processes will tend to be conservative and request resources they might need • Variation: a process must give up all resources before making a new request – Process is then granted all prior resources as well as the new ones – Problem: what if someone grabs the resources in the meantime— how can the process save its state?
  • 35. Chapter 3 35CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Attacking “no preemption” • This is not usually a viable option • Consider a process given the printer – Halfway through its job, take away the printer – Confusion ensues! • May work for some resources – Forcibly take away memory pages, suspending the process – Process may be able to resume with no ill effects
  • 36. Chapter 3 36CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Attacking “circular wait” • Assign an order to resources • Always acquire resources in numerical order – Need not acquire them all at once! • Circular wait is prevented – A process holding resource n can’t wait for resource m if m < n A 1 B C D 23
  • 37. 37 Deadlock prevention: summary • Mutual exclusion – Spool everything • Hold and wait – Request all resources initially • No preemption – Take resources away • Circular wait – Order resources numerically