Nothing Special   »   [go: up one dir, main page]

OS - CS - 403 Unit - 3.2 Deadlocks - 2020

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 29

Unit_3.

2
Deadlocks
Resource-Allocation Graph Algorithm

• Claim edge Pi  Rj indicated that process Pi may


request resource Rj; represented by a dashed line.
• Claim edge converts to request edge when a
process requests a resource.
• Request edge converts to assignment edge when a
resource is allocated to a process.
• When a resource is released by a process,
assignment edge reconverts to a claim edge.
• Resources must be claimed a priori in the system.
Resource-Allocation Graph For Deadlock Avoidance
Unsafe State In Resource-Allocation Graph
Banker’s Algorithm

(The resource-allocation graph algorithm is not


applicable to a resource-allocation system with
multiple instances of each resource type.)
• Multiple instances.
• Each process must a priori claim of 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.
Data Structures for the Banker’s Algorithm
Let n = number of processes, and m = number of resources types.

• 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].
Example of Banker’s Algorithm
Consider the following snap shot of the system with 5
processes P0 through P4; 3 resource types A (10 instances),
B (5instances), and C (7 instances). Snapshot at time T0:
Allocation Max Available
ABC ABC ABC
P0 010 753 332
P1 200 322
P2 302 902
P3 211 222
P4 002 433
Example (Cont.)

• The content of the matrix. Need is defined to be Max


– Allocation.
Need
ABC
P0 7 4 3
P1 122
P2 600
P3 011
P4 431
Safety Algorithm

1. Let Work and Finish be vectors of length m and n,


respectively. Initialize:
Work = Available
Finish [i] = false for i - 1,2, …, n.
2. Find an i such that both:
(a) Finish [i] = false
(b) Needi  Work
If no such i exists, go to step 4.
3. Work = Work + Allocationi
Finish[i] = true
go to step 2.
4. If Finish [i] == true for all i, then the system is in a safe
state.
Example (Cont.)
Consider the following snap shot of the system with 5 processes P0
through P4; 3 resource types A (10 instances), B (5instances), and C
(7 instances). Snapshot at time T0:
Allocation Max NEED Available
ABC ABC ABC ABC
P0 010 753 743 332
P1 200 322 122
P2 302 902 600
P3 211 222 011
P4 002 433 431
Is the System in safe state? Justify your answer.
Example (Cont.)

Applying the Banker’s Algorithm,


Let Avail be a vector of length 3.
Let Avail = Available,
Avail = (3, 3, 2)
Iteration 1. Check all processes , starting from P0.
P0 Need > Avail; so P0 is Not Marked.
P1 Need < Avail; so P1 is Marked.
Now Avail = Avail + Allocated [1] = (3,3,2) + (2,0,0) = (5,3,2)
P2 Need > Avail; so P2 is Not Marked.
P3 Need < Avail; so P3 is Marked.
Now Avail = Avail + Allocated [3] = (5,3,2) + (2,1,1) = (7,4,3)
P4 Need < Avail; so P4 is Marked.
Now Avail = Avail + Allocated [4] = (7,4,3) + (0,0,2) = (7,4,5)
Iteration 2. As only P0 and P2 left. Now Check P0 and P2
Allowing P0 first will take the system in unsafe state so FIRST P2 is Marked.
System is in a safe state since the sequence <P1,P3,P4,P2,P0> satisfies
safety criteria.
Resource-Request Algorithm for Process Pi

Request = request vector for process Pi. If Requesti [j] = k then process
Pi wants k instances of resource type Rj.
1. If Requesti  Needi go to step 2. Otherwise, raise error
condition, since process has exceeded its maximum claim.
2. If Requesti  Available, go to step 3. Otherwise Pi must wait,
since resources are not available.
3. Pretend to allocate requested resources to Pi by modifying the
state as follows:
Available = Available – Requesti ;
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
Example P1 Request (1,0,2) (Cont.)
Example P1 Request (1,0,2) (Cont.)

Check that:
Request  Need (that is, (1,0,2)  (1,2,2)  true.
and Request  Available (that is, (1,0,2)  (3,3,2)  true.
Allocation Need Available
ABC A B CA B C
P0 010743 230
P1 3 0 20 2 0
P2 3 0 26 0 0
P3 211011
P4 002431
Executing safety algorithm shows that sequence <P1, P3, P4,
P0, P2> satisfies safety requirement.
Try Yourself
1. Can request for (3,3,0) by P4 be granted?

2. Can request for (0,2,0) by P0 be granted?


Consider 5 processes P0 through P4; 3 resource types A (12
instances), B (8 instances), and C (9 instances). Calculate the
need matrix for snapshot given below at time T0. Can
request for (3,2,0) by P4 be granted?
Allocation Max Available
ABC ABC ABC
P0 201 864 5 6 4
P1 100 422
P2 211 812
P3 111 222
P4 102 433
Wait-for Graph

1. Single Instance of Each Resource Type

2. Nodes are processes.

3. Pi  Pj if Pi is waiting for Pj.


Resource-Allocation Graph and Wait-for Graph

Resource-Allocation Graph Corresponding wait-for graph


Several Instances of a Resource Type

• Available: A vector of length m indicates the number of


available resources of each type.
• Allocation: An n x m matrix defines the number of
resources of each type currently allocated to each
process.
• Request: An n x m matrix indicates the current request
of each process. If Request [ij] = k, then process Pi is
requesting k more instances of resource type. Rj.
Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively
Initialize:
(a) Work = Available
(b) For i = 1,2, …, n, if Allocationi  0, then
Finish[i] = false ; otherwise, Finish[i] = true.
2. Find an index i such that both:
(a) Finish[i] == false
(b) Requesti  Work
If no such i exists, go to step 4.
3. Work = Work + Allocationi
Finish[i] = true
go to step 2.
4. If Finish[i] == false, for some i, 1  i  n, then the system is in
deadlock state. Moreover, if Finish[i] == false, then Pi is deadlocked.
Example of Detection Algorithm

• Five processes P0 through P4; three resource types


A (7 instances), B (2 instances), and C (6 instances).
• Snapshot at time T0:
Allocation Request Available
ABC ABCABC
P0 010000 000
P1 200201
P2 3 0 30 0 0
P3 211100
P4 002002
• Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i.
Example (Cont.)

• P2 requests an additional instance of type C.


Request
ABC
P0 000
P1 201
P2 001
P3 100
P4 002
• State of system?
– Can reclaim resources held by process P0, but insufficient resources to
fulfill other processes; requests.
– Deadlock exists, consisting of processes P1, P2, P3, and P4.
Recovery from Deadlock: Process Termination
• Abort all deadlocked processes.
• Abort one process at a time until the deadlock cycle is
eliminated.
• In which order should we choose to abort?
– Priority of the process.
– How long process has computed, and how much longer to
completion.
– Resources the process has used.
– Resources process needs to complete.
– How many processes will need to be terminated.
– Is process interactive or batch?
Recovery from Deadlock: 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.
Question PU Exam May 2016

Consider the following snap-shot of a system with current state: (P1, P2, P3,
P4, P5 are processes and A, B, C, D are resources)

Allocated Max Available


Process
A B C D A B C D A B C D
P1 0 0 1 2 0 0 1 2 2 1 0 0
P2 2 0 0 0 2 7 5 0
P3 0 0 3 4 6 6 5 6
P4 2 3 5 4 4 3 5 6
P5 0 3 3 2 0 6 5 2

i. Compute NEED Matrix


ii. Is the system in safe state? Justify.
iii. Is system deadlocked? Justify the answer.
iv. Can a request (0, 1, 0, 0) from P3 be safely granted immediately? Justify the
answer. Show the system state after grant of request.
Solution

(i) NEED Matrix is:


Need [i] = Max[i] – Allocation [i]

Need
Process
A B C D
P1 0 0 0 0
P2 0 7 5 0
P3 6 6 2 2
P4 2 0 0 2
P5 0 3 2 0
(ii) Is the system in safe state? Justify.
Applying the Banker’s Algorithm,
Let Avail be a vector of length 4.
Let Avail = Available, Avail = (2, 1, 0, 0)
Iteration 1. Check all processes , starting from P1 .
P1 Need < Avail; so P1 is Marked.
Now Avail = Avail + Allocated [1] = (2, 1, 0, 0) + (0, 0, 1, 2) = (2, 1, 1, 2)
P2 Need > Avail; so P2 is Not Marked.
P3 Need > Avail; so P3 is Not Marked.
P4 Need < Avail; so P4 is Marked.
Now Avail = Avail + Allocated [4] = (2, 1, 1, 2) + (2, 3, 5, 4) = (4, 4, 6, 6)
P5 Need < Avail; so P5 is Marked.
Now Avail = Avail + Allocated [5] = (4, 4, 6, 6) + (0, 3, 3, 2) = (4, 7, 9, 8)
Iteration 2. Check only P2 and P3 which are still un- Marked.
P2 Need < Avail; so P2 is Marked.
Now Avail = Avail + Allocated [2] = (4, 7, 9, 8) + (2, 0, 0, 0) = (6, 7, 9, 8)
P3 Need < Avail; so P3 is Marked.
Now Avail= Avail+ Allocated [2]=(6, 7, 9, 8)+(0, 0, 3, 4)=(6, 7, 12, 12) = System Capacity
Since all processes got marked, no further iterations are required.
The sequence is P1, P4, P5, P2, P3.
Since a safe sequence exits, the system satisfies the safety criteria.
(iii) So there is no deadlock as justified above that system satisfied the safety criteria.
(iv)Granting of request (0, 1, 0, 0) from P3.
Since the request [3] <Need [3], it is valid request. Also since request [3] <Available[3],
We can examined whether it can be safely granted immediately, which follows as:
Step 1: The system state after making the tentative grant of the request:
New State:
Allocated Max Available Need
Process Process
A B C D A B C D A B C D A B C D
P1 0 0 1 2 0 0 1 2 2 0 0 0 P1 0 0 0 0
P2 2 0 0 0 2 7 5 0 P2 0 7 5 0
P3 0 1 3 4 6 6 5 6 P3 6 5 2 2
P4 2 3 5 4 4 3 5 6 P4 2 0 0 2
P5 0 3 3 2 0 6 5 2 P5 0 3 2 0

Step2: Applying the Banker’s Algorithm to determine whether the new state is safe or
not. If the state is safe, the request can be safely granted immediately, else, grant of the
request is to be deferred.
Question Abraham Silberschatz, Peter Baer Galvin, Greg Gagne

Consider the following snap-shot of a system with current state: (P1, P2, P3,
P4, P5 are processes and A, B, C, D are resources)

Allocated Max Available


Process
A B C D A B C D A B C D
P1 0 0 1 2 0 0 1 2 1 5 2 0
P2 1 0 0 0 1 7 5 0
P3 1 3 5 4 2 3 5 6
P4 0 6 3 2 0 6 5 2
P5 0 0 1 4 0 6 5 6

Answer the following questions using Banker’s Algorithm:


a. What is the content of NEED Matrix.
b. Is the system in a safe state? Justify.
c. Is system deadlocked? Justify the answer.
d. Can a request (0, 4, 2, 0) from P1 be safely granted immediately? Justify the
answer. Show the system state after grant of request.

You might also like