cs 333 introduction to operating systems class 7 - deadlock
DESCRIPTION
Jonathan Walpole Computer Science Portland State University. CS 333 Introduction to Operating Systems Class 7 - Deadlock. Resources and deadlocks. Processes need access to resources in order to make progress Examples of computer resources printers disk drives - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/1.jpg)
1
CS 333Introduction to Operating Systems
Class 7 - Deadlock
Jonathan WalpoleComputer Science
Portland State University
![Page 2: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/2.jpg)
2
Resources and deadlocks
Processes need access to resources in order to make progress
Examples of computer resources printers disk drives kernel data structures (scheduling queues …) locks/semaphores to protect critical sections
Suppose a process holds resource A and requests resource B
at the same time another process holds B and requests A both are blocked and remain so … this is deadlock
![Page 3: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/3.jpg)
3
Deadlock modeling: resource usage model
Sequence of events required to use a resource
request the resource (like acquiring a mutex lock) use the resource release the resource (like releasing a mutex lock)
Must wait if request is denied block busy wait fail with error code
![Page 4: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/4.jpg)
4
Preemptable vs nonpreemptable resources
Preemptable resources can be taken away from a process with no ill effects
Nonpreemptable resources will cause the holding process to fail if taken away May corrupt the resource itself
Deadlocks occur when processes are granted exclusive access to non-preemptable resources and wait when the resource is not available
![Page 5: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/5.jpg)
5
Definition of deadlock
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 the release of a currently held resource
None of the processes can … be awakened run release resources
![Page 6: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/6.jpg)
6
Deadlock conditions
A deadlock situation can occur if and only if the following conditions hold simultaneously
Mutual exclusion condition – resource assigned to one process only
Hold and wait condition – processes can get more than one resource
No preemption condition
Circular wait condition – chain of two or more processes (must be waiting for resource from next one in chain)
![Page 7: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/7.jpg)
7
Examples of deadlock
![Page 8: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/8.jpg)
8
Resource acquisition scenarios
acquire (resource_1)use resource_1release (resource_1)
Thread A:
Example: var r1_mutex: Mutex ... r1_mutex.Lock() Use resource_1 r1_mutex.Unlock()
![Page 9: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/9.jpg)
9
Resource acquisition scenarios
Thread A:
acquire (resource_1)use resource_1release (resource_1)
Another Example: var r1_sem: Semaphore r1_sem.Up() ... r1_sem.Down() Use resource_1 r1_sem.Up()
![Page 10: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/10.jpg)
10
Resource acquisition scenarios
acquire (resource_2)use resource_2release (resource_2)
Thread A: Thread B:
acquire (resource_1)use resource_1release (resource_1)
![Page 11: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/11.jpg)
11
Resource acquisition scenarios
acquire (resource_2)use resource_2release (resource_2)
Thread A: Thread B:
No deadlock can occur here!
acquire (resource_1)use resource_1release (resource_1)
![Page 12: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/12.jpg)
12
Resource acquisition scenarios: 2 resources
acquire (resource_1)acquire (resource_2)use resources 1 & 2release (resource_2)release (resource_1)
acquire (resource_1)acquire (resource_2)use resources 1 & 2release (resource_2)release (resource_1)
Thread A: Thread B:
![Page 13: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/13.jpg)
13
Resource acquisition scenarios: 2 resources
acquire (resource_1)acquire (resource_2)use resources 1 & 2release (resource_2)release (resource_1)
acquire (resource_1)acquire (resource_2)use resources 1 & 2release (resource_2)release (resource_1)
Thread A: Thread B:
No deadlock can occur here!
![Page 14: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/14.jpg)
14
Resource acquisition scenarios: 2 resources
acquire (resource_1)use resources 1release (resource_1)acquire (resource_2)use resource 2release (resource_2)
acquire (resource_2)use resources 2release (resource_2)acquire (resource_1)use resource 1release (resource_1)
Thread A: Thread B:
![Page 15: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/15.jpg)
15
Resource acquisition scenarios: 2 resources
acquire (resource_1)use resources 1release (resource_1)acquire (resource_2)use resource 2release (resource_2)
acquire (resource_2)use resources 2release (resource_2)acquire (resource_1)use resource 1release (resource_1)
Thread A: Thread B:
No deadlock can occur here!
![Page 16: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/16.jpg)
16
Resource acquisition scenarios: 2 resources
acquire (resource_1)acquire (resource_2)use resources 1 & 2release (resource_2)release (resource_1)
acquire (resource_2)acquire (resource_1)use resources 1 & 2release (resource_1)release (resource_2)
Thread A: Thread B:
![Page 17: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/17.jpg)
17
Resource acquisition scenarios: 2 resources
acquire (resource_1)acquire (resource_2)use resources 1 & 2release (resource_2)release (resource_1)
acquire (resource_2)acquire (resource_1)use resources 1 & 2release (resource_1)release (resource_2)
Thread A: Thread B:
Deadlock is possible!
![Page 18: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/18.jpg)
18
Dealing with deadlock
Four general strategies Ignore the problem
• Hmm… advantages, disadvantages?
Detection and recovery
Dynamic avoidance via careful resource allocation
Prevention, by structurally negating one of the four necessary conditions
![Page 19: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/19.jpg)
19
Deadlock detection
Let the problem happen, then recover
How do you know it happened?
Do a depth-first-search on the resource allocation graph
![Page 20: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/20.jpg)
20
Detection: Resource Allocation Graphs
Resource
R
AProcess/Thread
![Page 21: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/21.jpg)
21
Detection: Resource Allocation Graphs
Resource
R
AProcess/Thread
“is held by”
![Page 22: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/22.jpg)
22
Detection: Resource Allocation Graphs
R
A
“is requesting”
S
Resource
Process/Thread
Resource
![Page 23: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/23.jpg)
23
Detection: Resource Allocation Graphs
R
A S
B
![Page 24: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/24.jpg)
24
Detection: Resource Allocation Graphs
Deadlock
R
A S
B
![Page 25: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/25.jpg)
25
Detection: Resource Allocation Graphs
Deadlock = a cycle in the graph
R
A S
B
![Page 26: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/26.jpg)
26
Deadlock detection (1 resource of each)
Do a depth-first-search on the resource allocation graph
![Page 27: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/27.jpg)
27
Deadlock detection (1 resource of each)
Do a depth-first-search on the resource allocation graph
![Page 28: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/28.jpg)
28
Deadlock detection (1 resource of each)
Do a depth-first-search on the resource allocation graph
![Page 29: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/29.jpg)
29
Deadlock detection (1 resource of each)
Do a depth-first-search on the resource allocation graph
![Page 30: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/30.jpg)
30
Deadlock detection (1 resource of each)
Do a depth-first-search on the resource allocation graph
Deadlock!
![Page 31: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/31.jpg)
31
Mulitple units/instances of a resource
Some resources have only one “unit”. Only one thread at a time may hold the resource.
• Printer• Lock on ReadyQueue
Some resources have several units. All units are considered equal; any one will do.
• Page Frames• Dice in the Gaming Parlor problem
A thread requests “k” units of the resource. Several requests may be satisfied simultaneously.
![Page 32: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/32.jpg)
32
Deadlock modeling with multiple resources
Theorem: If a graph does not contain a cycle then no processes are deadlocked
A cycle in a RAG is a necessary condition for deadlock
Is it a sufficient condition?
![Page 33: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/33.jpg)
33
Deadlock modeling with multiple resources
Theorem: If a graph does not contain a cycle then no processes are deadlocked
A cycle in a RAG is a necessary condition for deadlock
Is it a sufficient condition?
![Page 34: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/34.jpg)
34
Deadlock detection issues
How often should the algorithm run?
On every resource request?
Periodically?
When CPU utilization is low?
When we suspect deadlock because some thread has been asleep for a long period of time?
![Page 35: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/35.jpg)
35
Recovery from deadlock
If we detect deadlock, what should be done to recover?
Abort deadlocked processes and reclaim resources Abort one process at a time until deadlock cycle is
eliminated
Where to start? Lowest priority process? Shortest running process? Process with fewest resources held? Batch processes before interactive processes? Minimize number of processes to be terminated?
![Page 36: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/36.jpg)
36
Other deadlock recovery techniques
How do we prevent the resource becoming corrupted
For example, shared variables protected by a lock?
Recovery through preemption and rollback Save state periodically (at start of critical section)
• take a checkpoint of memory• start computation again from checkpoint
– Checkpoint must be prior to resource acquisition! Useful for long-lived computation systems
![Page 37: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/37.jpg)
37
Deadlock avoidance
Detection vs. avoidance…
Detection – “optimistic” approach• Allocate resources• “Break” system to fix the problem if necessary
Avoidance – “pessimistic” approach• Don’t allocate resource if it may lead to
deadlock• If a process requests a resource... ... make it wait until you are sure it’s OK
Which one to use depends upon the application• And how easy is it to recover from deadlock!
![Page 38: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/38.jpg)
38
Avoidance using process-resource trajectories
time
Process At1 t2 t3 t4
![Page 39: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/39.jpg)
39
Avoidance using process-resource trajectories
time
Process At1 t2 t3 t4
Requests Printer
Requests CD-RW
Releases Printer
Releases CD-RW
![Page 40: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/40.jpg)
40
Avoidance using process-resource trajectories
tim
eP
roce
ss B
tW
tX
tY
tZ
![Page 41: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/41.jpg)
41
Avoidance using process-resource trajectories
tim
eP
roce
ss B
tW
tX
tY
tZ Requests CD-RW
Requests Printer
Releases CD-RW
Releases Printer
![Page 42: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/42.jpg)
42
Avoidance using process-resource trajectories
Pro
cess
B
tW
tX
tY
tZ
Process At1 t2 t3 t4
tim
e
time
![Page 43: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/43.jpg)
43
Avoidance using process-resource trajectories
Pro
cess
B
tW
tX
tY
tZ
Process At1 t2 t3 t4
tim
e
time
Both processeshold CD-RW
![Page 44: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/44.jpg)
44
Avoidance using process-resource trajectories
Pro
cess
B
tW
tX
tY
tZ
Process At1 t2 t3 t4
tim
e
time
Both processeshold Printer
![Page 45: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/45.jpg)
45
Avoidance using process-resource trajectories
Pro
cess
B
tW
tX
tY
tZ
Process At1 t2 t3 t4
tim
e
time
ForbiddenZone
![Page 46: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/46.jpg)
46
Avoidance using process-resource trajectories
Pro
cess
B
tW
tX
tY
tZ
Process At1 t2 t3 t4
tim
e
time
Trajectory showingsystem progress
![Page 47: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/47.jpg)
47
Avoidance using process-resource trajectories
Pro
cess
B
tW
tX
tY
tZ
Process At1 t2 t3 t4
tim
e
time
B makes progress,A is not running
![Page 48: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/48.jpg)
48
Avoidance using process-resource trajectories
Pro
cess
B
tW
tX
tY
tZ
Process At1 t2 t3 t4
tim
e
time
B requeststhe CD-RW
![Page 49: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/49.jpg)
49
Avoidance using process-resource trajectories
Pro
cess
B
tW
tX
tY
tZ
Process At1 t2 t3 t4
tim
e
time
Request is granted
![Page 50: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/50.jpg)
50
Avoidance using process-resource trajectories
Pro
cess
B
tW
tX
tY
tZ
Process At1 t2 t3 t4
tim
e
time
A runs & makesa request for printer
![Page 51: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/51.jpg)
51
Avoidance using process-resource trajectories
Pro
cess
B
tW
tX
tY
tZ
Process At1 t2 t3 t4
tim
e
time
Request is granted;A proceeds
![Page 52: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/52.jpg)
52
Avoidance using process-resource trajectories
Pro
cess
B
tW
tX
tY
tZ
Process At1 t2 t3 t4
tim
e
time
B runs & requeststhe printer...
MUST WAIT!
![Page 53: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/53.jpg)
53
Avoidance using process-resource trajectories
Pro
cess
B
tW
tX
tY
tZ
Process At1 t2 t3 t4
tim
e
time
A runs & requeststhe CD-RW
![Page 54: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/54.jpg)
54
Avoidance using process-resource trajectories
Pro
cess
B
tW
tX
tY
tZ
Process At1 t2 t3 t4
tim
e
time
A... holds printer requests CD-RWB... holds CD-RW requests printer
![Page 55: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/55.jpg)
55
Avoidance using process-resource trajectories
Pro
cess
B
tW
tX
tY
tZ
Process At1 t2 t3 t4
tim
e
time
A... holds printer requests CD-RWB... holds CD-RW requests printer
DEADLOCK!
![Page 56: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/56.jpg)
56
Avoidance using process-resource trajectories
Pro
cess
B
tW
tX
tY
tZ
Process At1 t2 t3 t4
tim
e
time
A danger occurred here.
Should the OS give A the printer, or make it wait???
![Page 57: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/57.jpg)
57
Avoidance using process-resource trajectories
Pro
cess
B
tW
tX
tY
tZ
Process At1 t2 t3 t4
tim
e
time
This area is “unsafe”
![Page 58: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/58.jpg)
58
Avoidance using process-resource trajectories
Pro
cess
B
tW
tX
tY
tZ
Process At1 t2 t3 t4
tim
e
time
Within the “unsafe” area, deadlock is inevitable.
We don’t want toenter this area.
The OS should makeA wait at this point!
![Page 59: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/59.jpg)
59
Avoidance using process-resource trajectories
Pro
cess
B
tW
tX
tY
tZ
Process At1 t2 t3 t4
tim
e
time
B requests the printer,B releases CD-RW,B releases printer,then A runs to completion!
![Page 60: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/60.jpg)
60
Safe states
The current state:“which processes hold which resources”
A “safe” state: No deadlock, and There is some scheduling order in which every
process can run to completion even if all of them request their maximum number of units immediately
The Banker’s Algorithm: Goal: Avoid unsafe states!!! When a process requests more units, should the
system grant the request or make it wait?
![Page 61: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/61.jpg)
61
Avoidance with multiple resources
Available resource vectorTotal resource vector
Maximum Request Vector
Row 2 is what process 2 might need
Note: These are the max. possiblerequests, which we assumeare known ahead of time!
![Page 62: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/62.jpg)
62
Banker’s algorithm for multiple resources
Look for a row, R, whose unmet resource needs are all smaller than or equal to A. If no such row exists, the system will eventually deadlock since no process can run to completion
Assume the process of the row chosen requests all the resources that it needs (which is guaranteed to be possible) and finishes. Mark that process as terminated and add all its resources to A vector
Repeat steps 1 and 2, until either all process are marked terminated, in which case the initial state was safe, or until deadlock occurs, in which case it was not
![Page 63: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/63.jpg)
63
Avoidance with multiple resources
Available resource vectorTotal resource vector
Maximum Request Vector
Row 2 is what process 2 might need
Run algorithm on every resource request!
![Page 64: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/64.jpg)
64
Avoidance with multiple resources
Max request matrix
![Page 65: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/65.jpg)
65
Avoidance with multiple resources
Max request matrix
![Page 66: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/66.jpg)
66
Avoidance with multiple resources
Max request matrix
![Page 67: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/67.jpg)
67
Avoidance with multiple resources
2 2 2 0
Max request matrix
![Page 68: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/68.jpg)
68
Avoidance with multiple resources
2 2 2 0
Max request matrix
![Page 69: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/69.jpg)
69
Avoidance with multiple resources
4 2 2 1 2 2 2 0
Max request matrix
![Page 70: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/70.jpg)
70
Problems with deadlock avoidance
Deadlock avoidance is often impossible because you don’t know in advance what
resources a process will need!
Alternative approach “deadlock prevention”
Make deadlock impossible! Attack one of the four conditions that are
necessary for deadlock to be possible
![Page 71: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/71.jpg)
71
Deadlock prevention
Conditions necessary for deadlock:
Mutual exclusion condition
Hold and wait condition
No preemption condition
Circular wait condition
![Page 72: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/72.jpg)
72
Deadlock prevention
Attacking mutual exclusion? a bad idea for some resource types
• resource could be corrupted works for some kinds of resources in certain
situations• eg., when a resource can be partitioned
Attacking no preemption? a bad idea for some resource types
• resource may be left in an inconsistent state may work in some situations
• checkpointing and rollback of idempotent operations
![Page 73: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/73.jpg)
73
Deadlock prevention
Attacking hold and wait? Require processes to request all resources
before they begin! Process must know ahead of time Process must tell system its “max potential
needs”• eg., like in the bankers algorithm• When problems occur a process must release all
its resources and start again
![Page 74: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/74.jpg)
74
Attacking the conditions
Attacking circular waiting? Number each of the resources Require each process to acquire lower
numbered resources before higher numbered resources
More precisely: “A process is not allowed to request a resource whose number is lower than the highest numbered resource it currently holds”
![Page 75: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/75.jpg)
75
Recall this example of deadlock
Assume that resources are ordered:1. Resource_12. Resource_23. ...etc...
acquire (resource_1)acquire (resource_2)use resources 1 & 2release (resource_2)release (resource_1)
acquire (resource_2)acquire (resource_1)use resources 1 & 2release (resource_1)release (resource_2)
Thread A: Thread B:
![Page 76: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/76.jpg)
76
Recall this example of deadlock
Assume that resources are ordered: 1. Resource_1 2. Resource_2 3. ...etc... Thread B violates the ordering!
acquire (resource_1)acquire (resource_2)use resources 1 & 2release (resource_2)release (resource_1)
acquire (resource_2)acquire (resource_1)use resources 1 & 2release (resource_1)release (resource_2)
Thread A: Thread B:
![Page 77: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/77.jpg)
77
Why Does Resource Ordering Work?
Assume deadlock has occurred.
Process A holds X requests Y
Process B holds Y requests Z
Process C holds Z requests X
![Page 78: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/78.jpg)
78
Why Does Resource Ordering Work?
Assume deadlock has occurred.
Process A holds X requests Y
Process B holds Y requests Z
Process C holds Z requests X
X < Y
![Page 79: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/79.jpg)
79
Why Does Resource Ordering Work?
Assume deadlock has occurred.
Process A holds X requests Y
Process B holds Y requests Z
Process C holds Z requests X
X < Y
Y< Z
![Page 80: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/80.jpg)
80
Why Does Resource Ordering Work?
Assume deadlock has occurred.
Process A holds X requests Y
Process B holds Y requests Z
Process C holds Z requests X
X < Y
Y< Z
Z < X
![Page 81: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/81.jpg)
81
Why Does Resource Ordering Work?
Assume deadlock has occurred.
Process A holds X requests Y
Process B holds Y requests Z
Process C holds Z requests X
X < Y
Y< Z
Z < X
This is impossible!
![Page 82: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/82.jpg)
82
Why Does Resource Ordering Work?
Assume deadlock has occurred.
Process A holds X requests Y
Process B holds Y requests Z
Process C holds Z requests X
X < Y
Y< Z
Z < X
This is impossible!
Therefore theassumption mustbe false!
![Page 83: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/83.jpg)
83
Resource Ordering
The chief problem: It may be hard to come up with an
acceptable ordering of resources!
Still,this is the most useful approach in an OS
1. ProcessControlBlock2. FileControlBlock3. Page Frames
Also, the problem of resources with multiple units is not addressed.
![Page 84: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/84.jpg)
84
A word on starvation
Starvation and deadlock are two different things
With deadlock – no work is being accomplished for the processes that are deadlocked, because processes are waiting for each other. Once present, it will not go away.
With starvation – work (progress) is getting done, however, a particular set of processes may not be getting any work done because they cannot obtain the resource they need
![Page 85: CS 333 Introduction to Operating Systems Class 7 - Deadlock](https://reader036.vdocuments.us/reader036/viewer/2022062519/568151bc550346895dbfeca5/html5/thumbnails/85.jpg)
85
Quiz
What is deadlock? What conditions must hold for deadlock
to be possible? What are the main approaches for
dealing with deadlock? Why does resource ordering help?