Академический Документы
Профессиональный Документы
Культура Документы
Deadlock
Potential Deadlock
I need quad
I need quad A and B
D and A
Conditions for possible Deadlock
• Mutual exclusion
– Only one process may use a resource at a time
• Hold-and-wait
– A process may hold allocated resources while awaiting
assignment of other resources
• No pre-emption
– No resource can be forcibly removed form a process
holding it
HALT until B
HALT until A is free
is free
Deadlock
• A set of processes is deadlocked when each
process in the set is blocked awaiting an event
that can only be triggered by another blocked
process in the set
– Typically involves processes competing for the
same set of resources
Resource Categories
Two general categories of resources:
• Reusable
– can be safely used by only one process at a time
and is not depleted by that use.
• Consumable
– one that can be created (produced) and destroyed
(consumed).
Reusable Resources
• Such as:
– Processors, I/O channels, main and secondary
memory, devices, and data structures such as files,
databases, and semaphores
• Deadlock occurs if each process holds one
resource and requests the other
Example of Deadlock on Reusable Resource
P1 P2
... ...
Request 80 Kbytes; Request 70 Kbytes;
... ...
Request 60 Kbytes; Request 80 Kbytes;
• The best way to deal with this particular problem is, in effect, to
eliminate the possibility by using virtual memory, to be discussed
later
Methods for handling deadlock
There are three ways to handle a deadlock:
1) Deadlock prevention or avoidance: The idea is to not
let the system into deadlock state.
• prevent deadlock
– adopt a policy that eliminates one of the conditions
(conditions 1 through 4).
• avoid deadlock
– by making the appropriate dynamic choices based on the
current state of resource allocation.
This later scheme would prevent deadlock only if no two processes possessed the
same priority.
This approach is practical only with resources whose state can be easily saved and
restored later, as is the case with a processor.
• Circular Wait
Can be prevented by defining a linear ordering of resource types. i.e Each resource
will be assigned with a numerical number. A process can request for the
resources only in increasing order of numbering.
Resources
Amount of
available after
Existing
allocation
Resources
Process i
• Cij - Aij ≤ Vj, for all j
• This is not possible for P1,
– which has only 1 unit of R1 and requires 2 more
units of R1, 2 units of R2, and 2 units of R3.
• If we assign one unit of R3 to process P2,
– Then P2 has its maximum required resources
allocated and can run to completion and return
resources to ‘available’ pool
After P2
runs to completion
• Can any of the remaining processes can be
completed?
Note P2 is
completed
After P1 completes
P3 Completes
This time
Suppose that P1
makes the
request for one
additional unit
each of R1 and
R3.
Is this safe?
Deadlock Avoidance
• When a process makes a request for a set of
resources,
– assume that the request is granted,
– Update the system state accordingly,
• Then determine if the result is a safe state.
– If so, grant the request and,
– if not, block the process until it is safe to grant the
request.
Example of Banker’s Algorithm
• 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 2 0 0 322
P2 3 0 2 902
P3 2 1 1 222
P4 0 0 2 433
29
Example (Cont.)
• The content of the matrix Need is defined to be Max –
Allocation.
Need
ABC
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.
30
Example: P1 Request (1,0,2) ?
• Check that Request Available (that is, (1,0,2) (3,3,2)
true.
Allocation Need Available
ABC ABC ABC
P0 0 1 0 743 230
P1 3 0 2 020
P2 3 0 1 600
P3 2 1 1 011
P4 0 0 2 431
• 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?
31
Example (Cont.)
• Request for (3,3,0) by P4 cannot be granted, resources are
not available; request is greater than what is available.
32
Deadlock Avoidance Advantages
1. Deadlock avoidance has the advantage that It is not
necessary to preempt and rollback processes, as in
deadlock detection,
2. It is less restrictive than deadlock prevention.
2. W = (0 0 0 0 1).
3. The request of process P3 is less than or equal to W, so mark P3 and set W=W + (0 0
0 1 0) = (0 0 0 1 1).
The algorithm concludes with P1 and P2 unmarked, indicating that these processes are
deadlocked.
Detection Algorithm cont.
• The strategy in this algorithm is to find a process
whose resource requests can be satisfied with the
available resources,
• and then assume that those resources are granted
and that the process runs to completion and
releases all of its resources.
– The algorithm then looks for another process to satisfy.
Advantages
• sometimes it is possible to see a deadlock directly by using
RAG, but then you might not be able to know that by
looking at the vector & matrices.
• Vector & matrices are better if the system contains lots of
processes and resources and Graph (RAG) is better if the
system contains less number of processes and resources.
Resource Allocation Graph (RAG)
In RAG vertices are of two types:
1. Process vertex – The process will be represented
with a circle.
2. Resource vertex – The resource will be
represented by a box. There are two type of
resources:
Single instance type resource – Represented as a box
with one dot.
Multi-resource instance type resource – Represented
as a box with many dots. So the number of dots indicate
how many instances are there.
Resource Allocation Graph (RAG)
Resource Allocation Graph (RAG)
There are two types of edges in RAG
1. Assign Edge – already assigned a resource to a process
So, there is no deadlock in this RAG, eventhough there is a cycle. Therefore in multi-
instance resource cycle is not sufficient condition for deadlock.
Resource Allocation Graph (RAG)
The Available resource is = (0, 0), but requirements are (0, 1),
(1, 0) and (1, 0).So no any one requirement can be fulfilled. The
system is in deadlock.