deadlock - computer science and...
TRANSCRIPT
![Page 1: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/1.jpg)
Deadlock
CSE 2431: Introduction to Operating SystemsInstructor: Adam C. Champion, Ph.D.Reading: Chap. 7, [OSC]
1
![Page 2: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/2.jpg)
Outline
• Resources• Deadlock• Deadlock Prevention• Deadlock Avoidance• Deadlock Detection• Deadlock Recovery
2
![Page 3: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/3.jpg)
Review: Synchronization• Data races• Critical regions and mutual exclusions• Solutions:– Peterson’s solution– TSL– Semaphores and Mutexes– Monitors– Message Passing– Barriers
• Classic Synchronization Problems 3
![Page 4: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/4.jpg)
Resource (1)• A resource is a commodity needed by a process. • Resources can be either: – Serially reusable: e.g., CPU, memory, disk space, I/O
devices, files. Acquire → use → release
– Consumable: produced by a process, needed by a process; e.g., messages, buffers of information, interrupts. create → acquire → use Resource ceases to exist after it has been used, so it is not released.
4
![Page 5: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/5.jpg)
Resource (2)
• Resources can also be either: – Preemptible: e.g., CPU, central memory or–Non-preemptible: e.g., tape drives.
• And resources can be either: – Shared among several processes or– Dedicated exclusively to a single process.
5
![Page 6: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/6.jpg)
Outline
• Resources• Deadlock• Deadlock Prevention• Deadlock Avoidance• Deadlock Detection• Deadlock Recovery
6
![Page 7: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/7.jpg)
Using Semaphores to Share Resources
7
Process P();{ A.wait();
B.wait();//use both resource
B.signal();A.signal(); }
Process Q();{ A.wait();
B.wait();//use both resource
B.signal();A.signal(); }
4
23
External Semaphore A(1), B(1);External Semaphore A(0), B(1);2External Semaphore A(0), B(0);3External Semaphore A(0), B(1);4External Semaphore A(1), B(1);5
5
06
![Page 8: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/8.jpg)
But Deadlock can Happen!
8
Process P();{ A.wait();
B.wait();//use both resources
B.signal();A.signal(); }
Process Q();{ B.wait();
A.wait();//use both resources
A.signal();B.signal(); }
3211
External Semaphore A(1), B(1);1External Semaphore A(0), B(1);2External Semaphore A(0), B(0);3
DEADLOCK!
![Page 9: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/9.jpg)
Deadlock
9
Mechanisms for Deadlock Control
![Page 10: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/10.jpg)
Deadlock Definition
• What is a deadlock?– A process is deadlocked if it is waiting for an event that will
never occur. – Typically, but not necessarily, more than one process will be
involved together in a deadlock (the deadly embrace).
• Is deadlock the same as starvation (or infinitely postponed)? – A process is infinitely postponed if it is delayed repeatedly
over a long period of time while the attention of the system is given to other processes (i.e., logically the process may proceed but the system never gives it resources).
10
![Page 11: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/11.jpg)
Conditions for Deadlock
• What conditions should exist in order to lead to a deadlock?
• Real life analogies such as–“You take the monitor, I grab the keyboard”–Cars in intersection
–Your turn for real life analogies
11
![Page 12: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/12.jpg)
Necessary Conditions for Deadlock• Mutual exclusion: Processes claim exclusive control of the
resources they require.• Wait-for condition: Processes hold resources already
allocated to them while waiting for additional resources.• No preemption condition: Resources cannot be removed
from the processes holding them until used to completion.• Circular wait condition: A circular chain of processes
exists in which each process holds one or more resources that are requested by the next process in the chain .
12
![Page 13: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/13.jpg)
Resource Allocation Graph
13
![Page 14: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/14.jpg)
Deadlock Model
14
![Page 15: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/15.jpg)
How To Deal With Deadlock?• Prevention: Design a system such that deadlocks cannot
occur, at least with respect to serially reusable resources. • Avoidance: Impose less stringent conditions than for
prevention, allowing the possibility of deadlock, but sidestepping it as it approaches.
• Detection: In a system that allows the possibility of deadlock, determine if deadlock has occurred, and which processes and resources are involved.
• Recovery: After a deadlock has been detected, clear the problem, allowing the deadlocked processes to complete and the resources to be reused. Usually involves destroying the affected processes and restarting them. 15
![Page 16: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/16.jpg)
Outline
• Resources• Deadlock• Deadlock Prevention• Deadlock Avoidance• Deadlock Detection• Deadlock Recovery
16
![Page 17: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/17.jpg)
The Ostrich Algorithm
• Guess: what is implemented in Linux?• Don’t do anything, simply restart the system (stick your
head into the sand, pretend there is no problem at all).• Rationale:
– Make the common path faster and more reliable– Deadlock prevention, avoidance or detection/recovery
algorithms are expensive– If deadlock occurs only rarely, it is not worth the overhead to
implement any of these algorithms.
17
![Page 18: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/18.jpg)
Deadlock Prevention: Havender’sAlgorithms • Break one of the deadlock conditions:– Mutual exclusion
• Solution: exclusive use of resources is an important feature, but for some resources (virtual memory, CPU), it is possible.
– Hold-and-Wait condition• Solution: Force each process to request all required resources at
once. It cannot proceed until all resources have been acquired. – No preemption condition
• Solution: Forcibly take away the resources assigned to the process due to lack of other requested resources.
– Circular wait condition• Solution:All resource types are numbered. Processes must
request resources in numerical order18
![Page 19: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/19.jpg)
Locking with Two Phases• Phase One
– process tries to lock all records it needs, one at a time– If needed record found locked, start over– (No real work done in phase one)
• If phase one succeeds, start Phase Two– Perform updates– Release locks
• Note similarity to requesting all resources at once• Problems?
19
![Page 20: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/20.jpg)
Break Circular Wait Condition
• Request one resource at a time.
• Global ordering of resources– Requests have to be made in increasing order– Req(resource1), req(resource2)..
• Why no circular wait?• Problems?
20
![Page 21: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/21.jpg)
Summary: Deadlock Prevention
Condition How to Break ItMutual exclusion Spool everything
Hold and wait Request all resources initially
No preemption Take resources away
Circular wait Order resources numerically
21
![Page 22: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/22.jpg)
Review
• Resource: preemptible or non-preemptible• Deadlock conditions–Mutual exclusion–Hold and wait–Non-preemption– Circular wait
• Deadlock prevention– Break the condition– Locking with two phases
22
![Page 23: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/23.jpg)
Questions
• Design an algorithm to detect deadlocks?– Single resource per type–Multiple resources per type
• How to avoid deadlocks?
• How to recover from deadlocks?
23
![Page 24: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/24.jpg)
Deadlock Avoidance• The system needs to know the resource ahead of time• Banker’s algorithm (Dijkstra, 1965)
– Each customer tells banker the max. number of resources needed – Customer borrows resources from banker – Customer returns resources to banker – Customer eventually pays back loan – Banker only lends resources if the system will be in a safe state
after the loan
• Safe state: there is a lending sequence such that all customers can take out a loan
• Unsafe state: there is a possibility of deadlock
24
![Page 25: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/25.jpg)
Safe State and Unsafe State• Safe State– there is some scheduling order in which
every process can run to completion even if all of them suddenly request their maximum number of resources immediately
– From a safe state, the system can guarantee that all processes will finish
• Unsafe state: no such guarantee–Not a deadlock state– Some process may be able to complete
25
![Page 26: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/26.jpg)
Is Allocation (1 0 2) to P1 Safe?
26
If P1 requests resources (1 0 2), can it complete
Process Alloc Max Need AvailableA B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 3 3 2P1 2 0 0 3 2 2 1 2 2P2 3 0 2 9 0 2 6 0 0P3 2 1 1 2 2 2 0 1 1P4 0 0 2 4 3 3 4 3 1
![Page 27: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/27.jpg)
Run Safety Test
27
If P1 requests max resources, it can complete.
Process Alloc Max Need AvailableA B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 3 3 2P1 3 0 2 3 2 2 0 2 0 2 3 0P2 3 0 2 9 0 2 6 0 2P3 2 1 1 2 2 2 0 1 1P4 0 0 2 4 3 3 4 3 1
![Page 28: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/28.jpg)
Allocate (0 2 0) to P1
28
Process Alloc Max Need AvailableA B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 2 3 0P1 3 2 2 3 2 2 1 2 2 2 1 0P2 3 0 0 9 0 2 6 0 2P3 2 1 1 2 2 2 0 1 1P4 0 0 2 4 3 3 4 3 1
![Page 29: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/29.jpg)
Release Resources: P1 Finishes
29
Now P3 can acquire max resources and release
Process Alloc Max Need AvailableA B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 2 1 0P1 0 0 0 3 2 2 3 2 2 5 3 2P2 3 0 0 9 0 2 6 0 2P3 2 1 1 2 2 2 0 1 1P4 0 0 2 4 3 3 4 3 1
![Page 30: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/30.jpg)
Release Resources: P3 Finishes
30
Now P4 can acquire max resources and release
Process Alloc Max Need AvailableA B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 5 3 2P1 0 0 0 3 2 2 3 2 2 7 4 3P2 3 0 0 9 0 2 6 0 2P3 0 0 0 2 2 2 2 2 2P4 0 0 2 4 3 3 4 3 1
![Page 31: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/31.jpg)
Release Resources: P4 Finishes
31
Now P2 can acquire max resources and release
Process Alloc Max Need AvailableA B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 7 4 3P1 0 0 0 3 2 2 3 2 2 7 4 5P2 3 0 0 9 0 2 6 0 2P3 0 0 0 2 2 2 2 2 2P4 0 0 0 4 3 3 4 3 3
![Page 32: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/32.jpg)
Release Resources: P2 Finishes
32
Now P0 can acquire max resources and release
Process Alloc Max Need AvailableA B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 7 4 5P1 0 0 0 3 2 2 3 2 2 10 4 5P2 0 0 0 9 0 2 9 0 2P3 0 0 0 2 2 2 2 2 2P4 0 0 0 4 3 3 4 3 3
![Page 33: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/33.jpg)
So P1 Allocation (1 0 2) Is Safe
33
Process Alloc Max Need AvailableA B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 3 3 2P1 3 0 2 3 2 2 0 2 0 2 3 0P2 3 0 0 9 0 2 6 0 2P3 2 1 1 2 2 2 0 1 1P4 0 0 2 4 3 3 4 3 1
![Page 34: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/34.jpg)
Is allocation (0 2 0) to P0 Safe?
34
Try to allocate 2 of resource B to P0…
Process Alloc Max Need AvailableA B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 2 3 0P1 3 0 2 3 2 2 0 2 0P2 3 0 0 9 0 2 6 0 2P3 2 1 1 2 2 2 0 1 1P4 0 0 2 4 3 3 4 3 1
![Page 35: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/35.jpg)
Run Safety Test
35
No process can get its max resources and release them
Process Alloc Max Need AvailableA B C A B C A B C A B C
P0 0 3 0 7 5 3 7 2 3 2 3 0P1 3 0 2 3 2 2 0 2 0 2 1 0P2 3 0 0 9 0 2 6 0 2P3 2 1 1 2 2 2 0 1 1P4 0 0 2 4 3 3 4 3 1
![Page 36: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/36.jpg)
So Unsafe State ⟹ Do Not Enter
36
Return to safe state; do not allocate resource
Process Alloc Max Need AvailableA B C A B C A B C A B C
P0 0 3 0 7 5 3 7 2 3 2 3 0P1 3 0 2 3 2 2 0 2 0 2 1 0P2 3 0 0 9 0 2 6 0 2P3 2 1 1 2 2 2 0 1 1P4 0 0 2 4 3 3 4 3 1
![Page 37: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/37.jpg)
P0 Suspended Pending Request
37
When enough resources become available, P0 can awaken
Process Alloc Max Need AvailableA B C A B C A B C A B C
P0 0 1 0 7 5 3 7 4 3 2 3 0P1 3 0 2 3 2 2 0 2 0P2 3 0 0 9 0 2 6 0 2P3 2 1 1 2 2 2 0 1 1P4 0 0 2 4 3 3 4 3 1
![Page 38: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/38.jpg)
Results
• P0’s request for 2 Bs cannot be granted because that would prevent any other process from completing if they need their maximum claim
38
![Page 39: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/39.jpg)
How to Compute Safety?Given: n kinds of resourcesp processesSet P of processesstruct {resource needs[n], owns[n]} ToDo[p] resource_available[n]......
while there exists p in P such that for all i (ToDo[p].needs[i] < available[i]){
for all i{
available[i] += ToDo[p].owns[i];P = P – {p};
}}
If (P is empty) then system is safe;39
![Page 40: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/40.jpg)
Just Because It’s Unsafe —
• P0 could have been allocated 2 Bs and a deadlock might not have occurred if:– P1 didn’t use its maximum resources but
finished using the resources it had
40
![Page 41: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/41.jpg)
If P1 Doesn’t Need Max—
41
Then P0 would have finished…
Process Alloc Max Need AvailableA B C A B C A B C A B C
P0 3 0 2 1 0P1 0 0 5 1 2P2 0 0P3 1 1P4 0 2
![Page 42: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/42.jpg)
Tradeoff
• What is the tradeoff?• The banker’s algorithm is conservative: it
reduces parallelism for safety’s sake
42
![Page 43: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/43.jpg)
Banker Solution Issues
• Process needs to indicate its resource needs in the beginning
• Process may not terminate• Process may request more than claimed• A process may suffer infinite
postponement (starvation problem)
43
![Page 44: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/44.jpg)
Deadlock Detection
• Check to see if a deadlock has occurred!• Single resource per type– Can use wait-for graph– Check for cycles• How?
• How about multiple resources per type?
44
![Page 45: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/45.jpg)
Wait-For Graphs
45
Resource allocation graph Corresponding wait-for graph
![Page 46: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/46.jpg)
Detection: Multiple Resources per Type• Run variant of banker’s algorithm to see if
processes can finish – Time: O(mn2), m: # resources, n: # processes
• Optimizations?– Optimistic version: check only that at least
one process can finish– The deadlock detection is delayed
• How often should the detection algorithm be run?
46
![Page 47: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/47.jpg)
Recovery From Deadlock• Options:– Kill deadlocked processes and release
resources– Kill one deadlocked process at a time and
release its resources– Rollback all or one of the processes to a
checkpoint that occurred before they requested any resources
• Note: may have starvation problems47
![Page 48: Deadlock - Computer Science and Engineeringweb.cse.ohio-state.edu/~champion.17/2431/06-Deadlock.pdf · prevention, allowing the possibility of deadlock, but sidestepping it as it](https://reader036.vdocuments.us/reader036/viewer/2022081404/5f05df437e708231d41522fd/html5/thumbnails/48.jpg)
Deadlock Summary
• In general, deadlock detection or avoidance is expensive
• Must evaluate cost of deadlock against detection or avoidance costs
• Deadlock avoidance and recovery may cause starvation problems
• UNIX and Windows use Ostrich Algorithm
48