Download presentation
Presentation is loading. Please wait.
2
10 Deadlock
3
Example Process 1 Process 2 Resource 1 Resource 2 Process holds the resource Process requests the resource
4
Three Deadlocked Processes Process 1 Process 2 Process 3 Resource 1 Resource 2 Resource 3 Process holds the resource Process requests the resource
5
A Model P = {p 1, p 2, …, p n } be a set of processes R = {R 1, R 2, …, R m } be a set of resources c j = number of units of R j in the system S = {S 0, S 1, …} be a set of states representing the assignment of R j to p i –State changes when processes take action –This allows us to identify a deadlock situation in the operating system
6
State Transitions The system changes state because of the action of some process, p i There are three pertinent actions: –Request (“r i ”): request one or more units of a resource –Allocation (“a i ”): All outstanding requests from a process for a given resource are satisfied –Deallocation (“d i ”): The process releases units of a resource SjSj SkSk xixi
7
Properties of States Want to define deadlock in terms of patterns of transitions Define: p i is blocked in S j if p i cannot cause a transition out of S j SjSj r3r3 a1a1 r1r1 p 2 is blocked in S j
8
Properties of States (cont) If p i is blocked in S j, and will also be blocked in every S k reachable from S j, then p i is deadlocked S j is called a deadlock state
9
A Simple Process-Resource Model Instance P1P1 P1P1 P3P3 P3P3 P2P2 P2P2 R1R1 R1R1 R2R2 R2R2 R3R3 R3R3 Process holds resource Process requests resource
10
Example ra d S0S0 S1S1 S2S2 S3S3 S4S4 ra d One process, two units of one resource Can request one unit at a time
11
Extension of Example r0r0 a0a0 d0d0 S 00 S 10 S 20 S 30 S 40 r0r0 a0a0 d0d0 r0r0 a0a0 d0d0 S 01 S 11 S 21 S 31 S 41 r0r0 a0a0 d0d0 r0r0 a0a0 d0d0 S 02 S 12 S 22 S 32 r0r0 r0r0 a0a0 d0d0 S 03 S 13 S 23 S 33 r0r0 S 04 S 14 r0r0 r1r1 r1r1 r1r1 r1r1 r1r1 r1r1 r1r1 r1r1 a1a1 a1a1 a1a1 a1a1 a1a1 a1a1 d1d1 d1d1 d1d1 d1d1 d1d1 d1d1 r1r1
12
Figure 10 ‑ 10: A Model of a State with a Circular Wait RP RP P holds R P requests R RiRi PiPi
13
Dining Philosophers Revisited philosopher(int i){ while (TRUE) { // Think // Eat P(fork[i]); /* Pick up left fork */ P(fork[(i+1) mod 5]); /* Pick up right fork */ eat(); V(fork[(i+1) mod 5]); V(fork[i]); } philosopher4(){ while (TRUE) { // Think // Eat P(fork[0]); /* Pick up right fork */ P(fork[4]); /* Pick up left fork */ eat(); V(fork[4]); V(fork[0]); } semaphore fork[5]; fork[0] = fork[1] = fork[2] = fork[3] = fork[4] = 1; fork(philosopher, 1, 0); fork(philosopher, 1, 1); fork(philosopher, 1, 2); fork(philosopher, 1, 3); fork(philosopher4, 0);
14
Addressing Deadlock Prevention: Design the system so that deadlock is impossible Avoidance: Construct a model of system states, then choose a strategy that will not allow the system to go to a deadlock state Detection & Recovery: Check for deadlock (periodically or sporadically), then recover Manual intervention: Have the operator reboot the machine if it seems too slow
15
Prevention Necessary conditions for deadlock –Mutual exclusion –Hold and wait –Circular waiting –No preemption Ensure that at least one of the necessary conditions is false at all times –Mutual exclusion must hold at all times
16
Hold and Wait Need to be sure a process does not hold one resource while requesting another Approach 1: Force a process to request all resources it needs at one time Approach 2: If a process needs to acquire a new resource, it must first release all resources it holds, then reacquire all it needs What does this say about state transition diagrams?
17
Circular Wait Have a situation in which there are K processes holding units of K resources RP RP P holds R P requests R RiRi PiPi
18
Circular Wait (cont) There is a cycle in the graph of processes and resources Choose a resource request strategy by which no cycle will be introduced Total order on all resources, then can only ask for R j if R i < R j for all R i the process is currently holding
19
Circular Wait (cont) There is a cycle in the graph of processes and resources Choose a resource request strategy by which no cycle will be introduced Total order on all resources, then can only ask for R j if R i < R j for all R i the process is currently holding This is how we noticed the easy solution for the dining philosophers
20
Allowing Preemption Allow a process to time-out on a blocked request -- withdrawing the request if it fails SiSi ruru dvdv ruru SjSj SkSk wuwu
21
Avoidance Define a model of system states, then choose a strategy that will guarantee that the system will not go to a deadlock state Requires extra information, e.g., the maximum claim for each process Allows resource manager to see the worst case that could happen, then to allow transitions based on that knowledge
22
Safe vs Unsafe States Safe state: one in which the system can assure that any sequence of subsequent transitions leads back to the initial state –Even if all exercise their maximum claim, there is an allocation strategy by which all claims can be met Unsafe state: one in which the system cannot guarantee that the system will transition back to the initial state –Unsafe state can lead to a deadlock state if too many processes exercise their maximum claim at once
23
More on Safe & Unsafe States Normal Execution Request Max Claim Execute, then release No Yes Likely to be in a safe state Probability of being in unsafe state increases
24
More on Safe & Unsafe States I Safe States Unsafe States Deadlock States Disallow
25
Banker’s Algorithm Let maxc[i, j] be the maximum claim for R j by p i Let alloc[i, j] be the number of units of R j held by p i Can always compute –avail[j] = c j - 0 i n alloc[i,j] –Then number of available units of R j Should be able to determine if the state is safe or not using this info
26
Banker’s Algorithm Copy the alloc[i,j] table to alloc’[i,j] Given C, maxc and alloc’, compute avail vector Find p i : maxc[i,j] - alloc’[i,j] avail[j] for 0 j < m and 0 i < n. –If no such p i exists, the state is unsafe –If alloc’[i,j] is 0 for all i and j, the state is safe Set alloc’[i,j] to 0; deallocate all resources held by p i ; go to Step 2
27
Example ProcessR 0 R 1 R 2 R 3 p 0 3214 p 1 0252 p 2 5105 p 3 1530 p 4 3033 Maximum Claim ProcessR 0 R 1 R 2 R 3 p 0 2011 p 1 0121 p 2 0000 p 3 0210 p 4 1030 Sum3372 Allocated Resources C = Compute total allocated Determine available units avail = = Can anyone’s maxc be met? maxc[4,0]-alloc’[4,0] = 5-1 = 4 5 = avail[0] maxc[4,1]-alloc’[4,1] = 0-0 = 0 2 = avail[1] maxc[4,2]-alloc’[4,2] = 3-3 = 0 2 = avail[2] maxc[4,3]-alloc’[4,3] = 3-0 = 3 5 = avail[3] P 4 can exercise max claim avail[0] = avail[0]+alloc’[4,0] = 5+1 = 6 avail[1] = avail[1]+alloc’[4,1] = 2+0 = 2 avail[2] = avail[2]+alloc’[4,2] = 2+3 = 5 avail[3] = avail[3]+alloc’[4,3] = 5+0 = 5
28
Example ProcessR 0 R 1 R 2 R 3 p 0 3214 p 1 0252 p 2 5105 p 3 1530 p 4 3033 Maximum Claim ProcessR 0 R 1 R 2 R 3 p 0 2011 p 1 0121 p 2 0000 p 3 0210 p 4 0000 Sum2142 Allocated Resources C = Compute total allocated Determine available units avail = = Can anyone’s maxc be met? (Yes, any of them can)
29
Detection & Recovery Check for deadlock (periodically or sporadically), then recover Can be far more aggressive with allocation No maximum claim, no safe/unsafe states Differentiate between –Serially reusable resources: A unit must be allocated before being released –Consumable resources: Never release acquired resources; resource count is number currently available
30
Recovery No magic here –Choose a blocked resource –Preempt it (releasing its resources) –Run the detection algorithm –Iterate if until the state is not a deadlock state
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.