cos 140: foundations of computer science
TRANSCRIPT
![Page 1: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/1.jpg)
CSomputer
cience
Foundations
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 1 / 25
COS 140: Foundations of Computer Science
Handling Deadlocks: Banker’s Algorithm
Fall 2018
![Page 2: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/2.jpg)
CSomputer
cience
Foundations
Homework, reminder
Deadlocks
Deadlocks and digraphs
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 2 / 25
• Chapter 22 (online)
• Homework at the end of chapter
• Homework due 11/16 (later than usual!)
• Prelim II: Wednesday, 11/14
![Page 3: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/3.jpg)
CSomputer
cience
Foundations
Operating systems as resource managers
Deadlocks
• Resource conflicts
• What are deadlocks?
• Conditions for
deadlocks
Deadlocks and digraphs
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 3 / 25
• Example of resources
• Sharable vs non-sharable resources
• Preemptible vs non-preemptible resources
• Potential problem: deadlocks
![Page 4: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/4.jpg)
CSomputer
cience
Foundations
What are deadlocks?
Deadlocks
• Resource conflicts
• What are deadlocks?
• Conditions for
deadlocks
Deadlocks and digraphs
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 4 / 25
• A deadlock occurs when each process in a set of processes is
waiting for some event that only another process in the set can
cause. [after Tannenbaum]
• Example:
![Page 5: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/5.jpg)
CSomputer
cience
Foundations
What are deadlocks?
Deadlocks
• Resource conflicts
• What are deadlocks?
• Conditions for
deadlocks
Deadlocks and digraphs
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 4 / 25
• A deadlock occurs when each process in a set of processes is
waiting for some event that only another process in the set can
cause. [after Tannenbaum]
• Example:
◦ P1: needs CD-ROM and sound card
![Page 6: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/6.jpg)
CSomputer
cience
Foundations
What are deadlocks?
Deadlocks
• Resource conflicts
• What are deadlocks?
• Conditions for
deadlocks
Deadlocks and digraphs
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 4 / 25
• A deadlock occurs when each process in a set of processes is
waiting for some event that only another process in the set can
cause. [after Tannenbaum]
• Example:
◦ P1: needs CD-ROM and sound card
◦ P1: asks for CD-ROM and receives it
![Page 7: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/7.jpg)
CSomputer
cience
Foundations
What are deadlocks?
Deadlocks
• Resource conflicts
• What are deadlocks?
• Conditions for
deadlocks
Deadlocks and digraphs
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 4 / 25
• A deadlock occurs when each process in a set of processes is
waiting for some event that only another process in the set can
cause. [after Tannenbaum]
• Example:
◦ P1: needs CD-ROM and sound card
◦ P1: asks for CD-ROM and receives it
◦ P2: needs CD-ROM and sound card
![Page 8: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/8.jpg)
CSomputer
cience
Foundations
What are deadlocks?
Deadlocks
• Resource conflicts
• What are deadlocks?
• Conditions for
deadlocks
Deadlocks and digraphs
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 4 / 25
• A deadlock occurs when each process in a set of processes is
waiting for some event that only another process in the set can
cause. [after Tannenbaum]
• Example:
◦ P1: needs CD-ROM and sound card
◦ P1: asks for CD-ROM and receives it
◦ P2: needs CD-ROM and sound card
◦ P2: asks for sound card and gets it
![Page 9: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/9.jpg)
CSomputer
cience
Foundations
What are deadlocks?
Deadlocks
• Resource conflicts
• What are deadlocks?
• Conditions for
deadlocks
Deadlocks and digraphs
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 4 / 25
• A deadlock occurs when each process in a set of processes is
waiting for some event that only another process in the set can
cause. [after Tannenbaum]
• Example:
◦ P1: needs CD-ROM and sound card
◦ P1: asks for CD-ROM and receives it
◦ P2: needs CD-ROM and sound card
◦ P2: asks for sound card and gets it
◦ P1: asks for sound card ⇒ blocks
![Page 10: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/10.jpg)
CSomputer
cience
Foundations
What are deadlocks?
Deadlocks
• Resource conflicts
• What are deadlocks?
• Conditions for
deadlocks
Deadlocks and digraphs
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 4 / 25
• A deadlock occurs when each process in a set of processes is
waiting for some event that only another process in the set can
cause. [after Tannenbaum]
• Example:
◦ P1: needs CD-ROM and sound card
◦ P1: asks for CD-ROM and receives it
◦ P2: needs CD-ROM and sound card
◦ P2: asks for sound card and gets it
◦ P1: asks for sound card ⇒ blocks
◦ P2: asks for CD-ROM ⇒ blocks
![Page 11: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/11.jpg)
CSomputer
cience
Foundations
Conditions for deadlocks
Deadlocks
• Resource conflicts
• What are deadlocks?
• Conditions for
deadlocks
Deadlocks and digraphs
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 5 / 25
Mutual exclusion: Resource is either available or assigned to at
most one process
![Page 12: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/12.jpg)
CSomputer
cience
Foundations
Conditions for deadlocks
Deadlocks
• Resource conflicts
• What are deadlocks?
• Conditions for
deadlocks
Deadlocks and digraphs
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 5 / 25
Mutual exclusion: Resource is either available or assigned to at
most one process
Hold-and-wait: Process can hold one resource and then ask for
others
![Page 13: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/13.jpg)
CSomputer
cience
Foundations
Conditions for deadlocks
Deadlocks
• Resource conflicts
• What are deadlocks?
• Conditions for
deadlocks
Deadlocks and digraphs
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 5 / 25
Mutual exclusion: Resource is either available or assigned to at
most one process
Hold-and-wait: Process can hold one resource and then ask for
others
No preemption: Can’t take a resource away from a process once
assigned
![Page 14: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/14.jpg)
CSomputer
cience
Foundations
Conditions for deadlocks
Deadlocks
• Resource conflicts
• What are deadlocks?
• Conditions for
deadlocks
Deadlocks and digraphs
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 5 / 25
Mutual exclusion: Resource is either available or assigned to at
most one process
Hold-and-wait: Process can hold one resource and then ask for
others
No preemption: Can’t take a resource away from a process once
assigned
Circular wait: ≥ 2 processes in circle in which each is waiting for
resource held by next in circle
![Page 15: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/15.jpg)
CSomputer
cience
Foundations
Digression: Directed graphs
Deadlocks
Deadlocks and digraphs
• Digraphs
• Modeling deadlocks
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 6 / 25
• Many areas of CS require us to think of objects and relationships
between them; e.g., paths between locations, data
dependencies, constraints in logic puzzles
![Page 16: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/16.jpg)
CSomputer
cience
Foundations
Digression: Directed graphs
Deadlocks
Deadlocks and digraphs
• Digraphs
• Modeling deadlocks
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 6 / 25
• Many areas of CS require us to think of objects and relationships
between them; e.g., paths between locations, data
dependencies, constraints in logic puzzles
• Can represent this formally as a graph:
◦ Vertices (or nodes) represent the objects
◦ Edges (or arcs, or links) represent the relationships
![Page 17: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/17.jpg)
CSomputer
cience
Foundations
Digression: Directed graphs
Deadlocks
Deadlocks and digraphs
• Digraphs
• Modeling deadlocks
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 6 / 25
• Many areas of CS require us to think of objects and relationships
between them; e.g., paths between locations, data
dependencies, constraints in logic puzzles
• Can represent this formally as a graph:
◦ Vertices (or nodes) represent the objects
◦ Edges (or arcs, or links) represent the relationships
• Sometimes, relationship is directional
◦ Think “one-way streets”
◦ Now the edges have direction, and the graph is called a
directed graph or digraph
![Page 18: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/18.jpg)
CSomputer
cience
Foundations
Modeling deadlocks as digraphs
Deadlocks
Deadlocks and digraphs
• Digraphs
• Modeling deadlocks
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 7 / 25
• Circles: processes
• Squares: resources
• Link from process → resource: process requests resource
• Link from resource → process: process has control of resource
P1
Printer
P1
Printer
![Page 19: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/19.jpg)
CSomputer
cience
Foundations
Modeling deadlocks as digraphs
Deadlocks
Deadlocks and digraphs
• Digraphs
• Modeling deadlocks
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 8 / 25
Proc.A
Proc.B
CD-ROMSoundCard
![Page 20: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/20.jpg)
CSomputer
cience
Foundations
Modeling deadlocks as digraphs
Deadlocks
Deadlocks and digraphs
• Digraphs
• Modeling deadlocks
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 8 / 25
Proc.A
Proc.B
CD-ROMSoundCard
![Page 21: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/21.jpg)
CSomputer
cience
Foundations
Modeling deadlocks as digraphs
Deadlocks
Deadlocks and digraphs
• Digraphs
• Modeling deadlocks
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 8 / 25
Proc.A
Proc.B
CD-ROMSoundCard
![Page 22: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/22.jpg)
CSomputer
cience
Foundations
Modeling deadlocks as digraphs
Deadlocks
Deadlocks and digraphs
• Digraphs
• Modeling deadlocks
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 8 / 25
Proc.A
Proc.B
CD-ROMSoundCard
![Page 23: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/23.jpg)
CSomputer
cience
Foundations
Modeling deadlocks as digraphs
Deadlocks
Deadlocks and digraphs
• Digraphs
• Modeling deadlocks
Handling deadlocks
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 8 / 25
Proc.A
Proc.B
CD-ROMSoundCard
![Page 24: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/24.jpg)
CSomputer
cience
Foundations
What do we do about deadlocks?
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 9 / 25
• Ignore them
• Detect them and (try to) recover
• Prevent them altogether
• Predict and avoid them
![Page 25: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/25.jpg)
CSomputer
cience
Foundations
Ignoring deadlocks: The Ostrich Algorithm
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 10 / 25
• Sounds stupid, but...
• Consider:
◦ How often will a deadlock happen?
◦ How severe will it be if it does happen?
◦ How hard would it be to avoid/prevent/detect?
![Page 26: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/26.jpg)
CSomputer
cience
Foundations
Deadlock detection/recovery
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 11 / 25
• Detection:
◦ Monitor resource allocation using (e.g.) a digraph
◦ If detect a cycle ⇒ deadlock has occurred
• Recovery:
◦ Kill one of the processes
◦ If that doesn’t work: kill another, etc.
• Another alternative: just look for processes that have been idle
for a long time and kill them
• May be okay when aborting and restarting is okay (e.g., batch
jobs)
![Page 27: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/27.jpg)
CSomputer
cience
Foundations
Deadlock prevention
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 12 / 25
• Set things up so that deadlocks cannot occur at all
• Done by attacking one of the deadlock conditions
• Attacking mutual exclusion condition:
◦ Don’t let non-sharable resources be assigned to anyone
◦ E.g., spooling
![Page 28: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/28.jpg)
CSomputer
cience
Foundations
Deadlock prevention
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 13 / 25
• Attacking hold-and-wait condition:
◦ Process can’t request a resource if holding any
◦ One way: processes request all resources up front
◦ Problem: may not know ahead of time what you need!
◦ Problem: hold resources too long in general
◦ Another approach: release all you’re holding momentarily to
request another
• Attacking no preemption condition: not realistic
![Page 29: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/29.jpg)
CSomputer
cience
Foundations
Deadlock prevention
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 14 / 25
• Attacking the circular wait condition:
◦ Stupid way: processes can only hold a single resource at a
time
![Page 30: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/30.jpg)
CSomputer
cience
Foundations
Deadlock prevention
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 14 / 25
• Attacking the circular wait condition:
◦ Stupid way: processes can only hold a single resource at a
time
◦ Better way:
• Number the resources
• Process can request whatever it wants, whenever it
wants...as long as the requests are in numerical order
![Page 31: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/31.jpg)
CSomputer
cience
Foundations
Deadlock prevention
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 15 / 25
• Attacking the circular wait condition (cont’d):
◦ Resource allocation graph can’t have cycles in this scheme –
why not?
![Page 32: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/32.jpg)
CSomputer
cience
Foundations
Deadlock prevention
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 15 / 25
• Attacking the circular wait condition (cont’d):
◦ Resource allocation graph can’t have cycles in this scheme –
why not?
• Consider the case where process A holds resource i and
B holds j – deadlock only possible if A requests j and B
requests i
![Page 33: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/33.jpg)
CSomputer
cience
Foundations
Deadlock prevention
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 15 / 25
• Attacking the circular wait condition (cont’d):
◦ Resource allocation graph can’t have cycles in this scheme –
why not?
• Consider the case where process A holds resource i and
B holds j – deadlock only possible if A requests j and B
requests i
• If i > j, then A can’t request j
![Page 34: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/34.jpg)
CSomputer
cience
Foundations
Deadlock prevention
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 15 / 25
• Attacking the circular wait condition (cont’d):
◦ Resource allocation graph can’t have cycles in this scheme –
why not?
• Consider the case where process A holds resource i and
B holds j – deadlock only possible if A requests j and B
requests i
• If i > j, then A can’t request j
• If j > i, then B can’t request i
![Page 35: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/35.jpg)
CSomputer
cience
Foundations
Deadlock prevention
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 15 / 25
• Attacking the circular wait condition (cont’d):
◦ Resource allocation graph can’t have cycles in this scheme –
why not?
• Consider the case where process A holds resource i and
B holds j – deadlock only possible if A requests j and B
requests i
• If i > j, then A can’t request j
• If j > i, then B can’t request i
◦ Problem – may not be able to find an ordering that satisfies
everyone!
![Page 36: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/36.jpg)
CSomputer
cience
Foundations
Deadlock avoidance
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 16 / 25
• Idea: predict when some action → deadlock, avoid it
• Dijkstra’s Banker’s Algorithm (single resource version)
◦ Modeled on the way a banker might deal with lines of credit to
customers
◦ Deadlock if there is no way to guarantee that all customers
can borrow up to their maximum resource limit at some point
in time
![Page 37: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/37.jpg)
CSomputer
cience
Foundations
Dijkstra’s Banker’s Algorithm
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 17 / 25
• Safety:
A state is safe if some sequence of other possible
states exists that allows all customers (processes) to
get up to their maximum resource limit at some time
• Keep track of maxmimum and current allocation for each
customer
• Start in a safe state
• When process requests additional amount of resource, make
sure that next state will also be safe
• If so, allow request, else disallow it
![Page 38: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/38.jpg)
CSomputer
cience
Foundations
Banker’s Algorithm Example
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 18 / 25
• Initial state:
Process Current Maximum
A 0 7
B 0 3
C 0 2
D 0 4
Remaining: 8
![Page 39: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/39.jpg)
CSomputer
cience
Foundations
Banker’s Algorithm Example 2
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 19 / 25
• Initial state:
Process Current Maximum
A 3 7
B 2 3
C 0 2
D 2 4
Remaining: 1
• Safe or not?
![Page 40: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/40.jpg)
CSomputer
cience
Foundations
Banker’s Algorithm Example 2
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 19 / 25
• Initial state:
Process Current Maximum
A 3 7
B 2 3
C 0 2
D 2 4
Remaining: 1
• Safe or not?
◦ Safe
◦ Possible sequence of processes running to completion: B →
D → C → A
![Page 41: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/41.jpg)
CSomputer
cience
Foundations
Banker’s Algorithm Example 2
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 19 / 25
• Initial state:
Process Current Maximum
A 3 7
B 3 3
C 0 2
D 2 4
Remaining: 0
• Safe or not?
◦ Safe
◦ Possible sequence of processes running to completion: B →
D → C → A
![Page 42: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/42.jpg)
CSomputer
cience
Foundations
Banker’s Algorithm Example 2
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 19 / 25
• Initial state:
Process Current Maximum
A 3 7
B – –
C 0 2
D 2 4
Remaining: 3
• Safe or not?
◦ Safe
◦ Possible sequence of processes running to completion: B →
D → C → A
![Page 43: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/43.jpg)
CSomputer
cience
Foundations
Banker’s Algorithm Example 2
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 19 / 25
• Initial state:
Process Current Maximum
A 3 7
B – –
C 0 2
D 4 4
Remaining: 1
• Safe or not?
◦ Safe
◦ Possible sequence of processes running to completion: B →
D → C → A
![Page 44: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/44.jpg)
CSomputer
cience
Foundations
Banker’s Algorithm Example 2
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 19 / 25
• Initial state:
Process Current Maximum
A 3 7
B – –
C 0 2
D – –
Remaining: 5
• Safe or not?
◦ Safe
◦ Possible sequence of processes running to completion: B →
D → C → A
![Page 45: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/45.jpg)
CSomputer
cience
Foundations
Banker’s Algorithm Example 2
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 19 / 25
• Initial state:
Process Current Maximum
A 3 7
B – –
C 2 2
D – –
Remaining: 3
• Safe or not?
◦ Safe
◦ Possible sequence of processes running to completion: B →
D → C → A
![Page 46: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/46.jpg)
CSomputer
cience
Foundations
Banker’s Algorithm Example 2
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 19 / 25
• Initial state:
Process Current Maximum
A 3 7
B – –
C – –
D – –
Remaining: 5
• Safe or not?
◦ Safe
◦ Possible sequence of processes running to completion: B →
D → C → A
![Page 47: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/47.jpg)
CSomputer
cience
Foundations
Banker’s Algorithm Example 2
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 19 / 25
• Initial state:
Process Current Maximum
A 7 7
B – –
C – –
D – –
Remaining: 1
• Safe or not?
◦ Safe
◦ Possible sequence of processes running to completion: B →
D → C → A
![Page 48: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/48.jpg)
CSomputer
cience
Foundations
Banker’s Algorithm Example 2
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 19 / 25
• Initial state:
Process Current Maximum
A – –
B – –
C – –
D – –
Remaining: 8
• Safe or not?
◦ Safe
◦ Possible sequence of processes running to completion: B →
D → C → A
![Page 49: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/49.jpg)
CSomputer
cience
Foundations
Banker’s Algorithm Example 3
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 20 / 25
Proc Curr Max Proc Curr Max
A 3 7 A 3 7
B 2 3B wants 1=⇒ B 3 3
C 0 2 C 0 2
D 2 4 D 2 4
Remaining: 1 Remaining: 0
• Allow the request?
![Page 50: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/50.jpg)
CSomputer
cience
Foundations
Banker’s Algorithm Example 3
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 20 / 25
Proc Curr Max Proc Curr Max
A 3 7 A 3 7
B 2 3B wants 1=⇒ B 3 3
C 0 2 C 0 2
D 2 4 D 2 4
Remaining: 1 Remaining: 0
• Allow the request?
◦ Yes.
◦ Possible sequence of processes running to completion: B →
D → C → A
![Page 51: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/51.jpg)
CSomputer
cience
Foundations
Banker’s Algorithm Example 4
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 21 / 25
Proc Curr Max Proc Curr Max
A 6 7 A 7 7
B 0 3A wants 1=⇒ B 0 3
C 0 2 C 0 2
D 1 4 D 1 4
Remaining: 1 Remaining: 0
• Allow the request?
![Page 52: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/52.jpg)
CSomputer
cience
Foundations
Banker’s Algorithm Example 4
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 21 / 25
Proc Curr Max Proc Curr Max
A 6 7 A 7 7
B 0 3A wants 1=⇒ B 0 3
C 0 2 C 0 2
D 1 4 D 1 4
Remaining: 1 Remaining: 0
• Allow the request?
◦ Yes.
◦ Possible sequence of processes running to completion: A →
B → C → D
![Page 53: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/53.jpg)
CSomputer
cience
Foundations
Banker’s Algorithm Example 5
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 22 / 25
Proc Curr Max Proc Curr Max
A 4 7 A 4 7
B 1 3D wants 1=⇒ B 1 3
C 0 2 C 0 2
D 1 4 D 2 4
Remaining: 2 Remaining: 1
• Allow the request?
![Page 54: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/54.jpg)
CSomputer
cience
Foundations
Banker’s Algorithm Example 5
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 22 / 25
Proc Curr Max Proc Curr Max
A 4 7 A 4 7
B 1 3D wants 1=⇒ B 1 3
C 0 2 C 0 2
D 1 4 D 2 4
Remaining: 2 Remaining: 1
• Allow the request?
◦ NO!
◦ No sequence possible where they all can finish
![Page 55: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/55.jpg)
CSomputer
cience
Foundations
Banker’s Algorithm: Critique
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 23 / 25
• Is it too strong?
• After all – no guarantee that in the previous example:
Proc Curr Max Proc Curr Max
A 4 7 A 4 7
B 1 3D wants 1=⇒ B 1 3
C 0 2 C 0 2
D 1 4 D 2 4
Remaining: 2 Remaining: 1
D might not give back 1 immediately, moving back into safe state
• But we’re interested in guarantee that there will be no deadlock,
so this is what we need.
![Page 56: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/56.jpg)
CSomputer
cience
Foundations
Banker’s Algorithm: Critique
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 24 / 25
• Scales to multiple processes/resources
• Problems:
◦ Need to know maximum resources needed per process –
often (usually?) impossible for multiprocess system
◦ Number of processes constantly changes
◦ Resources can disappear
• But: Really no better general-purpose algorithm exists for this
![Page 57: COS 140: Foundations of Computer Science](https://reader031.vdocuments.net/reader031/viewer/2022013101/61d1cf4adddcc132bc26124b/html5/thumbnails/57.jpg)
CSomputer
cience
Foundations
Summary
Deadlocks
Deadlocks and digraphs
Handling deadlocks
• How to handle?
• Ignore them
• Detection/recover
• Prevention
• Avoidance
• Banker’s Algorithm
• Examples
• Critique
• Summary
Copyright c© 2002–2018 UMaine School of Computing and Information Science – 25 / 25
• Handling deadlocks is difficult
• No best general solution
• How you choose to handle it depends on your situation: trade-offs