Download presentation
Presentation is loading. Please wait.
1
Lecture 28 Syed Mansoor Sarwar
Operating Systems Lecture 28 Syed Mansoor Sarwar
2
© Copyright Virtual University of Pakistan
Agenda for Today Review of previous lecture Deadlock avoidance Banker’s algorithms Safety algorithm Safe Sequence Recap of lecture 12 January 2019 © Copyright Virtual University of Pakistan
3
© Copyright Virtual University of Pakistan
Review of Lecture 27 Deadlock handling Deadlock prevention Deadlock avoidance 12 January 2019 © Copyright Virtual University of Pakistan
4
© Copyright Virtual University of Pakistan
RAG Algorithm Claim edge Pi Rj indicates that process Pi may request an instance of resource Rj; represented by a dashed line. Claim edge converts to request edge when a process requests a resource. 12 January 2019 © Copyright Virtual University of Pakistan
5
© Copyright Virtual University of Pakistan
RAG Algorithm When a resource is assigned to a process, request edge reconverts to an assignment edge. When a resource is released by a process, assignment edge reconverts to a claim edge. Resources must be claimed a priori in the system. 12 January 2019 © Copyright Virtual University of Pakistan
6
RAG For Deadlock Avoidance
12 January 2019 © Copyright Virtual University of Pakistan
7
© Copyright Virtual University of Pakistan
Unsafe State In RAG 12 January 2019 © Copyright Virtual University of Pakistan
8
© Copyright Virtual University of Pakistan
Banker’s Algorithm Multiple instances of resources. Each process must a priori claim maximum use. When a process requests a resource it may have to wait. When a process gets all its resources it must return them in a finite amount of time. 12 January 2019 © Copyright Virtual University of Pakistan
9
Data Structures for the Banker’s Algorithm
n = number of processes m = number of resource types Available: Vector of length m. If available[j] = k, there are k instances of resource type Rj available. 12 January 2019 © Copyright Virtual University of Pakistan
10
Data Structures for the Banker’s Algorithm
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. 12 January 2019 © Copyright Virtual University of Pakistan
11
Data Structures for the Banker’s Algorithm
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] 12 January 2019 © Copyright Virtual University of Pakistan
12
© Copyright Virtual University of Pakistan
Safety Algorithm Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish[i] = false; i = 1, 2, …, n. 12 January 2019 © Copyright Virtual University of Pakistan
13
© Copyright Virtual University of Pakistan
Safety Algorithm Find and i such that both: (a) Finish[i] = false (b) Needi Work If no such i exists, go to step 4. 12 January 2019 © Copyright Virtual University of Pakistan
14
© Copyright Virtual University of Pakistan
Safety Algorithm Work = Work + Allocationi Finish[i] = true go to step 2. If Finish [i] == true for all i, then the system is in a safe state. 12 January 2019 © Copyright Virtual University of Pakistan
15
Resource-Request Algorithm for Pi
Requesti = request vector for Pi. If Requesti [j] = k then process Pi wants k instances of resource type Rj. If Requesti Needi go to step 2. Otherwise, report error since process has exceeded its maximum claim. 12 January 2019 © Copyright Virtual University of Pakistan
16
Resource-Request Algorithm for Pi
If Requesti Available, go to step 3. Otherwise Pi must wait, since resources are not available. Pretend to allocate requested resources to Pi by modifying the state as follows: 12 January 2019 © Copyright Virtual University of Pakistan
17
Resource-Request Algorithm for Pi
Available = Available – Requestj; Allocationi = Allocationi + Requesti; Needi = Needi – Requesti; If safe the resources are allocated to Pi. If unsafe Pi must wait, and the old resource-allocation state is restored 12 January 2019 © Copyright Virtual University of Pakistan
18
© Copyright Virtual University of Pakistan
Example System Five processes: P0 … P4 Three resource types: A (10 instances) B (5 instances) C (7 instances) 12 January 2019 © Copyright Virtual University of Pakistan
19
© Copyright Virtual University of Pakistan
Example System System state: Is it safe? Process P0 P1 P2 P3 P4 Max A B C 7 5 3 3 2 2 9 0 2 2 2 2 4 3 3 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Available A B C 3 2 12 January 2019 © Copyright Virtual University of Pakistan
20
© Copyright Virtual University of Pakistan
Example System Needi = Maxi – Allocationi Need matrix Process A B C P0 7 4 3 P1 1 2 P2 6 P3 P4 12 January 2019 © Copyright Virtual University of Pakistan
21
© Copyright Virtual University of Pakistan
Example System Process P0 P1 P2 P3 P4 Need A B C 7 4 3 1 2 2 6 0 0 0 1 1 4 3 1 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Work A B C 3 2 Safe Sequence: <> 12 January 2019 © Copyright Virtual University of Pakistan
22
© Copyright Virtual University of Pakistan
Example System Process P0 P1 P2 P3 P4 Need A B C 7 4 3 1 2 2 6 0 0 0 1 1 4 3 1 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Work A B C 3 2 5 Safe Sequence: < P1> 12 January 2019 © Copyright Virtual University of Pakistan
23
© Copyright Virtual University of Pakistan
Example System Process P0 P1 P2 P3 P4 Need A B C 7 4 3 1 2 2 6 0 0 0 1 1 4 3 1 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Work A B C 3 2 5 7 4 Safe Sequence: < P1, P3> 12 January 2019 © Copyright Virtual University of Pakistan
24
© Copyright Virtual University of Pakistan
Example System Process P0 P1 P2 P3 P4 Need A B C 7 4 3 1 2 2 6 0 0 0 1 1 4 3 1 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Work A B C 3 2 5 7 4 Safe Sequence: < P1, P3, P4 > 12 January 2019 © Copyright Virtual University of Pakistan
25
© Copyright Virtual University of Pakistan
Example System Process P0 P1 P2 P3 P4 Need A B C 7 4 3 1 2 2 6 0 0 0 1 1 4 3 1 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Work A B C 3 2 5 7 4 Safe Sequence: <P1, P3, P4, P0> 12 January 2019 © Copyright Virtual University of Pakistan
26
© Copyright Virtual University of Pakistan
Example System Final safe sequence: <P1, P3, P4, P0, P2> Not a unique sequence Possible safe sequences for the this example: <P1,P3,P4,P0,P2>, <P1,P3,P4,P2,P0>, <P1,P3,P2,P0,P4>, <P1,P3,P2,P4,P0>, <P1,P3,P0,P2,P4>, <P1,P3,P0,P4,P2> 12 January 2019 © Copyright Virtual University of Pakistan
27
© Copyright Virtual University of Pakistan
Example P1 requests (1,0,2). Can this request be granted immediately? Is Request1 Need1? (1,0,2) (1,2,2) true Is Request1 Available? (1,0,2) (3,3,2) true 12 January 2019 © Copyright Virtual University of Pakistan
28
© Copyright Virtual University of Pakistan
Example Process P0 P1 P2 P3 P4 Need A B C 7 4 3 1 2 2 6 0 0 0 1 1 4 3 1 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Available A B C 3 2 12 January 2019 © Copyright Virtual University of Pakistan
29
© Copyright Virtual University of Pakistan
Example Process P0 P1 P2 P3 P4 Need A B C 7 4 3 0 2 0 6 0 0 0 1 1 4 3 1 Allocation A B C 0 1 0 3 0 2 2 1 1 0 0 2 Work A B C 2 3 Safe Sequence: <> 12 January 2019 © Copyright Virtual University of Pakistan
30
© Copyright Virtual University of Pakistan
Example Process P0 P1 P2 P3 P4 Need A B C 7 4 3 0 2 0 6 0 0 0 1 1 4 3 1 Allocation A B C 0 1 0 3 0 2 2 1 1 0 0 2 Work A B C 2 3 5 Safe Sequence: < P1 > 12 January 2019 © Copyright Virtual University of Pakistan
31
© Copyright Virtual University of Pakistan
Example Process P0 P1 P2 P3 P4 Need A B C 7 4 3 0 2 0 6 0 0 0 1 1 4 3 1 Allocation A B C 0 1 0 3 0 2 2 1 1 0 0 2 Work A B C 2 3 5 7 4 Safe Sequence: < P1, P3 > 12 January 2019 © Copyright Virtual University of Pakistan
32
© Copyright Virtual University of Pakistan
Example Process P0 P1 P2 P3 P4 Need A B C 7 4 3 0 2 0 6 0 0 0 1 1 4 3 1 Allocation A B C 0 1 0 3 0 2 2 1 1 0 0 2 Work A B C 2 3 5 7 4 Safe Sequence: < P1, P3, P4 > 12 January 2019 © Copyright Virtual University of Pakistan
33
© Copyright Virtual University of Pakistan
Example Process P0 P1 P2 P3 P4 Need A B C 7 4 3 0 2 0 6 0 0 0 1 1 4 3 1 Allocation A B C 0 1 0 3 0 2 2 1 1 0 0 2 Work A B C 2 3 5 7 4 Safe Sequence: <P1, P3, P4, P0> 12 January 2019 © Copyright Virtual University of Pakistan
34
© Copyright Virtual University of Pakistan
Example Executing safety algorithm shows that sequence <P1,P3,P4,P0,P2> satisfies safety requirement. Yes, P1’s request may be granted immediately. 12 January 2019 © Copyright Virtual University of Pakistan
35
© Copyright Virtual University of Pakistan
More Examples Can request for (0,2,0) by P0 be granted? Is Request0 Need0? (0,2,0) (7,4,3) true Is Request1 Available? (0,2,0) (3,3,2) true 12 January 2019 © Copyright Virtual University of Pakistan
36
© Copyright Virtual University of Pakistan
Example Process P0 P1 P2 P3 P4 Need A B C 7 4 3 1 2 2 6 0 0 0 1 1 4 3 1 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Available A B C 3 2 12 January 2019 © Copyright Virtual University of Pakistan
37
© Copyright Virtual University of Pakistan
Example Process P0 P1 P2 P3 P4 Need A B C 7 2 3 1 2 2 6 0 0 0 1 1 4 3 1 Allocation A B C 0 3 0 2 0 0 3 0 2 2 1 1 0 0 2 Work A B C 3 1 2 Safe Sequence: <> 12 January 2019 © Copyright Virtual University of Pakistan
38
© Copyright Virtual University of Pakistan
Example Process P0 P1 P2 P3 P4 Need A B C 7 2 3 1 2 2 6 0 0 0 1 1 4 3 1 Allocation A B C 0 3 0 2 0 0 3 0 2 2 1 1 0 0 2 Work A B C 3 1 2 5 Safe Sequence: < P3 > 12 January 2019 © Copyright Virtual University of Pakistan
39
© Copyright Virtual University of Pakistan
Example Process P0 P1 P2 P3 P4 Need A B C 7 2 3 1 2 2 6 0 0 0 1 1 4 3 1 Allocation A B C 0 3 0 2 0 0 3 0 2 2 1 1 0 0 2 Work A B C 3 1 2 5 7 Safe Sequence: < P3,P1 > 12 January 2019 © Copyright Virtual University of Pakistan
40
© Copyright Virtual University of Pakistan
Example Process P0 P1 P2 P3 P4 Need A B C 7 2 3 1 2 2 6 0 0 0 1 1 4 3 1 Allocation A B C 0 3 0 2 0 0 3 0 2 2 1 1 0 0 2 Work A B C 3 1 2 5 7 10 Safe Sequence: < P3,P1,P2 > 12 January 2019 © Copyright Virtual University of Pakistan
41
© Copyright Virtual University of Pakistan
Example Process P0 P1 P2 P3 P4 Need A B C 7 2 3 1 2 2 6 0 0 0 1 1 4 3 1 Allocation A B C 0 3 0 2 0 0 3 0 2 2 1 1 0 0 2 Work A B C 3 1 2 5 7 10 Safe Sequence: <P3,P1,P2,P0,P4> 12 January 2019 © Copyright Virtual University of Pakistan
42
© Copyright Virtual University of Pakistan
Example Executing safety algorithm shows that sequence <P3,P1,P2,P0,P4> satisfies safety requirement. Yes, P0’s request may be granted immediately. 12 January 2019 © Copyright Virtual University of Pakistan
43
© Copyright Virtual University of Pakistan
More Examples Can request for (3,3,0) by P4 be granted? Do it yourself! 12 January 2019 © Copyright Virtual University of Pakistan
44
© Copyright Virtual University of Pakistan
Recap of Lecture Deadlock avoidance Banker’s algorithm Safety algorithm Safe state Safe sequence 12 January 2019 © Copyright Virtual University of Pakistan
45
Lecture 28 Syed Mansoor Sarwar
Operating Systems Lecture 28 Syed Mansoor Sarwar
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.