june 13 deadlock
TRANSCRIPT
-
7/30/2019 June 13 Deadlock
1/14
Chapter 81
Deadlock is: A set of blocked processes each holding a
resource and waiting to acquire a resource
held by another process in the set.
Example (hardware resources):
System has 2 tape drives.
P1 and P2 each hold one tape drive and each
needs another one.
Example (software resource:):
semaphoresA
andB
, initialized to 1P0 P1
wait (A); wait(B)
wait (B); wait(A)
signal(B); signal(A)
signal(A); signal(B)
-
7/30/2019 June 13 Deadlock
2/14
Chapter 82
The (4) Conditions for Deadlock
There are three policy conditions that must hold fora deadlock to be possible(the necessary conditions):
1. Mutual Exclusion
only one process at a time may use a resource
2. Hold-and-Wait
a process may hold allocated resources while awaiting
assignment of others
3. No Preemption
a resource can be released only voluntarily by the
process holding it, after that process has completed its
task
..and a fourth condition which must actually arise
to make a deadlock happen
-
7/30/2019 June 13 Deadlock
3/14
Chapter 83
The Conditions for Deadlock
In the presence of these necessary conditions,onemore condition must arise for deadlock to actually
occur:
4.Circular Wait
a closed chain of processes exists, such that each
process holds at least one resource needed by the next
process in the chain
-
7/30/2019 June 13 Deadlock
4/14
Chapter 84
The Conditions for Deadlock
Deadlock occurs if and only ifthe circular
wait condition is unresolvable
The circular wait condition is unresolvable
if the first 3 policy conditions hold
So the 4 conditions taken togetherconstitute necessary and sufficient
conditions for deadlock
-
7/30/2019 June 13 Deadlock
5/14
Chapter 85
Resource Allocation Graph:A Cycle But No Deadlock
Multiple
instances
of resources R1and R2:
P2 and P4 can
complete,
freeing
up resources for
P1 and P3
-
7/30/2019 June 13 Deadlock
6/14
Chapter 86
Approaches to Handling Deadlocks
Deadlock Prevention
disallow 1 of the 3 necessary conditions ofdeadlock occurrence, or prevent the circularwait condition from happening
Deadlock Avoidance
do not grant a resource request if thisallocation might lead to deadlock
Deadlock Detection and Recovery grant resource requests when possible, but
periodically check for the presence of deadlockand then take action to recoverfrom it
-
7/30/2019 June 13 Deadlock
7/14Chapter 87
Deadlock Avoidance
We accept the 3 policy conditions but makejudicious choices to assure that the deadlock
point is never reached
Allows more concurrency than prevention
Two approaches: do not start a process if its total demand might lead to
deadlock: (Process Initiation Denial), or
do not grant an incremental resource request if this
allocation couldlead to deadlock: (Resource Allocation
Denial)
In both cases: maximum requirements of each
resource must be stated in advance
-
7/30/2019 June 13 Deadlock
8/14Chapter 88
Resource Allocation Denial
A Better Approach:
Grant incremental resource requests if we can prove that
this leaves the system in a state in which deadlock
cannot occur.
Based on the concept of a safe state
Bankers Algorithm:
Tentatively grant each resource request
Analyze resulting system state to see if it is safe.
Ifsafe, grant the request
ifunsafe refuse the request (undo the tentative grant)
block the requesting process until it is safe to grant it.
-
7/30/2019 June 13 Deadlock
9/14
Data Structures for the Bankers Algorithm
Let n = number of processes,
m = number of resource types
Available: Vectorof length m. IfAvailable [j] = k, there are k
instances of resource type Rj currently available
Max:nxmmatrix. IfMax[i ,j] = k, then process Piwill request atmost kinstances of resource type Rj.
Al loc:nxmmatrix. IfAlloc[i ,j] = kthen Piis currently allocated(i.e. holding) k instances ofRj.
Need:nxmmatrix. IfNeed[i ,j] = k, then Pimay need kmoreinstances ofRjto complete its task.
Need[i,j]= Max[i ,j]Al loc[i ,j].
-
7/30/2019 June 13 Deadlock
10/14Chapter 810
Safety Algorithm
1. Let Workand Finishbe vectors of length mand n,respectively. Initialize:
Work:= Available
Finish[i] == falsefor i= 1,2, , n.
2. Find an isuch that both:
Finish[i] == false
NeediWorkIf no such iexists, go to step 4.
3. Work := Work+ Allocationi
(Resources freed when process completes!)Finish[i] := true
go to step 2.
4. IfFinish[i] = true for all i, then the system is in a
safe state.
-
7/30/2019 June 13 Deadlock
11/14Chapter 811
Resource-Request Algorithm for Process Pi
Requesti = request vector for Pi .
Requesti [j] = kmeans process Pi wants k instances of resource
type Rj.
1. IfRequesti Needi go to step 2. Otherwise, error ( processexceeded its maximum claim).
2. IfRequesti
Available, go to step 3. Otherwise Pi mustwait, (resources not available).
3. Allocate requested resources to Pi as follows:
Available := Available - Requesti
Alloci := Alloci + Requesti
Needi
:= Needi
RequestiIfsafe the resources are allocated to Pi.
Ifunsafe restore the old resource-allocation state andblock Pi
-
7/30/2019 June 13 Deadlock
12/14Chapter 812
Example of Bankers Algorithm
5 processesP0 throughP43 resource typesA (10 units),B (5 units), and C(7
units).
Snapshot at time T0:
Al location 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 2P4 0 0 2 4 3 3
-
7/30/2019 June 13 Deadlock
13/14
Chapter 813
Example (cont)
Need = Max
AllocationNeed
A 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 satisfies safety criteria.
-
7/30/2019 June 13 Deadlock
14/14
Chapter 814
Now P1 requests (1,0,2)
Check that Request Available
(that is, (1,0,2) (3,3,2))true.
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