4778 3 71 Module 3 Deadlock
4778 3 71 Module 3 Deadlock
DEADLOCKS
Deadlock : Definition
System Model
• System consists of different resources
• Resource types: R1, R2, . . ., Rm
• CPU cycles, memory space, I/O devices
•A process in operating system uses resources in the following way:
1. Request
• The process requests the resource.
• If the request cannot be granted immediately (for example, if the resource is
being used by another process), then the requesting process must wait until it
can acquire the resource.
2. Use
• The process can operate on the resource (for example, if the resource is a
printer, the process can print on the printer).
3. Release
• The process releases the resource.
• The request and release of resources may be system calls
• Examples are the request() and release() device, open() and close() file, and
allocate() and free() memory system calls.
• Similarly, the request and release of semaphores can be accomplished through
the wait() and signal() operations on semaphores or through acquire() and
release() of a mutex lock.
• A system table records whether each resource is free or allocated.
• For each resource that is allocated, the table also records the process to which
it is allocated.
• If a process requests a resource that is currently allocated to another process, it
can be added to a queue of processes waiting for this resource.
Deadlock Characterization
• A deadlock situation can arise if
the following four conditions hold
simultaneously in a system:
1. Mutual exclusion
2. Hold and wait
3. No preemption
4. Circular wait
1. Mutual exclusion
• Only one process can use a resource at a time.
• At least one resource must be held in a nonsharable mode.
• If another process requests that resource, the requesting process must
be delayed until the resource has been released.
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 (RAG)
Example: Resource allocation graph with a
Deadlock
• For a deadlock to occur, each of the four necessary conditions must hold.
● Method 1
• 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.
● Method 2
Case1:
• If a process request some resources, we first check whether they are available.
• If they are, allocate them.
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:
● 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
● The request can be granted only if converting the request edge to an assignment edge
does not result in the formation of a cycle in the resource allocation graph.
Resource-Allocation Graph
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:
Updated Available
Final Safe Sequence
Problem 1
Problem 3
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:
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:
Tutorial 3 : Q1
Q2
• Given the following snapshot of a system at time T0. Find whether the
system is in a safe state using Banker's algorithm. If P1 generates a
request for (0,4,2,0), can the request be granted immediately?
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.
• The algorithm employs several time-varying data structures that are similar to
those used in the banker’s algorithm:
2. Rollback.
-If we preempt a resource from a process, what should be done with that process?
3. Starvation.
-How do we ensure that starvation will not occur? That is, how can we guarantee
that resources will not always be preempted from the same process.