![Page 1: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/1.jpg)
Deadlocks
Dec 11, 2007
CPE 261403 - Operating Systemshttp://groups.google.com/group/[email protected]
![Page 2: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/2.jpg)
![Page 3: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/3.jpg)
Conditions of a Deadlock (7.2.1)
Mutual exclusion
Hold and wait
No preemption
Circular wait
![Page 4: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/4.jpg)
![Page 5: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/5.jpg)
Methods for Handling Deadlocks Never allow a deadlock state
Allow deadlock and then recover
Ignore the problem
![Page 6: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/6.jpg)
Deadlock Prevention (7.4)Prevent deadlock conditions from occurring Mutual exclusion
Hold and wait
No preemption
Circular wait
![Page 7: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/7.jpg)
Deadlock Avoidance (7.5)
Figure 7.5
![Page 8: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/8.jpg)
Example
Max Needs Needs at T0(safe)
Needs at T1(unsafe)
P0 4 2 2
P1 10 5 5
P2 9 2 3
Total number of resources = 12
![Page 9: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/9.jpg)
Resource Allocation Graph
![Page 10: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/10.jpg)
Banker’s Algorithm
Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj available.
Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances of resource type Rj.
Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj.
Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task.
Need [i,j] = Max[i,j] – Allocation [i,j].
Let n = number of processes, and m = number of resources types.
![Page 11: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/11.jpg)
Example
5 processes P0 through P4; 3 resource types: A (10 instances), B (5instances), and C (7 instances). Snapshot at time T0:
Allocation Max AvailableA 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
![Page 12: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/12.jpg)
Safety Algorithm
Work
Need Finish
P0
P1
P2
P3
P4
R1 R2 R3
![Page 13: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/13.jpg)
Safety Algorithm
7 4 3
1 2 2
6 0 0
0 1 1
4 3 1
3 3 2Work
Need Finish
P0
P1
P2
P3
P4
R1 R2 R3
![Page 14: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/14.jpg)
Example (Cont.) The content of the matrix Need is defined to be Max – Allocation.
NeedA B C
P0 7 4 3 P1 1 2 2 P2 6 0 0 P3 0 1 1 P4 4 3 1
The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria.
![Page 15: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/15.jpg)
Process P1 makes a request for: 1 instance of A 2 instance of C
Request Vector = (1,0,2)
![Page 16: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/16.jpg)
Safety Algorithm after (1,0,2) request
7 4 3
1 2 2
6 0 0
0 1 1
4 3 1
3 3 2Work
Need Finish
P0
P1
P2
P3
P4
R1 R2 R3
![Page 17: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/17.jpg)
Example: P1 Request (1,0,2) Check that Request Available (that is, (1,0,2) (3,3,2) true.
Allocation Need AvailableA B C A B C A B C P0 0 1 0 7 4 3 2 3 0P1 3 0 2 0 2 0 P2 3 0 1 6 0 0 P3 2 1 1 0 1 1P4 0 0 2 4 3 1
Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies safety requirement.
Can request for (3,3,0) by P4 be granted? Can request for (0,2,0) by P0 be granted?
![Page 18: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/18.jpg)
Deadlock Detection (7.6)
![Page 19: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/19.jpg)
Wait-for Graphs (single instance system only)
Resource-Allocation Graph Corresponding wait-for graph
![Page 20: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/20.jpg)
Wait-for Graph does not work with multiple instance resources
![Page 21: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/21.jpg)
Banker’s Algorithm as a deadlock detection tool for multiple instance systems
![Page 22: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/22.jpg)
Deadlock Recovery (7.7)
![Page 23: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/23.jpg)
Process Termination
Abort all deadlock processes
Abort one at a time until deadlock cycle is eliminated
![Page 24: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/24.jpg)
Resource Preemption
Selecting a victim – minimize cost.
Rollback – return to some safe state, restart process for that state.
Starvation – same process may always be picked as victim, include number of rollback in cost factor.
![Page 25: Deadlocks Dec 11, 2007 CPE 261403 - Operating Systems](https://reader035.vdocuments.net/reader035/viewer/2022062306/5a4d1b107f8b9ab05998e706/html5/thumbnails/25.jpg)
หวัขอ้ present ท่ีเหลือ
Memory Management1. Paging - บท 8.42. Demand Paging - บท 9.2
Storage Management3. File Protection - บท 10.64. RAID structure - บท 12.75. Linux File system - บท 21.7
System security6. Program Threats - บท 18.2 (ยกเวน้ 18.2.5 Viruses)7. Computer Viruses - บท 18.2.58. System & Network threats - บท 18.39. Cryptography - บท 18.4