deadlock theory
deadlock theory
deadlock theory
DEADLOCK
What is Deadlock? List the conditions that lead to
deadlock. How Deadlock can be prevented?
OR
What is Deadlock? Explain Deadlock prevention &
Avoidance
OR
What is Deadlock? List the conditions that lead to
deadlock. How Deadlock can be prevented?
Deadlock: A Deadlock is a situation where each of the computer process waits for a resource which
is being assigned to some another process. In this situation, none of the process gets executed since
the resource it needs, is held by some other process which is also waiting for some other resource to
be released.
Let us assume that there are three processes P1, P2 and P3. There are three different resources R1, R2
and R3. R1 is assigned to P1, R2 is assigned to P2 and R3 is assigned to P3.
After some time, P1 demands for R1 which is being used by P2. P1 halts its execution since it can't
complete without R2. P2 also demands for R3 which is being used by P3. P2 also stops its execution
because it can't continue without R3. P3 also demands for R1 which is being used by P1 therefore P3
also stops its execution.
n this scenario, a cycle is being formed among the three processes. None of the process is progressing
and they are all waiting. The computer becomes unresponsive since all the processes got blocked.
conditions that lead to deadlock
Let’s see that both processes begin execution at the same time.
Process A obtains Resource 1.
Process B obtains Resource 2.
We have a deadlock because neither process can release the resource it is holding until it completes
its task, and neither can proceed without the resource the other process is holding. Both processes
are effectively “deadlocked,” unable to move forward.
Necessary Conditions for the Occurrence of a Deadlock
Let’s explain all four conditions related to deadlock in the context of the scenario with two processes
and two resources:
Mutual Exclusion
This condition requires that at least one resource be held in a non-shareable mode, which means that
only one process can use the resource at any given time. Both Resource 1 and Resource 2 are non-
shareable in our scenario, and only one process can have exclusive access to each resource at any
given time.
As an example:
Both processes hold one resource while waiting for the other, satisfying the hold and wait condition.
No Preemption
Preemption is the act of taking a resource from a process before it has finished its task. According to
the no preemption condition, resources cannot be taken forcibly from a process a process can only
release resources voluntarily after completing its task. In our scenario, neither Process A nor Process
B can be forced to release the resources in their possession. The processes can only release
resources voluntarily.
Circular Wait
This condition implies that circular processes must exist, with each process waiting for a resource
held by the next process in the chain. In our scenario, Process A is waiting for Resource 2, which is
being held by Process B.
Deadlock prevention
Eliminate Mutual Exclusion: It is not possible to dis-satisfy the mutual exclusion because some
resources, such as the tape drive and printer, are inherently non-shareable. Let’s take a practical
example to understand this issue. Jack and Jones share a bowl of soup. Both of them want to
drink the soup from the same bowl and use a single spoon simultaneously, which is not
feasible.
Eliminate Hold and wait: Allocate all required resources to the process before the start of its
execution, this way hold and wait condition is eliminated but it will lead to low device utilization. for
example, if a process requires a printer at a later time and we have allocated a printer before the start
of its execution printer will remain blocked till it has completed its execution. The process will make a
new request for resources after releasing the current set of resources. This solution may lead to
starvation.
Eliminate No Preemption : Preempt resources from the process when resources are required
by other high-priority processes.
Eliminate Circular Wait : Each resource will be assigned a numerical number. A process can
request the resources to increase/decrease. order of numbering. For Example, if the P1 process is
allocated R5 resources, now next time if P1 asks for R4, R3 lesser than R5 such a request will not be
granted, only a request for resources more than R5 will be granted.
Deadlock Avoidance
For example, let two processes P1, P2, and two resources R1, R2:
Here, P1 uses R1, and it’ll use R2 in the future, denoted as a dotted line. At the current time, it’s free.
P2 is requesting R2, but OS will not allocate that, as it’ll be needed for P1 later. So it’ll avoid the
formation of a circle. Thus, it avoids the probability of deadlock occurrence. But from the above
solution, R2 will not be in use. Hence, resource utilization will be less.
Banker Algorithm
When resources have multiple instances, we use the Banker’s algorithm. It is known as the Banker’s
algorithm because banks follow it to manage the resources in such a manner so that they’ll never be
out of resources and avoid getting into an unsafe state. Banks use this algorithm before allocating or
sanctioning any loan in the banking system.
In this algorithm, we need some predetermined data, such as the maximum required resources from
each process to complete the life cycle and the total available resources in the OS. From that data,
it’ll make a safe sequence. If the OS runs all the processes in that sequence, it’ll avoid the occurrence
of deadlock.
This algorithm maintains a matrix of total available resources, maximum required resources for each
process, total allocated resources to each process, and currently required resources.
In order to detect deadlocks, the system must monitor the state of all processes and resources in the
system. This involves examining the current resource allocation state and predicting the future
resource allocation state. Various methods can be used to detect deadlocks, such as the wait-for
graph (WFG) and resource allocation graph (RAG) methods.
Deadlock recovery
Deadlock recovery is a critical process that is initiated after a deadlock has been detected in a computer
system. This complex process involves a set of actions and procedures that are undertaken to resolve
the deadlock by breaking the cycle of resource dependency between the processes involved in the
deadlock. There are four primary methods of deadlock recovery: process termination, resource
pre-emption, priority inversion, and rollback.
Process Termination
Process termination is a simple method for resolving deadlocks. In this method, the operating system
identifies the processes involved in the deadlock and terminates one or more processes. This frees up
the resources held by the terminated processes, which can be used by the remaining processes to
continue their execution. However, this method has some drawbacks, such as loss of data, abrupt
termination of processes, and inconsistency in the system.
Resource Pre-emption
Resource pre-emption is a more complex method for resolving deadlocks. In this method, the operating
system identifies the resources involved in the deadlock and selects one or more resources to be pre-
empted. The resources are then taken away from the process holding them and allocated to the waiting
processes. The pre-empted process is suspended until the required resources become available again.
This method can cause delays in the execution of the pre-empted process and can result in a
suboptimal allocation of resources.
Priority Inversion
Priority inversion is a method for resolving deadlocks in real-time systems. In this method, the priority
of the processes is changed to avoid deadlock situations. The process holding the required resources
is given a higher priority, and the process waiting for the resources is given a lower priority. This method
can lead to the inversion of priorities, which can cause performance issues and degrade the
performance of the system. Additionally, this method can also lead to starvation of lower priority
processes, as higher priority processes can keep preempting the resources.
Rollback
Rollback is a method for resolving deadlocks that is commonly used in database systems. In this
method, the system rolls back the transactions of the involved processes to a previous state where they
were not deadlocked. This method requires that the system maintains a log of all the transactions and
the state of the system at different points in time. The system can then roll back the transactions to the
previous state and re-execute them. This method can cause significant delays in the execution of the
transactions and can result in a loss of data.
The algorithm is used to prevent deadlocks that can occur when multiple processes are competing for
a finite set of resources. The resources can be of different types such as memory, CPU cycles, or I/O
devices. It works by first analysing the current state of the system and determining if granting a
resource request from a process will result in a safe state. A state is considered safe if there is at
least one sequence of resource allocations that can satisfy all processes without causing a deadlock.
The Banker's algorithm assumes that each process declares its maximum resource requirements
upfront. Based on this information, the algorithm allocates resources to each Resource-Allocation
Graph process such that the total number of allocated resources never exceeds the total number of
available resources. The algorithm does not grant access to resources that could potentially lead to a
deadlock situation. The Banker's algorithm uses a matrix called the "allocation matrix" to keep track of
the resources allocated to each process, and a "request matrix" to keep track of the resources
requested by each process. It also uses a "need matrix" to represent the resources that each process
still needs to complete its execution.
To determine if a request can be granted, the algorithm checks if there is enough available resources
to satisfy the request, and then checks if granting the request will still result in a safe state. If the
request can be granted safely, the algorithm grants the resources and updates the allocation matrix,
request matrix, and need matrix accordingly. If the request cannot be granted safely, the process
must wait until sufficient resources become available.
Following are the important data structures terms applied in the banker's algorithm as follows:
Suppose n is the number of processes, and m is the number of each type of resource used in a
computer system.
Available: It is an array of length 'm' that defines each type of resource available in the
system. When Available[j] = K, means that 'K' instances of Resources type R[j] are available in
the system.
Max: It is a [n x m] matrix that indicates each process P[i] can store the maximum number of
resources R[j] (each type) in a system.
Finish: It is the vector of the order m. It includes a Boolean value (true/false) indicating
whether the process has been allocated to the requested resources, and all resources have
been released after finishing its task.
The Banker's Algorithm is the combination of the safety algorithm and the
resource request algorithm to control the processes and avoid deadlock in a
system:
Safety Algorithm
It is a safety algorithm used to check whether or not a system is in a safe state or follows the safe
sequence in a banker's algorithm:
1. There are two vectors Wok and Finish of length m and n in a safety algorithm.
2. Check the availability status for each type of resources [i], such as:
Finish[i] = true
Go to step 2 to check the status of resource availability for the next process.
4. If Finish[i] == true; it means that the system is safe for all processes.
Let create a resource request array R[i] for each process P[i]. If the Resource Requesti [j] equal to 'K',
which means the process P[i] requires 'k' instances of Resources type R[j] in the system.
1. When the number of requested resources of each type is less than the Need resources, go to step
2 and if the condition fails, which means that the process P[i] exceeds its maximum claim for the
resource. As the expression suggests:
2. And when the number of requested resources of each type is less than the available resource for
each process, go to step (3). As the expression suggests:
if Request(i) <= Available
Else Process P[i] must wait for the resource since it is not available for use.
When the resource allocation state is safe, its resources are allocated to the process P(i). And if the
new state is unsafe, the Process P (i) has to wait for each type of Request R(i) and restore the old
resource-allocation state.
It also provides information on all instances of all resources, whether available or in use by
processes. The process is represented by a circle in the Resource Allocation Graph, whereas the
resource is represented using a rectangle. Let’s take a closer look at the various types of vertices and
edges.
There are two sorts of vertices: resource and process. A distinct form will be used to symbolise each
of them. The circle denotes the process, whereas the rectangle denotes the resource.
There can be several instances of a resource.
In RAG, there are two sorts of edges: One represents assignment, and the other represents a
process waiting for a resource. Each of them is depicted in the image above.
In case the tail of the arrow is connected to a resource instance and the head is connected to a
process, the resource is shown to be assigned to that process. If the tail of the arrow is connected to
the process and the head is pointed towards the resource, the process is represented as waiting for a
resource.
Example
Consider three processes, P1, P2, and P3, as well as two resource types, R1 and R2. Each resource
has a single instance. According to the graph, P1 is using R1, P2 is holding R2 while waiting for R1,
and P3 is waiting for both R1 and R2.
If there is a cycle in the Resource Allocation Graph and each resource in the cycle provides
only one instance, then the processes will be in deadlock. For example, if process P1 holds
resource R1, process P2 holds resource R2 and process P1 is waiting for R2 and process
P2 is waiting for R1, then process P1 and process P2 will be in deadlock.
Here’s another example, that shows Processes P1 and P2 acquiring resources R1 and R2
while process P3 is waiting to acquire both resources. In this example, there is no deadlock
because there is no circular dependency.
So cycle in single-instance resource type is the sufficient condition for deadlock.
From the above example, it is not possible to say the RAG is in a safe state or in an unsafe
state. So to see the state of this RAG, let’s construct the allocation matrix and request
matrix.