CS333 Intro to Operating Systems Jonathan Walpole
description
Transcript of CS333 Intro to Operating Systems Jonathan Walpole
![Page 1: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/1.jpg)
CS333 Intro to Operating SystemsJonathan Walpole
![Page 2: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/2.jpg)
2
Deadlock
![Page 3: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/3.jpg)
3
Resources & DeadlocksProcesses 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 Bat the same time another process holds B and requests Aboth are blocked and remain so … this is deadlock
![Page 4: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/4.jpg)
4
Resource Usage ModelSequence of events required to use a resource
- request the resource (eg. acquire mutex)- use the resource-release the resource (eg. release mutex)
Must wait if request is denied- block- busy wait- fail with error code
![Page 5: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/5.jpg)
5
Preemptable ResourcesPreemptable resources
- Can be taken away 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 6: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/6.jpg)
6
Definition of DeadlockA 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 its resources
![Page 7: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/7.jpg)
7
Deadlock ConditionsA 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 8: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/8.jpg)
8
Examples of Deadlock
![Page 9: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/9.jpg)
9
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 10: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/10.jpg)
10
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 11: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/11.jpg)
11
Resource Acquisition Scenarios
acquire (resource_2)use resource_2release (resource_2)
Thread A: Thread B:
acquire (resource_1)use resource_1release (resource_1)
![Page 12: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/12.jpg)
12
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 13: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/13.jpg)
13
Resource Acquisition Scenarios
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 14: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/14.jpg)
14
Resource Acquisition Scenarios
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 15: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/15.jpg)
15
Resource Acquisition Scenarios
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 16: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/16.jpg)
16
Resource Acquisition Scenarios
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 17: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/17.jpg)
17
Resource Acquisition Scenarios
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 18: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/18.jpg)
18
Resource Acquisition Scenarios
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 19: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/19.jpg)
19
Dealing With Deadlock
1. Ignore the problem2. Detect it and recover from it3. Dynamically avoid is via careful resource allocation4. Prevent it by attacking one of the four necessary
conditions
![Page 20: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/20.jpg)
20
Deadlock DetectionLet the problem happen, then recover
- How do you know it happened?Do a depth-first-search on a resource allocation graph
![Page 21: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/21.jpg)
21
Resource Allocation Graphs
Resource
R
AProcess/Thread
![Page 22: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/22.jpg)
22
Resource Allocation Graphs
Resource
R
AProcess/Thread
“is held by”
![Page 23: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/23.jpg)
23
Resource Allocation Graphs
R
A
“is requesting”
S
Resource
Process/ThreadResource
![Page 24: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/24.jpg)
24
Resource Allocation Graphs
R
A S
B
![Page 25: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/25.jpg)
25
Resource Allocation Graphs
Deadlock
R
A S
B
![Page 26: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/26.jpg)
26
Resource Allocation Graphs
Deadlock = a cycle in the graph
R
A S
B
![Page 27: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/27.jpg)
27
Deadlock DetectionDo a depth-first-search on the resource allocation graph
![Page 28: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/28.jpg)
28
Deadlock DetectionDo a depth-first-search on the resource allocation graph
![Page 29: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/29.jpg)
29
Deadlock DetectionDo a depth-first-search on the resource allocation graph
![Page 30: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/30.jpg)
30
Deadlock DetectionDo a depth-first-search on the resource allocation graph
![Page 31: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/31.jpg)
31
Deadlock DetectionDo a depth-first-search on the resource allocation graph
Deadlock!
![Page 32: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/32.jpg)
32
Multiple Instances of a ResourceSome resources have only one instance
- i.e. a lock or a printer- Only one thread at a time may hold the resource
Some resources have several instances-i.e. Page frames in memory-All units are considered equal; any one will do
![Page 33: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/33.jpg)
33
Multiple Instances of a ResourceTheorem: 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: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/34.jpg)
34
Multiple Instances of a Resource
![Page 35: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/35.jpg)
35
Deadlock Detection IssuesHow 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 36: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/36.jpg)
36
Recovery From DeadlockIf 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 37: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/37.jpg)
37
Deadlock RecoveryHow do we prevent resource corruption
- For example, shared variables protected by a lock?
Recovery through preemption and rollback-Save state periodically (ie. at start of critical section)-Take a checkpoint of memory-Start computation again from checkpoint-Can also make long-lived computation systems resilient
![Page 38: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/38.jpg)
38
Deadlock AvoidanceDetection – 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 39: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/39.jpg)
39
time
Process At1 t2 t3 t4
Deadlock Avoidance
![Page 40: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/40.jpg)
40
time
Process At1 t2 t3 t4
Requests Printer
Requests CD-RW
Releases Printer
Releases CD-RW
Deadlock Avoidance
![Page 41: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/41.jpg)
41
time
Proc
ess B
tW
tX
tY
tZ
Deadlock Avoidance
![Page 42: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/42.jpg)
42
time
Proc
ess B
tW
tX
tY
tZ Requests CD-RW
Requests PrinterReleases CD-RW
Releases Printer
Deadlock Avoidance
![Page 43: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/43.jpg)
43
Proc
ess B
tW
tX
tY
tZ
Process At1 t2 t3 t4
time
time
Deadlock Avoidance
![Page 44: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/44.jpg)
44
Proc
ess B
tW
tX
tY
tZ
Process At1 t2 t3 t4
time
time
Both processeshold CD-RW
Deadlock Avoidance
![Page 45: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/45.jpg)
45
Proc
ess B
tW
tX
tY
tZ
Process At1 t2 t3 t4
time
time
Both processeshold Printer
Deadlock Avoidance
![Page 46: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/46.jpg)
46
Proc
ess B
tW
tX
tY
tZ
Process At1 t2 t3 t4
time
time
ForbiddenZone
Deadlock Avoidance
![Page 47: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/47.jpg)
47
Proc
ess B
tW
tX
tY
tZ
Process At1 t2 t3 t4
time
time
Trajectory showingsystem progress
Deadlock Avoidance
![Page 48: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/48.jpg)
48
Proc
ess B
tW
tX
tY
tZ
Process At1 t2 t3 t4
time
time
B makes progress,A is not running
Deadlock Avoidance
![Page 49: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/49.jpg)
49
Proc
ess B
tW
tX
tY
tZ
Process At1 t2 t3 t4
time
time
B requeststhe CD-RW
Deadlock Avoidance
![Page 50: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/50.jpg)
50
Proc
ess B
tW
tX
tY
tZ
Process At1 t2 t3 t4
time
time
Request is granted
Deadlock Avoidance
![Page 51: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/51.jpg)
51
Proc
ess B
tW
tX
tY
tZ
Process At1 t2 t3 t4
time
time
A runs & makesa request for printer
Deadlock Avoidance
![Page 52: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/52.jpg)
52
Proc
ess B
tW
tX
tY
tZ
Process At1 t2 t3 t4
time
time
Request is granted;A proceeds
Deadlock Avoidance
![Page 53: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/53.jpg)
53
Proc
ess B
tW
tX
tY
tZ
Process At1 t2 t3 t4
time
time
B runs & requeststhe printer...
MUST WAIT!
Deadlock Avoidance
![Page 54: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/54.jpg)
54
Proc
ess B
tW
tX
tY
tZ
Process At1 t2 t3 t4
time
time
A runs & requeststhe CD-RW
Deadlock Avoidance
![Page 55: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/55.jpg)
55
Proc
ess B
tW
tX
tY
tZ
Process At1 t2 t3 t4
time
time
A... holds printer requests CD-RWB... holds CD-RW requests printer
Deadlock Avoidance
![Page 56: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/56.jpg)
56
Proc
ess B
tW
tX
tY
tZ
Process At1 t2 t3 t4
time
time
A... holds printer requests CD-RWB... holds CD-RW requests printer
DEADLOCK!
Deadlock Avoidance
![Page 57: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/57.jpg)
57
Proc
ess B
tW
tX
tY
tZ
Process At1 t2 t3 t4
time
time
A danger occurred here.
Should the OS give A the printer, or make it wait???
Deadlock Avoidance
![Page 58: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/58.jpg)
58
Proc
ess B
tW
tX
tY
tZ
Process At1 t2 t3 t4
time
time
This area is “unsafe”
Deadlock Avoidance
![Page 59: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/59.jpg)
59
Proc
ess B
tW
tX
tY
tZ
Process At1 t2 t3 t4
time
time
Within the “unsafe” area, deadlock is inevitable.
We don’t want toenter this area.
The OS should makeA wait at this point!
Deadlock Avoidance
![Page 60: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/60.jpg)
60
Proc
ess B
tW
tX
tY
tZ
Process At1 t2 t3 t4
time
timeB requests the printer,B releases CD-RW,B releases printer,then A runs to completion!
Deadlock Avoidance
![Page 61: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/61.jpg)
61
Safe StatesThe current state: which processes hold which resourcesA “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 62: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/62.jpg)
62
Avoidance - 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 63: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/63.jpg)
63
Banker’s AlgorithmLook 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 64: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/64.jpg)
64
Avoidance - 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 65: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/65.jpg)
65
Avoidance - Multiple Resources
Max request matrix
![Page 66: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/66.jpg)
66
Avoidance - Multiple Resources
Max request matrix
![Page 67: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/67.jpg)
67
Avoidance - Multiple Resources
Max request matrix
![Page 68: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/68.jpg)
68
Avoidance - Multiple Resources
2 2 2 0
Max request matrix
![Page 69: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/69.jpg)
69
Avoidance - Multiple Resources
2 2 2 0
Max request matrix
![Page 70: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/70.jpg)
70
Avoidance - Multiple Resources
4 2 2 1 2 2 2 0
Max request matrix
![Page 71: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/71.jpg)
71
Deadlock Avoidance ProblemsDeadlock avoidance may be 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 72: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/72.jpg)
72
Deadlock Prevention
Conditions necessary for deadlock
Mutual exclusion conditionHold and wait conditionNo preemption conditionCircular wait condition
![Page 73: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/73.jpg)
73
Deadlock Prevention
Attacking mutual exclusion?- Some resource types resource could be corrupted- May be OK if a resource can be partitioned
Attacking no preemption?- Some resources could be left in an inconsistent state- May work with support of checkpointing and rollback of
idempotent operations
![Page 74: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/74.jpg)
74
Deadlock Prevention
Attacking hold and wait?- Require processes to request all resources before they
start- Processes may not know what they need ahead of time- When problems occur a process must release all its
resources and start again
![Page 75: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/75.jpg)
75
Deadlock Prevention
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 76: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/76.jpg)
76
Example
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 77: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/77.jpg)
Example
Assume that resources are ordered:1. Resource_12. Resource_23. ...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 78: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/78.jpg)
78
Resource OrderingAssume deadlock has occurred.Process A
- holds X- requests Y
Process B- holds Y- requests Z
Process C- holds Z- requests X
![Page 79: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/79.jpg)
79
Resource OrderingAssume deadlock has occurred.Process A
- holds X- requests Y
Process B- holds Y- requests Z
Process C- holds Z- requests X
X < Y
![Page 80: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/80.jpg)
80
Resource OrderingAssume 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 81: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/81.jpg)
81
Resource OrderingAssume 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 82: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/82.jpg)
82
Resource OrderingAssume 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 83: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/83.jpg)
83
Resource OrderingThe chief problem:
It may be hard to come up with an acceptable ordering of resources!
![Page 84: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/html5/thumbnails/84.jpg)
84
StarvationStarvation and deadlock are different
- 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: CS333 Intro to Operating Systems Jonathan Walpole](https://reader031.fdocuments.in/reader031/viewer/2022020209/56813d3e550346895da6fc17/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?