MODULE III (Part 2) : Deadlocks
MODULE III (Part 2) : Deadlocks
DEADLOCKS
Deadlocks : Definition
System Model
• System consists of different resources
• Resource types: R1, R2, . . ., Rm
• CPU cycles, memory space, I/O devices
1. Mutual exclusion
2. Hold and wait
3. No preemption
4. Circular wait
1. Mutual exclusion.
• At least one resource must be held in a nonsharable mode; that is,
only one process at a time can use the resource.
• If another process requests that resource, the requesting process
must be delayed until the resource has been released.
2. Hold and wait.
• A process must be holding at least one resource and waiting to
acquire additional resources that are currently being held by other
processes.
3. No preemption.
• Resources cannot be preempted; that is, a resource can be released
only voluntarily by the process holding it, after that process has
completed its task.
4. Circular wait.
• A set {P0, P1, ..., Pn} of waiting processes must exist such that P0 is
waiting for a resource held by P1, P1 is waiting for a resource held by
P2, ..., Pn−1 is waiting for a resource held by Pn, and Pn is waiting for
a resource held by P0.
Resource-Allocation Graph
Example: Resource
allocation graph with a
Deadlock
• For a deadlock to occur, each of the four necessary conditions must hold.
2. Starvation possible
3. Eliminate No Preemption
● Resources can be preempted from processes.
● Method 1
process is waiting.
• Process will be restarted only when it can regain its old resources, as
Case2:
• If they are not, we check whether they are allocated to some other process that is
waiting for additional resources.
• If so pre-empt from that process and allocate to current process.
Case3:
• If the resources are neither available nor held by a waiting process, the requesting
process must wait.
• While it is waiting, some of its resources may be pre-empted, if another process request
them.
• A process can be restarted only when it is allocated the new resources it is requesting
and recovers any resources that were preempted while it was waiting.
4. Eliminate Circular Wait
● For Example:
• if P1 process is allocated R5 resources, now next time if P1 ask for R4, R3,
lesser than R5 such request will not be granted, only request for resources
more than R5 will be granted.
• let R = {R1, R2, ..., Rm} be the set of resource types.
• We assign to each resource type a unique integer number
• Formally, we define a one-to-one function F: R → N, where N is the set of
natural numbers.
• For example,
F(tape drive) = 1
F(disk drive) = 5
F(printer) = 12
• Each process can request resources only in an increasing order of
enumeration.
• That is, a process can initially request any number of instances of a resource
type say, Ri .
• After that, the process can request instances of resource type Rj if and only if
F(Rj) > F(Ri)
2. Deadlock Avoidance
● Requires that the system has some additional prior information available.
● Simplest and most useful model requires that each process declares the
maximum number of resources of each type that it may need.
Safe and unsafe states
● If the system cannot fulfill the request of all processes then the state of the system
is called unsafe.
2. Bankers Algorithm
• Used when multiple instances of a resource type are
present
1. Resource-Allocation Graph Algorithm
● Used if single instance of each resource type
● Edges
● Request edge
● Assignment edge
● Claim edge
Conclusion:
a) Safety Algorithm
- algorithm for finding out whether or not a system is in a
safe state (using a safe sequence).
b) Resource-Request Algorithm
- algorithm for determining whether requests can be safely
granted.
• When a new process enters the system, it must declare the maximum
number of instances of each resource type that it may need.
• This number may not exceed the total number of resources in the
system.
• When a user requests a set of resources, the system must determine
whether the allocation of these resources will leave the system in a
safe state.
• If it will, the resources are allocated; otherwise, the process must wait
until some other process releases enough resources.
• We need the following data structures, where n is the number of
processes in the system and m is the number of resource types:
a) Safety Algorithm
• We can now present the algorithm for finding out whether or not a system
is in a safe state. This algorithm can be described as follows:
Updated Available
Final Safe Sequence
b) Resource-Request Algorithm
• Next, we describe the algorithm for determining whether requests can be
safely granted.
• Let Requesti be the request vector for process Pi . If Requesti [ j] == k, then
process Pi wants k instances of resource type Rj .
• When a request for resources is made by process Pi , the following actions
are taken:
• We must determine whether this new system state is safe.
• To do so, we execute our safety algorithm and find that the sequence
<P1, P3, P4, P0, P2> satisfies the safety requirement.
• Hence, we can immediately grant the request of process P1.
• You should be able to see, however, that when the system is in this
state, a request for (3,3,0) by P4 cannot be granted, since the
resources are not available.
• Furthermore, a request for (0,2,0) by P0 cannot be granted, even
though the resources are available, since the resulting state is unsafe.
3. Deadlock Detection
• To detect deadlock there are two options based on the instances of
resource type:
• Single Instance of Each Resource Type
• Several Instances of a Resource Type
a) Single Instance of Each Resource Type
• Deadlock detection algorithm 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.
• 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.
• A deadlock exists in the
system if and only if the
wait-for graph contains
a cycle.
• To detect deadlocks, the
system needs to
maintain the wait for
graph and periodically
invoke an algorithm
that searches for a cycle
in the graph.
b)Several Instances of a Resource Type
• The wait-for graph scheme is not applicable to a resource-allocation system with
multiple instances of each resource type.
• A deadlock detection algorithm that is applicable to such a system.
• The algorithm employs several time-varying data structures that are similar to
those used in the banker’s algorithm: