Professional Documents
Culture Documents
Unit III Deadlocks-1
Unit III Deadlocks-1
Unit III
System Model
• Process
..
..
• Resource Type with 4 instances
• Deadlock prevention
• Deadlock avoidance
• Deadlock Detection & Recovery from
Deadlock
Deadlock Prevention
• Deadlock prevention provides a set of methods to
ensure that at least one of the necessary conditions
cannot hold
• Mutual Exclusion – not required for sharable resources; must
hold for non-sharable resources.
• Hold and Wait – must guarantee that whenever a process
requests a resource, it does not hold any other resources.
– Require process to request and be allocated all its resources before it
begins execution, or allow process to request resources only when the
process has none.
– Low resource utilization; starvation possible
Deadlock prevention
• No-Preemption
• If a process that is holding some resources requests
another resource that cannot be immediately allocated to
it, then all resources currently being held are released.
• Preempted resources are added to the list of resources
for which the process is waiting.
• Process will be restarted only when it can regain its old
resources, as well as the new ones that it is requesting.
Deadlock prevention
• Circular Wait – impose a total ordering of all
resource types, and require that each process requests
resources in an increasing order of enumeration.
Deadlock Avoidance
• Deadlock avoidance requires that the operating
system be given additional information in advance
concerning which resources a process will request
and use during its lifetime.
• With this additional knowledge, the operating system
can decide for each request whether or not the
process should wait
Deadlock Avoidance
• The simplest and most useful model requires that each process declare
the maximum number of resources of each type that it may need
• Given this a priori information, it is possible to construct an algorithm
that ensures that the system will never enter a deadlocked state
• A deadlock-avoidance algorithm dynamically examines the resource-
allocation state to ensure that a circular-wait condition can never exist.
• The resource allocation state is defined by the number of available and
allocated resources and the maximum demands of the processes.
Safe State
Process Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
• The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety
criteria.
Example: P1 Request (1,0,2)
• Check that Request1 Available (that is, (1,0,2) (3,3,2) true.
Allocation Need Available
ABC ABC ABC
P0 0 1 0 7 4 3 2 3 0
P1 302020
P2 302600
P3 211011
P4 002431
• 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?
Deadlock Detection
• If a system does not employ either a deadlock-
prevention or a deadlock avoidance algorithm, then a
deadlock situation may occur
– An algorithm that examines the state of the system
to determine whether a deadlock has occurred
– An algorithm to recover from the deadlock
Single Instance of Each Resource Type
• If all resources have only a single instance, then we
can define a deadlock detection algorithm that uses a
variant of the resource-allocation graph, called a
wait-for graph
• We obtain this graph from the resource-allocation
graph by removing the resource nodes and collapsing
the appropriate edges.
Single Instance of Each Resource Type
• More precisely, an edge from Pi to Pj in a wait-for graph implies
that process Pi is waiting for process Pj to release a resource
that Pi needs.
• An edge Pi → Pj exists in a wait-for graph if and only if the
corresponding resource allocation graph contains two edges Pi
→ Rq and Rq → Pj for some resource Rq
• Maintain wait-for graph
– Nodes are processes.
– Pi Pj if Pi is waiting for Pj.
• Periodically invoke an algorithm that searches for a cycle in the
graph. If there is a cycle, there exists a deadlock.
Resource-Allocation Graph and Wait-for Graph