Deadlock
Deadlock
Deadlocks
During the execution of processes, a process requests for resource, uses it and releases it. A process
may request as many resources it wants to execute its task. Under normal mode of operations, a
process may utilize a resource only in following sequence:
● Request: The process requests for a resource, if the process doesn’t get the resource then
the requesting process must wait till it acquires the resource.
● Use: The process can operate on the resource.
● Release: The process releases the resource
1
Operating Systems
Pictorially the processes are represented as circle and resource type as a rectangle. The instances
for each resource type are represented within a rectangle with a dot.
Resource Instances
Process States
Given the definition of resource allocation graph, if a graph contains no cycles then deadlock
doesn’t exist. If a graph consists a cycle then a deadlock may exist.
Also if a resource type has only 1 instance and cycle exist then a deadlock has occurred. Each
process involved in a cycle is deadlocked.
If each resource type has multiple instances and a cycle exists, then it is not necessary for deadlock
to exist. It may or may not exist.
2
Operating Systems
3
Operating Systems
● A protocol which prevents or avoid deadlocks, ensuring that a system will never enter a
deadlock.
● To allow a system to enter into a deadlock, detect it and recover.
● Ignore the problem altogether and pretend that the deadlock never occurs in the system.
Most of the operating systems use the third options. It is up to the application developer to write
programs which handle deadlock. To ensure that a deadlock never occurs, the system can either use
deadlock prevention or deadlock avoidance scheme.
● Deadlock prevention provides a set of methods for ensuring that at least one of the
necessary conditions cannot hold. These methods prevent deadlocks by constraining how a
request for resources can be made.
● Deadlock Avoidance requires that an operating system be given with advance information of
resources required by the process during its lifetime, so that it can decide for each
requestwhether or not the process should wait. To make this decision, the system must
consider the resources currently available, the resources currently allocated to each process
and the future requests and releases of each process.
● If both deadlock prevention and deadlock avoidance is not employed, then deadlock may
occur. Here, the system can use an algorithm to examine the system for any deadlock
occurrence and another algorithm to recover from deadlock.
● If the above 3 techniques are not used, then the system may arrive at deadlock state where
the system performance deteriorates as the resources are held by processes cannot run and
more processes requests for that resources. Eventually the system will stop functioning and
will need to be restarted manually.
Most of the operating systems use the 4th option as the deadlock occurs infrequently (once a year)
and thus this method is cheaper compared to the rest 3 options.
Deadlock Prevention:
Deadlock prevention provides a set of methods for ensuring that at least one of the necessary
conditions cannot hold.
Mutual Exclusion:
● Mutual Exclusion condition must hold for non-sharable resources. For example, a printer is
a non-sharable resource and it requires mutually exclusive access, as many processes
cannot use printer at the same time.
● Sharable resources cannot be mutually exclusive. Example, Read only files. These read-only
files can be accessed by many processes at same time.
● A process never needs to wait for sharable resources. In general, we cannot prevent
deadlocks by denying the mutual exclusion condition because some resources are
intrinsically non-sharable.
Hold and Wait:
To ensure hold and wait condition to be prevented, whenever a process requests for a resource, it
does not hold any resources.
4
Operating Systems
● One protocol that can be used requires each process to request and be allocated all its
resources at the beginning of execution.
● Another protocol allows a process to request resources only when it has none. A process
may request some resources, use them. However if they require additional resources, it
must release all the resources that are currently allocated.
Both these protocols have some disadvantages,
● Resource utilization may be low, since resources may be allocated and unused for long time.
● Starvation is possible. A process may be in need of some popular resources, it may need to
wait indefinitely to get them.
No preemption:
To ensure that this condition does not hold, the following protocols can be used
● If a process is holding some resource and requests another resource, and if that is not
allotted immediately, then all the resources that are currently allotted to process is
preempted. The preempted resources are added to the list of resources that the process is
waiting. The process is restarted only when it can regain its old resources as well as new
ones that it was requesting.
● If a process requests some resources, it will be checked whether it is available. If so, then it
is allotted. If it is not available, then check is made whether that resource is allotted to some
other process which is waiting for additional resources. If so, the desired resource is
preempted from the waiting process and is allotted to requesting process. If the resource is
neither available nor held by any other waiting process, then the requesting process must
wait. While it is waiting, some of its resources can be preempted only if any other process
requests it.
These protocols can be used on the resources whose state can be easily saved and restored later
such as CPU registers and memory space.
Circular Wait:
One way to ensure that this condition never holds is to impose a total ordering of all resource types
and to require that each process requests resource in an increasing order of enumeration.
Here, let R = {R1, R2,…., Rm} be the set of resource types. Each resource type is assigned with a unique
integer number. The one to one function F:R N, where N is the set of natural numbers.
For example, if R includes tape drives, disk drives and printers then F can be defined as
F (tape drive) = 1
F (disk drive) = 5
F (printers) = 12
● Each process can request resources only in an increasing order of enumeration. That is a
process can request any number of instances of resource type Ri after that the process can
request any number of instances of resource type Rj only if F ( Rj ) > F ( Ri ). In the above eg,
5
Operating Systems
if a process requires tape drives and printer at same time, it must 1st request tape drive and
then request for printer.
● A process requesting an instance of resource type Rj must have released any resources Ri
such that F( Ri ) >= F( Rj ).
If these 2 protocols are used, then circular wait condition cannot hold.
Deadlock Avoidance:
The side effects of deadlock prevention like low device utilization and reduced system throughput,
the deadlock avoidance techniques are used.
Here, the system requires additional information about how resources are to be requested and
allocated. Based on the information, the system will decide whether to allocate the resource directly
or wait until further resources are released. To make this decision, the system considers the
currently available resources, the resources currently allocated to the process and the future
requests and releases of each process.
The various algorithms that use this approach differ in the amount and type of information
required. The simplest algorithm requires the maximum number of resources of each type that it
may need. Based on this, it is possible to construct an algorithm that ensures that the system will
never enter the deadlock state. Such an algorithm defines deadlock avoidance approach. The
deadlock avoidance algorithm dynamically examines the resource allocation state to ensure
circular wait condition does not exist. The resource allocation state is defined by the number of
available and allocated resources and the maximum demands of the processes.
Safe State:
A state is safe if the system can allocate the resources to each process in some order and still the
system will avoid the deadlock. A system is said to be safe state only if there exists a safe sequence.
A sequence of processes < P1, P2, P3,…, Pn> is a safe sequence for the current allocation state if, for
each Pi, the resource request by process Pi, can still be satisfied by currently available resources
plus the resources held by process Pj. If the required resources are not available, then Pi can wait
until all the Pj have finished.When they have finished, Pi can obtain all the required resources and
can finish its task. If no such sequence exists, then the system is said to be in unsafe state.
6
Operating Systems
unsafe state, the operating system cannot prevent processes from requesting resources in such a
way that a deadlock occurs. The behavior of processes controls unsafe states.
Deadlock avoidance algorithm can be divided on the basis if instances,
● Available: a vector of length m indicates the number of resources which are available of each
type. If available[j]=k, then it means that there k instances of resource type j are available for
use.
7
Operating Systems
● Max: A vector of length n x m matrix defines the maximum demand of each process. If
Max[i][j]=k, it means that process Pi may request atmost k instances of resource type Rj.
● Allocation: A vector of length n x m matrix defines the number of resources of each type
currently allocated to each process. If Allocation[i][j]=k, then the process Pi has been allocated
with k instances of resource type Rj.
● Need: A vector of length n x m matrix defines the remaining resource need of each of the
process. If Need[i][j]=k, then process Pi needs k instances of resource type Rj. Need [i][j] = Max
[i] [j] – Allocation [i] [j]
Safety Algorithm:
This algorithm is used for finding out whetehr or not a system is in a safe state.
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize: Work = Available
Finish [i] = false for i = 0, 1, …, n- 1
2. Find an i such that both:
(a) Finish [i] = false
(b) Needi≤Work
3. If no such i exists, go to step 4
Work = Work + Allocationi
Finish[i] = true
go to step 2
4. If Finish [i] == true for all i, then the system is in a safe state
Resource Request Algorithm:
Request = request vector for process Pi. If Requesti[j] = k then process Pi wants k instances of
resource type Rj
1. If Requesti≤Needigo to step 2. Otherwise, raise error condition, since process has exceeded
its maximum claim
2. If Requesti≤Available, go to step 3. Otherwise Pi must wait, since resources are not available
3. Pretend to allocate requested resources to Pi by modifying the state as follows:
Available = Available – Request;
Allocationi= Allocationi + Requesti;
Needi= Needi – Requesti;
If resource allocation state is safe, then transaction is completed and the process Piis allocated with
the resources. If new state is unsafe, then Pi must wait for the requesti and the old resource-
allocation state is restored.
Refer Banker’s algorithm problems done in class
Deadlock Detection:
If a system does not employ either a deadlock prevention or deadlock avoidance algorithm then the
deadlock situation may occur. Here, the system may provide
8
Operating Systems
● An algorithm that examines the state of the system to determine whether a deadlock has
occurred
● An algorithm to recover from the deadlock.
Along this process of detection and recovery, the requires overhead that includes not only the run
time costs of maintaining the necessary information and executing the detection algorithm but also
the potential losses inherent in recovering from a deadlock.
In case there is single instance of each resource type, then a deadlock detection algorithm which a
variant of resource allocation graph is defined called as wait-for graph.
A wait-for graph eliminates the resource nodes and collapses the appropriate edges which is
present in resource allocation graph. In a wait-for graph, an edge from Pi to Pj implies that a process
Pi is waiting for a 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.as shown in Fig 4.6
9
Operating Systems
10
Operating Systems
Resource Preemption
To eliminate deadlocks using resource preemption, the resources from the process are successively
preempted and these preempted resources are given to other processes until the deadlock cycle is
broken.
If preemption is required to deal with deadlocks, then three issues need to be addressed:
● Selecting a Victim:
As in process termination, we must determine the order of preemption to minimize cost.
The parameters which determines the cost are the number of resources held by the
deadlocked process and the amount of time the process thus far consumed during its
execution.
● Rollback:
If a resource is preempted, the process cannot continue its normal execution. So the process
is roll backed to some safe state and restarted from that state only.
Since it is difficult to determine safe state, the solution is total rollback. Abort the process
and restart it. To roll back a process till its necessary to break a deadlock, the system
requires to keep more information about the state of all running processes.
● Starvation:
In a system where victim selection is based on cost factor, same process may be picked as
victim leading that process to be under starvation state. To overcome this, a number of
rollbacks in cost factor is included clearly picking a victim only for finite number of times.
11