Deadlocks: Coffman Conditions
Deadlocks: Coffman Conditions
A deadlock happens in an operating system when two or more processes need some resource
to complete their execution that is held by the other process.
In the above diagram, the process 1 has resource 1 and needs to acquire resource 2. Similarly
process 2 has resource 2 and needs to acquire resource 1. Process 1 and process 2 are in
deadlock as each of them needs the other’s resources to complete their execution but neither of
them is willing to relinquish their resources.
Coffman Conditions
A deadlock occurs if the four Coffman conditions hold true. But these conditions are not
mutually exclusive.
● Mutual Exclusion
There should be a resource that can only be held by one process at a time. In the
1
diagram below, there is a single instance of Resource 1 and it is held by Process 1 only.
● No Preemption
A resource cannot be preempted from a process by force. A process can only release a
resource voluntarily. In the diagram below, Process 2 cannot preempt Resource 1 from
Process 1. It will only be released when Process 1 relinquishes it voluntarily after its
execution is complete.
● Circular Wait
A process is waiting for the resource held by the second process, which is waiting for the
resource held by the third process and so on, till the last process is waiting for a
resource held by the first process. This forms a circular chain. For example: Process 1 is
allocated Resource2 and it is requesting Resource 1. Similarly, Process 2 is allocated
2
Resource 1 and it is requesting Resource 2. This forms a circular wait loop.
It also contains the information about all the instances of all the resources whether they are
available or being used by the processes.
In the Resource allocation graph, the process is represented by a Circle while the Resource is
represented by a rectangle. Let's see the types of vertices and edges in detail.
3
Vertices are mainly of two types, Resource and process. Each of them will be represented by a
different shape. Circle represents process while rectangle represents resource.
A resource can have more than one instance. Each instance will be represented by a dot inside
the rectangle.
Edges in RAG are also of two types, one represents assignment and other represents the wait
of a process for a resource. The above image shows each of them.
A resource is shown as assigned to a process if the tail of the arrow is attached to an instance
to the resource and the head is attached to a process.
A process is shown as waiting for a resource if the tail of an arrow is attached to the process
while the head is pointing towards the resource.
Example
4
Let's consider 3 processes P1, P2 and P3, and two types of resources R1 and R2. The
resources are having 1 instance each.
According to the graph, R1 is being used by P1, P2 is holding R2 and waiting for R1, P3 is
waiting for R1 as well as R2.
The graph is deadlock free since no cycle is being formed in the graph.
There is always a tradeoff between Correctness and performance. The operating systems like
Windows and Linux mainly focus upon performance. However, the performance of the system
decreases if it uses a deadlock handling mechanism all the time. If a deadlock happens 1 out of
100 times then it is completely unnecessary to use the deadlock handling mechanism all the
time.
In these types of systems, the user has to simply restart the computer in the case of deadlock.
Windows and Linux are mainly using this approach.
2. Deadlock prevention
5
Deadlock happens only when Mutual Exclusion, hold and wait, No preemption and circular wait
holds simultaneously. If it is possible to violate one of the four conditions at any time then the
deadlock can never occur in the system.
The idea behind the approach is very simple that we have to fail one of the four conditions but
there can be a big argument on its physical implementation in the system.
3. Deadlock avoidance
In deadlock avoidance, the operating system checks whether the system is in safe state or in
unsafe state at every step which the operating system performs. The process continues until the
system is in a safe state. Once the system moves to an unsafe state, the OS has to backtrack
one step.
In simple words, The OS reviews each allocation so that the allocation doesn't cause the
deadlock in the system.
Deadlock Prevention
If we simulate a deadlock with a table which is standing on its four legs then we can also
simulate four legs with the four conditions which when occurs simultaneously, cause the
deadlock.
However, if we break one of the legs of the table then the table will definitely fall. The same
happens with deadlock, if we are able to violate one of the four necessary conditions and don't
let them occur together then we can prevent the deadlock.
1. Mutual Exclusion
Mutual section from the resource point of view is the fact that a resource can never be used by
more than one process simultaneously which is fair enough but that is the main reason behind
6
the deadlock. If a resource could have been used by more than one process at the same time
then the process would have never been waiting for any resource.
However, if we can be able to violate resources behaving in the mutually exclusive manner then
the deadlock can be prevented.
Spooling
For a device like a printer, spooling can work. There is a memory associated with the printer
which stores jobs from each of the processes into it. Later, Printer collects all the jobs and print
each one of them according to FCFS. By using this mechanism, the process doesn't have to
wait for the printer and it can continue whatever it was doing. Later, it collects the output when it
is produced.
Spooling can be an effective approach to violate mutual exclusion but it suffers from two kinds
of problems.
We cannot force a resource to be used by more than one process at the same time since it will
not be fair enough and some serious problems may arise in the performance. Therefore, we
cannot violate mutual exclusion for a process practically.
7
Hold and wait condition lies when a process holds a resource and waits for some other resource
to complete its task. Deadlock occurs because there can be more than one process which are
holding one resource and waiting for other in the cyclic order.
However, we have to find out some mechanism by which a process either doesn't hold any
resource or doesn't wait. That means, a process must be assigned all the necessary resources
before the execution starts. A process must not wait for any resource once the execution has
been started.
!(Hold and wait) = !hold or !wait (negation of hold and wait is, either you don't hold or you
don't wait)
This can be implemented practically if a process declares all the resources initially. However,
this sounds very practical but can't be done in the computer system because a process can't
determine necessary resources initially.
Process is the set of instructions which are executed by the CPU. Each of the instructions may
demand multiple resources at multiple times. The need cannot be fixed by the OS.
3. No Preemption
Deadlock arises due to the fact that a process can't be stopped once it starts. However, if we
take the resource away from the process which is causing deadlock then we can prevent
deadlock.
This is not a good approach at all since if we take a resource away which is being used by the
process then all the work which it has done till now can become inconsistent.
Consider a printer that is being used by any process. If we take the printer away from that
process and assign it to some other process then all the data which has been printed can
become inconsistent and ineffective and also the fact that the process can't start printing again
from where it has left which causes performance inefficiency.
4. Circular Wait
8
To violate circular wait, we can assign a priority number to each of the resource. A process can't
request for a lesser priority resource. This ensures that not a single process can request a
resource which is being utilized by some other process and no cycle will be formed.
Among all the methods, violating Circular wait is the only approach that can be implemented
practically.
Deadlock avoidance
In deadlock avoidance, the request for any resource will be granted if the resulting state of the
system doesn't cause deadlock in the system. The state of the system will continuously be
checked for safe and unsafe states.
In order to avoid deadlocks, the process must tell OS, the maximum number of resources a
process can request to complete its execution.
The simplest and most useful approach states that the process should declare the maximum
number of resources of each type it may ever need. The Deadlock avoidance algorithm
examines the resource allocations so that there can never be a circular wait condition.
The resource allocation state of a system can be defined by the instances of available and
allocated resources, and the maximum instance of the resources demanded by the processes.
9
Resources Assigned
A 3 0 2 2
B 0 0 1 1
C 1 1 1 0
D 2 1 4 0
A 1 1 0 0
B 0 1 1 2
C 1 2 1 0
D 2 1 1 2
1. E = (7 6 8 4)
2. P = (6 2 8 3)
3. A = (1 4 0 1)
Above tables and vectors E, P and A describe the resource allocation state of a system. There
are 4 processes and 4 types of the resources in a system. Table 1 shows the instances of each
resource assigned to each process.
10
Table 2 shows the instances of the resources, each process still needs. Vector E is the
representation of total instances of each resource in the system.
Vector P represents the instances of resources that have been assigned to processes. Vector A
represents the number of resources that are not in use.
A state of the system is called safe if the system can allocate all the resources requested by all
the processes without entering into deadlock.
If the system cannot fulfill the request of all processes then the state of the system is called
unsafe.
The key of Deadlock avoidance approach is when the request is made for resources then the
request must only be approved in the case if the resulting state is also a safe state.
This algorithm is used to test for safely simulating the allocation for determining the maximum
amount available for all resources. It also checks for all the possible activities before
determining whether allocation should be continued or not.
For example, there are X number of account holders of a specific bank, and the total amount of
money of their accounts is G.
When the bank processes a car loan, the software system subtracts the amount of loan granted
for purchasing a car from the total money ( G+ Fixed deposit + Monthly Income Scheme + Gold,
etc.) that the bank has.
It also checks that the difference is more than or not G. It only processes the car loan when the
bank has sufficient money even if all account holders withdraw the money G simultaneously.
Available
11
[I: Y] indicate which resource is available.
Max
Allocation
Need
● 4 Pen drives
● 2 Printers
● 4 Scanners
● 3 Hard disks
Here, we have created a vector representing total resources: Available = (5, 2, 4, 3).
Assume there are four processes. The available resources are already allocated as per the
matrix table below.
2 0 1 1
P
Q 0 1 0 0
R 1 0 1 1
S 1 1 0 1
Total 4 2 2 3
12
Here, the allocated resources is the total of these columns:
We also create a Matrix to display the number of each resource required for all the processes.
This matrix is called Need=(3,0,2,2)
1 1 0 0
P
Q 0 1 1 2
R 2 1 0 0
S 0 0 1 0
The available vector will be :
Available=Available- Allocated
= (5, 2, 4, 3) -(4, 2, 2, 3)
=(1, 0, 2, 0)
Resource request algorithm enables you to represent the system behavior when a specific
process makes a resource request.
Step 1) When a total requested instance of all resources is lesser than the process, move to
step 2.
Step 2) When a requested instance of each and every resource type is lesser compared to the
available resources of each type, it will be processed to the next step. Otherwise, the process
requires waiting because of the unavailability of sufficient resources.
13
Allocation(x) = Allocation(x) + Request(x)
This final step is performed because the system needs to assume that resources have been
allocated. So that there should be less resources available after allocation.
● Keep many resources that satisfy the requirement of at least one client
● Whenever a process gets all its resources, it needs to return them in a restricted period.
● When a process requests a resource, it needs to wait
● The system has a limited number of resources
● Advance feature for max resource allocation
● Does not allow the process to change its Maximum need while processing
● It allows all requests to be granted in restricted time, but one year is a fixed period for
that.
● All processes must know and state their maximum resource needs in advance.
The main task of the OS is detecting the deadlocks. The OS can detect the deadlocks with the
help of a Resource allocation graph.
14
In single instanced resource types, if a cycle is being formed in the system then there will
definitely be a deadlock. On the other hand, in multiple instanced resource type graph, detecting
a cycle is not just enough. We have to apply the safety algorithm on the system by converting
the resource allocation graph into the allocation matrix and request matrix.
In order to recover the system from deadlocks, either OS considers resources or processes.
For Resource
We can snatch one of the resources from the owner of the resource (process) and give it to the
other process with the expectation that it will complete the execution and will release this
resource sooner. Well, choosing a resource which will be snatched is going to be a bit difficult.
System passes through various states to get into the deadlock state. The operating system can
rollback the system to the previous safe state. For this purpose, the OS needs to implement
check pointing at every state.
The moment we get into a deadlock, we will rollback all the allocations to get into the previous
safe state.
For Process
15
Kill a process
Killing a process can solve our problem but the bigger concern is to decide which process to kill.
Generally, the Operating system kills a process which has done the least amount of work until
now.
This is not a suggestible approach but can be implemented if the problem becomes very
serious. Killing all processes will lead to inefficiency in the system because all the processes will
execute again from starting.
In Case of Resource allocation graph with multi-instanced resource types, Cycle is a necessary
condition of deadlock but not the sufficient condition.
The following example contains three processes P1, P2, P3 and three resources R2, R2, R3. All
the resources are having single instances each.
16
If we analyze the graph then we can find out that there is a cycle formed in the graph since the
system is satisfying all the four conditions of deadlock.
Allocation Matrix
Allocation matrix can be formed by using the Resource allocation graph of a system. In the
Allocation matrix, an entry will be made for each of the resources assigned. For Example, in the
following matrix, en entry is being made in front of P1 and below R3 since R3 is assigned to P1.
Process R1 R2 R3
P1 0 0 1
P2 1 0 0
P3 0 1 0
Request Matrix
In request matrix, an entry will be made for each of the resources requested. As in the following
example, P1 needs R1 therefore an entry is being made in front of P1 and below R1.
Process R1 R2 R3
P1 1 0 0
P2 0 1 0
P3 0 0 1
17
Avial = (0,0,0)
Neither we are having any resource available in the system nor a process going to release.
Each of the processes needs at least a single resource to complete therefore they will
continuously be holding each one of them.
We cannot fulfill the demand of at least one process using the available resources therefore the
system is deadlocked as determined earlier when we detected a cycle in the graph.
18