You are on page 1of 16

1

Deadlock:
 Deadlock is a common problem in multi-programming where several
processes share a specific type of mutually exclusive resource.
 A Deadlock is a situation where each of the computer process waits for a
resource which is being assigned to another process. In this situation,
none of the processes gets executed since the resource one needs, is held
by some other process which is also waiting for some other resource to be
released.
 Deadlock is a situation where a set of processes are blocked because each
process is holding a resource and waiting for another resource acquired
by some other process.

Prof. Debabrata Nath (CSE, NITMAS)


2

Example of Deadlock:
 Let us assume that there are two processes (P1 and P2) and two different
resources (R1 and R2). R1 is assigned to P1 and R2 is assigned to P2.
 After some time, P1 demands for R2 which is being used by P2. P1 halts
its execution since it can't complete without R2. P2 also demands for R1
which is being used by P1, therefore P2 also stops its execution.
 In this scenario, a cycle is being formed among the two processes. None
of the process can progress and they are all waiting. The computer
becomes unresponsive since all the processes got blocked.

Necessary Conditions for Deadlock:


 Mutual Exclusion: A resource can only be shared in mutually exclusive
manner. It implies, if two process cannot use the same resource at the
same time.
 Hold and Wait: A process waits for some resources while holding
another resource at the same time.
 No pre-emption: The process which once scheduled will be executed till
the completion. No other process can be scheduled by the scheduler
meanwhile.
 Circular Wait: All the processes must be waiting for the resources in a
cyclic manner so that the last process is waiting for the resource which is
being held by the first process.

Prof. Debabrata Nath (CSE, NITMAS)


3

Starvation:
 Starvation is a phenomenon associated with the priority scheduling
algorithms, in which a process ready to run can wait indefinitely because
of low priority. In heavily loaded computer system, a steady stream of
higher-priority processes can prevent a low priority process from ever
getting the CPU.
 Starvation is a resource management problem where a process does not
get the resources it needs for a long time because the resources are being
used by other higher priority processes.

Deadlock vs. Starvation:


Deadlock Starvation
Deadlock is a situation where all Starvation is a situation where the low
processes got blocked and no process priority process got blocked and the
proceeds. higher priority processes proceed.
Deadlock is an infinite waiting. Starvation is a long waiting but not
infinite.
Every Deadlock is always a starvation. Every starvation need not be deadlock.
The requested resource is blocked by The requested resource is continuously
another process. used by the higher priority processes.
It happens when mutual exclusion, It occurs due to the uncontrolled
hold and wait, no pre-emption and priority and resource management.
circular wait occurs simultaneously.

Solution of Starvation (Aging):


 Aging is a technique of gradually increasing the priority of processes that
wait in the system for a long time.
 Aging is a technique to avoid starvation in a scheduling system. It works
by adding an aging factor to the priority of each request. The aging factor
must increase the priority as time passes and must ensure that a request
will eventually be the highest priority request.
 So, we are aging our low priority process to make it a high priority
process and as a result, to allocate the CPU for it.

Prof. Debabrata Nath (CSE, NITMAS)


4

 Suppose a process P has a priority number as 75 at 0 ms. Then after every


5 ms (you can use any time quantum), we can decrease the priority
number of the process P by 1 (here also instead of 1, you can take any
other number). So, after 5 ms, the priority of the process P will be 74.
Again after 5 ms, we will decrease the priority number of process P by 1.
So, after 10 ms, the priority of the process P will become 73 and this
process will continue. After a certain period of time, the process P will
become a high priority process when the priority number comes closer to
0 and the process P will get the CPU for its execution. In this way, the
lower priority process also gets the CPU. No doubt the CPU is allocated
after a very long time but since the priority of the process is very low so,
we are not that much concerned about the response time of the process.
The only thing that we are taking care of is starvation.

Livelock:
 There is a variant of deadlock called livelock. This is a situation in which
two or more processes continuously change their state in response to
changes in the other process (es) without doing any useful work. This is
similar to deadlock in that no progress is made but differs in that neither
process is blocked or waiting for anything.
 A human example of livelock would be two people who meet face-to-
face in a corridor and each moves aside to let the other pass, but they end
up swaying from side to side without making any progress because they
always move the same way at the same time.

Ways of Handling Deadlock:


 There are 4 ways of handling deadlocks:
 Deadlock ignorance
 Deadlock prevention
 Deadlock avoidance
 Deadlock detection and recovery
 In order to avoid deadlocks, the system must have additional information
about all processes. In particular, the system must know what resources a
process will or may request in the future.
 Deadlock detection is fairly straightforward, but deadlock recovery
requires either aborting processes or pre-empting resources, neither of
which is an attractive alternative.
 If deadlocks are neither prevented nor detected, then when a deadlock
occurs the system will gradually slow down, as more and more processes

Prof. Debabrata Nath (CSE, NITMAS)


5

become stuck waiting for resources currently held by the deadlock and by
other waiting processes. Unfortunately, this slowdown can be
indistinguishable from a general system slowdown when a real-time
process has heavy computing needs.
1) Deadlock Ignorance:
 If deadlocks only occur once in a year or so, it may be better to simply let
them happen and reboot as necessary than to incur the constant overhead
and system performance penalties associated with deadlock prevention or
detection. This is the approach that both Windows and UNIX take.
2) Deadlock Prevention:
 Deadlocks can be prevented by preventing at least one of the four
required conditions: mutual exclusion, hold and wait, no pre-emption, and
circular wait.
 Mutual exclusion:
 Shared resources such as read-only files do not lead to deadlocks.
 Unfortunately, some resources such as printers and tape drives,
require exclusive access by a single process.
 So, we should not avoid the mutual exclusion.
 Hold and wait: To prevent this condition processes must be prevented
from holding one or more resources while simultaneously waiting for one
or more others. There are several possibilities for this.
 Require that all processes request all resources at one time. This
can be wasteful of system resources if a process needs one resource
early in its execution and doesn't need some other resource until
much later.
 Require that processes holding resources must release them before
requesting new resources, and then re-acquire the released
resources along with the new ones in a single new request. This can
be a problem if a process has partially completed an operation
using a resource and then fails to get it re-allocated after releasing
it.
 Either of the methods described above can lead to starvation if a
process requires one or more popular resources.
 No pre-emption: Pre-emption of process resource allocations can prevent
this condition of deadlocks, when it is possible.
 One approach is that if a process is forced to wait when requesting
a new resource, then all other resources previously held by this

Prof. Debabrata Nath (CSE, NITMAS)


6

process are implicitly released (pre-empted), forcing this process to


re-acquire the old resources along with the new resources in a
single request, similar to the previous discussion.
 Another approach is that when a resource is requested and not
available, then the system looks to see what other processes
currently have those resources and whether are themselves blocked
waiting for some other resource. If such a process is found, then
some of their resources may get pre-empted and added to the list of
resources for which the process is waiting.
 Either of these approaches may be applicable for resources whose
states are easily saved and restored, such as registers and memory,
but are generally not applicable to other devices such as printers
and tape drives.
 Circular wait:
 One way to avoid circular wait is to number all resources, and to
require that processes request resources only in strictly increasing
(or decreasing) order.
 In other words, in order to request resource R j, a process must first
release all Ri such that i <= j (or i >= j).
 One big challenge in this scheme is determining the relative
ordering of the different resources.

3) Deadlock Avoidance:
 It requires that the system has some additional a priori information
available.
 Simplest and most useful model requires that each process declare
the maximum number of resources of each type that it may need.
 The deadlock avoidance algorithm dynamically examines the
resource allocation state to ensure that there can never be a circular
wait condition, i.e. to ensure safe state.
 A resource allocation state is defined by the number of available
and allocated resources, and the maximum requirements of all
processes in the system.
4) Deadlock Detection and Recovery:
 Allow system to enter deadlock state.
 Use detection algorithm to detect the deadlock and provide recovery
scheme.

Prof. Debabrata Nath (CSE, NITMAS)


7

 To use the detection algorithm, we should first understand resource


allocation graph, wait for graph, safety algorithm, safety state and
Banker’s algorithm.
 Detection algorithm can also be used for deadlock avoidance.
 There are three basic approaches to recover from deadlock.
 Manual intervention: For manual intervention, a user may follow
the guidelines stated below to recover from deadlock.
 Priority of the process.
 How long the process has computed, and how much remain
to completion?
 How many and what type of resources is the process
holding? (Are they easy to pre-empt and restore?)
 How many more resources does the process need to
complete?
 How many processes will need to be terminated?
 Whether the process interactive or batch?
 Whether or not the process has made non-restorable changes
to any resource?
 Process termination: Two basic approaches, both of which
recover resources allocated to terminated processes.
 Terminate all processes involved in the deadlock. This
definitely solves the deadlock, but at the expense of
terminating more processes than would be absolutely
necessary.
 Terminate processes one by one until the deadlock is broken.
This is more conservative, but requires doing deadlock
detection after each step. There are many factors that can go
into deciding which processes to terminate next. Here, a user
may follow the same guidelines stated in the mutual
intervention section.
 Resource pre-emption: When pre-empting resources to relieve
deadlock, there are three important issues to be addressed.
 Selecting a victim: Deciding which resources to pre-empt
from which processes involves many of the same decision
criteria outlined above.
 Rollback: Ideally one would like to roll back a pre-empted
process to a safe state prior to the point at which that
resource was originally allocated to the process.
Unfortunately, it can be difficult or impossible to determine
what such a safe state is, and so the only safe rollback is to
roll back all the way back to the beginning, i.e. abort the
process and make it start over.

Prof. Debabrata Nath (CSE, NITMAS)


8

 Starvation: How do you guarantee that a process won't starve


because its resources are constantly being pre-empted? One
option would be to use a priority system, and increase the
priority of a process every time its resources get pre-empted.
Eventually it should get a high enough priority that it won't
be pre-empted any more.

Resource Allocation Graph (RAG):


 The resource allocation graph is the pictorial representation of the state of
a system. As its name suggests, the resource allocation graph is the
complete information about all the processes which are holding some
resources or waiting for other resources.
 It also contains the information about all the instances of all the resources
whether they are available or being used by the processes.
 In Resource allocation graph, the process is represented by a Circle while
the resource is represented by a rectangle.
Vertices in RAG:
 Process vertex: Every process will be represented as a process vertex.
Generally, the process will be represented with a circle.
 Resource vertex: Every resource will be represented as a resource
vertex. It is also two type
 Single-instance type resource: It represents as a box that has one
dot. So the number of dots indicate how many instances are present
of each resource type.
 Multi-instance type resource: It also represents as a box having
many dots.

Edges in RAG:
 Assign Edge: If a resource is already assigned to a process then it is
called assign edge.
 Request Edge: In future if a process wants some resource to complete the
execution the it is called request edge.

Prof. Debabrata Nath (CSE, NITMAS)


9

 So, if a process is using a resource, an arrow is drawn from the resource


node to the process node. If a process is requesting a resource, an arrow
is drawn from the process node to the resource node.

Examples of RAG:
 If there is a cycle in the RAG and each resource in the cycle provides
only one instance, then the processes will be in deadlock.
 So cycle in single-instance resource type is the sufficient condition for
deadlock.
 But in multi-instance resource type, cycle is not the sufficient condition
for deadlock.

Prof. Debabrata Nath (CSE, NITMAS)


10

Wait-for Graph:
 Wait-for graph is a variation of RAG.
 A wait-for graph can be constructed from a resource allocation graph by
eliminating the resources and collapsing the associated edges.
 An arc from Pi to Pj in a wait-for graph indicates that process Pi is waiting
for a resource that process Pj is currently holding.
 Cycles in the wait-for graph indicate deadlocks.
 Wait-for-graph is one of the methods for detecting the deadlock situation.
 This method is suitable for smaller database. In this method a graph is
drawn based on the transaction and their lock on the resource.

Prof. Debabrata Nath (CSE, NITMAS)


11

Example:

Banker’s Algorithm:
 The banker’s algorithm is a resource allocation and deadlock avoidance
algorithm that tests for safety by simulating the allocation for
predetermined maximum possible amounts of all resources, then makes a
“safe state” check to test for possible activities, before deciding whether
the allocation should be allowed to continue or not.
 Banker’s algorithm is named so because it is used in banking system to
check whether a loan can be sanctioned to a person or not. Suppose there
are n number of account holders in a bank and the total sum of their
money is S. If a person applies for a loan, then the bank first subtracts the
loan amount from the total money that the bank has and if it is greater
than S then only the loan is sanctioned. This is maintained to satisfy all
the account holders who come to withdraw their money at a time.
 In other words, the bank would never allocate its money in such a way
that it can no longer satisfy the needs of all its customers. The bank
would try to be in safe state always.

Characteristics:
 If a process demands the resources, then it has to wait.
 It should know the maximum resource required of all the processes in
advance.
 Resources are maintained that fulfill the needs of at least one process.
 There are limited resources in the system.
 If a process gets all the needed resources, then it must return the resources
within a restricted period.

Prof. Debabrata Nath (CSE, NITMAS)


12

 Disadvantages:
 During processing, it does not permit a process to change its
maximum need.
 All the processes should know in advance about their maximum
required resources.
 It permits the requests to be provided in constrained time.

Data Structures:
 Available: It is an array of length m. It represents the number of
available resources of each type. If Available[j] = k, then there
are k instances available, of resource type R(j).
 Max: It is an n x m matrix which represents the maximum number of
instances of each resource that a process can request. If Max[i][j] = k,
then the process P(i) can request at most k instances of resource
type R(j).
 Allocation: It is an n x m matrix which represents the number of
resources of each type currently allocated to each process.
If Allocation[i][j] = k, then process P(i) is currently allocated k instances
of resource type R(j).
 Need: It is an n x m matrix which indicates the remaining resource
needs of each process. If Need[i][j] = k, then process P(i) may
need k more instances of resource type R(j) to complete its task.
 Need[i][j] = Max[i][j] – Allocation [i][j].

Resource Request Algorithm:


 This algorithm describes the behavior of the system when a process
Pi makes a resource request in the form of a request matrix.
 Step 1. If number of requested instances of each resource type is
less than the need (which was declared previously by the process),
go to step 2.
 Step 2. If number of requested instances of each resource type is
less than the available resources of each type, go to step 3. Else, the
process has to wait because sufficient resources are not available
yet.
 Step 3. The resources have been allocated. Accordingly update the
following matrices as follow:
Available[j] = Available[j] – Request[i][j]
Allocation[i][j] = Allocation[i][j] + Request[i][j]
Need[i][j] = Need[i][j] – Request[i][j]

Prof. Debabrata Nath (CSE, NITMAS)


13

 This step is done because the system needs to assume that resources
have been allocated to the process Pi. So there will be less resources
available after allocation. The number of allocated instances will
increase for Pi. The need of the resources of Pi will reduce. That's what
is represented by the above three operations.
 After completing the above three steps, check if the system is in safe
state by applying the safety algorithm. If it is in safe state, proceed to
allocate the requested resources. Else, the process has to wait longer
without getting the requested resources.

Safety Algorithm:
 This algorithm describes whether the need of all the processes can
be fulfilled or not by the system.
 Step 1: Let Work and Finish be two vectors of length m (number
of resource types) and n (number of processes), respectively.
Initially, Work[j] = Available[j] and
Finish[i] = false for i = 0, 1, ..., n – 1.
This means, initially no process has finished and the number of
available resources is represented by the Available array.
 Step 2: Find an index i (Process Pi) such that
Finish[i] == false and Need[i][j] <= Work[j]
If no such i present, then proceed to step 4.
This means, we need to find an unfinished process whose need can
be satisfied by the available resources.
 Step 3: Perform and go to step 2.
Work[j] = Work[j] + Allocation[i][j]
Finish[i] = true
This means, the resources are allocated to Pi so that Pi can
complete its task. After completing its task Pi releases all its
previously allocated resources which are then added to the
currently available resources and Pi is marked as finished. Then
the loop is repeated to check the same for all other processes.
 Step 4: If Finish[i] == true for all i, then the system is in a safe
state which indicates that all the processes are finished.

Numerical Example:
 Consider the following system that contains five processes P0, P1, P2,
P3, P4 and three resource types A, B and C. The allocation, max and
available matrices are given below. Here, A has 10, B has 5 and C has 7
instances.

Prof. Debabrata Nath (CSE, NITMAS)


14

Process Allocation Max Available


A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
 Q1: Determine whether the system is safe or not.
 Q2: If P0 raises the resource request (1, 3, 0), will the system
accept this request immediately?

Answer of Q1:

 Apply only safety algorithm.


 Need[i][j] = Max[i][j] – Allocation[i][j]
Need for P0: (7, 5, 3) – (0, 1, 0) = 7, 4, 3
Need for P1: (3, 2, 2) – (2, 0, 0) = 1, 2, 2
Need for P2: (9, 0, 2) – (3, 0, 2) = 6, 0, 0
Need for P3: (2, 2, 2) – (2, 1, 1) = 0, 1, 1
Need for P4: (4, 3, 3) – (0, 0, 2) = 4, 3, 1
 So, the need matrix becomes

Process Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
 Available resources of type A, B and C are 3, 3 and 2 respectively.
 Here, j denotes the number of resource types and i denotes the number
of processes. In this problem, j = 3 and i = 5.
 Work[j] = Available[j] = [3, 3, 2] for j = 0, 1, 2
Finish[i] = 0 for i = 0, 1, …, 4
 For P0: Need[0][j] <= Work[j] i.e. [7, 4, 3] <= [3, 3, 2] is false.
For P1: Need[1][j] <= Work[j] i.e. [1, 2, 2] <= [3, 3, 2] is true.
 Work[j] = Work[j] + Allocation[1][j]
i.e. Work[j] = [3, 3, 2] + [2, 0, 0] = [5, 3, 2]
 Finish[1] = true
 For P2: Need[2][j] <= Work[j] i.e. [6, 0, 0] <= [5, 3, 2] is false.
 For P3: Need[3][j] <= Work[j] i.e. [0, 1, 1] <= [5, 3, 2] is true.
 Work[j] = Work[j] + Allocation[3][j]
i.e. Work[j] = [5, 3, 2] + [2, 1, 1] = [7, 4, 3]
 Finish[3] = true

Prof. Debabrata Nath (CSE, NITMAS)


15

 For P4: Need[4][j] <= Work[j] i.e. [4, 3, 1] <= [7, 4, 3] is true.
 Work[j] = Work[j] + Allocation[4][j]
i.e. Work[j] = [7, 4, 3] + [0, 0, 2] = [7, 4, 5]
 Finish[4] = true
 For P0: Need[0][j] <= Work[j] i.e. [7, 4, 3] <= [7, 4, 5] is true.
 Work[j] = Work[j] + Allocation[0][j]
i.e. Work[j] = [7, 4, 5] + [0, 1, 0] = [7, 5, 5]
 Finish[0] = true
 For P2: Need[2][j] <= Work[j] i.e. [6, 0, 0] <= [7, 5, 5] is true.
 Work[j] = Work[j] + Allocation[2][j]
i.e. Work[j] = [7, 5, 5] + [3, 0, 2] = [10, 5, 7]
 Finish[2] = true
 All the processes are completed. So the system is in safe state and the
safe sequence is <P1, P3, P4, P0, P2>.

Answer of Q2:

 P0 raises the resource request (1, 3, 0).


 First apply resource request algorithm.
 Need[i][j] = Max[i][j] – Allocation[i][j]
Need for P0: (7, 5, 3) – (0, 1, 0) = 7, 4, 3
Need for P1: (3, 2, 2) – (2, 0, 0) = 1, 2, 2
Need for P2: (9, 0, 2) – (3, 0, 2) = 6, 0, 0
Need for P3: (2, 2, 2) – (2, 1, 1) = 0, 1, 1
Need for P4: (4, 3, 3) – (0, 0, 2) = 4, 3, 1
 So, the need matrix becomes

Process Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
 For P0: Request[0][j] <= Need[0][j] i.e. [1, 3, 0] <= [7, 4, 3] is true.
 Request[0][j] <= Available[j] i.e. [1, 3, 0] <= [3, 3, 2] is true.
 Available[j] = Available[j] – Request[0][j]
i.e. Available[j] = [3, 3, 2] – [1, 3, 0] = [2, 0, 2]
 Allocation[0][j] = Allocation[0][j] + Request[0][j]
i.e. Allocation[0][j] = [0, 1, 0] + [1, 3, 0] = [1, 4, 0]
 Need[0][j] = Need[0][j] – Request[0][j]
i.e. Need[0][j] = [7, 4, 3] – [1, 3, 0] = [6, 1, 3]
 Now, apply safety algorithm.

Prof. Debabrata Nath (CSE, NITMAS)


16

 The need matrix becomes


Process Need
A B C
P0 6 1 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1

 Work[j] = Available[j] = [2, 0, 2]


Finish[i] = 0 for i = 0, 1, …, 4
 For P0: Need[0][j] <= Work[j] i.e. [6, 1, 3] <= [2, 0, 2] is false.
 For P1: Need[1][j] <= Work[j] i.e. [1, 2, 2] <= [2, 0, 2] is false.
 For P2: Need[2][j] <= Work[j] i.e. [6, 0, 0] <= [2, 0, 2] is false.
 For P3: Need[3][j] <= Work[j] i.e. [0, 1, 1] <= [2, 0, 2] is false.
 For P4: Need[4][j] <= Work[j] i.e. [4, 3, 1] <= [2, 0, 2] is false.
 None of the processes will complete. So, it’s a deadlock situation.
 Since the system is not safe, the request of P0 will be discarded for
which P0 should wait longer.

Exercise:
 Consider the following system that contains five processes P0, P1, P2,
P3, P4 and four resource types A, B, C and D. At T0 the system has the
following state.

Process Allocation Max Available


A B C D A B C D A B C D
P0 0 1 1 0 0 2 1 0 1 5 2 0
P1 1 2 3 1 1 6 5 2
P2 1 3 6 5 2 3 6 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
 Q1: Determine whether the system is safe or not.
 Q2: If P1 raises the resource request (1, 1, 0, 0), will the system
grant this request immediately? Run the safety algorithm on the
request as necessary.
 Q3: If P1 raises the resource request (0, 2, 0, 1), will the system
grant this request immediately? Run the safety algorithm on the
request as necessary.
 Q4: If P1 raises the resource request (0, 2, 1, 0), will the system
grant this request immediately? Run the safety algorithm on the
request as necessary.

Prof. Debabrata Nath (CSE, NITMAS)

You might also like