safe state -> unsafe state.
• If P2 is given 1 more tape drive, then no longer safe -> Av. = 3 -1 = 2
• What if P0 now needs remaining 5 tape drives to complete; since there are only
2 available
(3 – 1), P0 waits
• What if P2 also now needs remaining 6 tape drives to complete; since there are only 2 available, P2 waits
• Even if P1 completes and releases resources, P0 & P2 deadlocked.
Deadlock Avoidance in Single Instance of Resources
Claim edge
P
i
→
R
j
indicated that process
Pi
may request resource
Rj;(虚线表示)
After the cycle check, if it is confirmed that there will be no circular wait, the claim edge is converted to a request edge. Otherwise, it will be rejected.
Request edge converted to an assignment edge when the resource is allocated to the process
Deadlock Avoidance in Multiple Instances of Resources - Banker’s Algorithm
Banker’s algorithm:
Safety Test algorithm
The banker’s algorithm has two parts:
Safety Test algorithm
that checks the current state of the system for its safe state.
Resource request algorithm
that verifies whether the requested resources, when allocated to the process, affect
the safe state. If it does, the request is denied.
Data Structures for the Banker
’
s Algorithm
n = number of processes m
= number of resources types
Available
:
Vector of length
m
. If
Available [
j
] =
k
, there are
k instances of resource type
R
j
available
Max
: n*m
matrix. If
Max
[
i,j
] =
k
, then process
P
i
may request at most
k
instances of resource type
R
j
Allocation
: n
*
m
matrix. If
Allocation[
i,j
] =
k
then
P
i
is currently allocated
k
instances of
R
j
Need
: n
x
m
matrix. If
Need
[
i,j
] =
k
, then
P
i
may need
k
more instances of
R
j
to complete its task
Need
[
i,j]
=
Max
[
i,j
] –
Allocation
[
i,j
]
e.g.
5 processes
P
0
through
P
4
;
3 resource types
:
A (10
instances
),
B
(5
instances
),
and
C
(7
instances
)
Snapshot at time
T
0
:
The system is in a safe state since the sequence <
P
1
,
P
3
,
P
4
,
P
2
,
P
0
> satisfies safety criteria
IF P1 Request (1,0,2)
Check that Request Available (that is, (1,0,2) <= (3,3,2) true
Executing safety algorithm shows that sequence <
P
1
,
P
3
,
P
4
,
P
0
,
P
2
> satisfies safety requirement
Deadlock Detection
Detection of single instance of resource
Resource-Allocation Graph Corresponding
wait-for graph
Periodically invoke an algorithm that searches for a cycle in the graph.
If there is a cycle, there exists a deadlock
An algorithm to detect a cycle in a graph requires an order of
n
2
operations, where
n
is the number of vertices in the
graph
Detection for multiple instances of resources
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
[
i
][
j
] =
k
, then process
P
i
is
requesting
k
more instances of resource type
R
j
.
Detection Algorithm
e.g.
5
processes
P
0
through
P
4
;
3
resource types
A
(
7
instances),
B
(
2
instances), and
C
(
6
instances)
Snapshot at time
T
0
:
Sequence <P0 , P2 , P3 , P1 , P4> will result in Finish[i] = true for all i
IF
P
2
requests
1
instance of type
C
then have a deadlock
Detection-Algorithm Usage
- How often a deadlock is likely to occur?
-
How many processes will be affected by deadlock when
it happens?
If detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph and so we would not be
able to tell which of the many deadlocked processes
“
caused
”
the deadlock.
Recovery from Deadlock
2 options for breaking a deadlock
❑
Process Termination / Abort Process
❑
Resource Preemption
Process Termination
➢
Abort all deadlocked processes
➢
Abort one process at a time until the deadlock cycle is eliminated
Many factors may affect which process is chosen
1.
Priority of the process
2.
How long process has computed, and how much longer to
completion
3.
Resources the process has used
4.
Resource's process needs to complete
5.
How many processes will need to be terminated
6.
Is process interactive or batch?
Resource Preemption
Select a victim
–
a process, whose execution has just started and requires many resources to complete, will be the
right victim for preemption (minimize cost).
Rollback
–
return the process to some safe state (safe checkpoint), restart it from that state
Starvation
–
it may be possible that the same process is always chosen for resource preemption, resulting in a
starvation situation. Thus, it is important to ensure that the
process will not starve. This can be done by fixing the number
of times a process can be chosen as a victim.