Deadlock
Disclaimer: some slides are adopted from Dr. Kulkarni’s and book authors’ slides with permission
1
Deadlock Disclaimer: some slides are adopted from Dr. Kulkarnis and - - PowerPoint PPT Presentation
Deadlock Disclaimer: some slides are adopted from Dr. Kulkarnis and book authors slides with permission 1 Recap: Synchronization Race condition A situation when two or more threads read and write shared data at the same time
Disclaimer: some slides are adopted from Dr. Kulkarni’s and book authors’ slides with permission
1
2
3
4
5
Critical section lock() Information bus (High) Communication (Medium) Weather (Low) lock() More reading: What really happened on Mars?
6
P0 P1 A B Owned by Wait for Owned by Wait for
7
bridge A B
8
– only one process at a time can use a resource
– resources cannot be preempted, release must be voluntary
– a process must be holding at least one resource, and waiting to acquire additional resources held by other processes
– There must be a circular dependency. For example, A waits B, B waits C, and C waits A.
9
10
11
Pi Pi
Rj Rj
12
Simple example Deadlock example With cycle, but no deadlock
request edge – directed edge Pi Rj
assignment edge – directed edge Rj Pi
13
Recall four deadlock conditions: (1) Mutual exclusion, (2) no preemption, (3) hold and wait, (4) circular wait
14
15
Resource-Allocation Graph Corresponding wait-for graph
16
– indicates the number of available resources of each type – [R1, R2] = [0,0]
– defines the number of resources of each type currently allocated to each process – Alloc[1] = [0,1], – Alloc[2] = [1, 0], …
– indicates the resources each process requests – Request[1] = [1,0], – Request[2] = [0,0], …
17
Avail = FreeResources; For i = 1,2, …, n, Finish[i] = false
Finish[i] == false AND Request[i] Avail If no such i exists, go to step 4
Go to step 2
(a) then the system is in deadlock state
FreeResources: resource vector [R1, R2] = [0,0]
Alloc[i]: process i’s allocated resource vector: Alloc[1] = [0,1], Alloc[2] = [1, 0]
Request[i]: process i’s requesting vector: Request[1] = [1,0] Request[2] = [0,0]
18
– Preempt the resources – Bridge example: throw the car to the river – Kill the deadlocked threads and return the resources
– Return to a known safe state – Bridge example: move one car backward – Dining philosopher: make one philosopher give up a chopstick
19
20
21
22
23
24
25
Avail = FreeResources; For i = 1,2, …, n, Finish[i] = false
Finish[i] == false AND Max[i] – Alloc[i] Avail If no such i exists, go to step 4
Go to step 2
(a) then the system is in deadlock state (b) if Finish[i] == false, then Pi is deadlocked
FreeResources: resource vector [R1, R2] = [0,0]
Alloc[i]: process i’s allocated resource vector: Alloc[1] = [0,1], Alloc[2] = [1, 0]
Max[i]: process i’s maximum resource demand vector
Max[i] - Alloc[i] : future need
26
27
3 resource types: A (10 instances), B (5instances), and C (7 instances)
Allocation Max Available A B C A B C A B C P0 0 1 0 7 5 3 3 3 2 P1 2 0 0 3 2 2 P2 3 0 2 9 0 2 P3 2 1 1 2 2 2 P4 0 0 2 4 3 3
Need A B C P0 7 4 3 P1 1 2 2 P2 6 0 0 P3 0 1 1 P4 4 3 1
safety criteria
29
Allocation Need Available A B C A B C A B C P0 0 1 0 7 4 3 2 3 0 P1 3 0 2 0 2 0 P2 3 0 2 6 0 0 P3 2 1 1 0 1 1 P4 0 0 2 4 3 1
satisfies safety requirement
31