OS Deadlock Notes Unit 3
OS Deadlock Notes Unit 3
OS Deadlock Notes Unit 3
A 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.
Examples Of Deadlock
1. The system has 2 tape drives. P1 and P2 each hold one tape drive and each needs
another one.
2. Semaphores A and B, initialized to 1, P0, and P1 are in deadlock as follows:
P0 executes wait(A) and preempts.
P1 executes wait(B).
Now P0 and P1 enter in deadlock.
P0 P1
wait(A); wait(B)
wait(B); wait(A)
3. Assume the space is available for allocation of 200K bytes, and the following sequence of
events occurs.
P0 P1
CHARACTERISTICS OF DEADLOCK
1. Mutual Exclusion: Processes contend for exclusive control over resources. Only one
process can use a resource at a time.
2. Hold and Wait: Processes hold resources already allocated to them while waiting for
additional resources. A process may request additional resources while holding onto its
current resources.
3. No Preemption: Resources cannot be forcibly taken away from a process; a process must
voluntarily release resources it holds.
4. Circular Wait: A circular chain of processes exists, where each process holds a resource that
the next process in the chain needs. This creates a cycle of dependencies.
RESOURCE ALLOCATION GRAPH
A resource allocation graphs shows which resource is held by which process and
which process is waiting for a resource of a specific kind. It is amazing and
straight – forward tool to outline how interacting processes can deadlock.
Therefore, resource allocation graph describe what the condition of the system
as far as process and resources are concern like what number of resources are
allocated and what is the request of each process.
Everything can be represented in terms of graph. One of the benefit of having
a graph is, sometimes it is conveivable to see a deadlock straight forward by
utilizing RAG and however you probably won’t realize that by taking a glance at
the table.
RAG also contains vertices and edges. In RAG vertices are two types :
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.
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.
Detection and Recovery: Another approach to dealing with deadlocks is to
detect and recover from them when they occur. This can involve killing one or
more of the processes involved in the deadlock or releasing some of the
resources they hold.
DEADLOCK AVOIDANCE
A deadlock avoidance policy grants a resource request only if it can establish
that granting the request cannot lead to a deadlock either immediately or in
the future.
The kernal lacks detailed knowledge about future behavior of processes, so it
cannot accurately predict deadlocks. To facilitate deadlock avoidance under
these conditions, it uses the following conservative approach: Each process
declares the maximum number of resource units of each class that it may
require.
The kernal permits a process to request these resource units in stages- i.e. a
few resource units at a time- subject to the maximum number declared by it
and uses a worst case analysis technique to check for the possibility of future
deadlocks.
DEADLOCK DETECTION
1. If resources have a single instance –
In this case for Deadlock detection, we can run an algorithm to check for the cycle in
the Resource Allocation Graph. The presence of a cycle in the graph is a sufficient
condition for deadlock.
In the above diagram, resource 1 and resource 2 have single instances. There is a
cycle R1 → P1 → R2 → P2. So, Deadlock is Confirmed.
DEADLOCK RECOVERY
A traditional operating system such as Windows doesn’t deal with deadlock
recovery as it is a time and space-consuming process. Real-time operating systems
use Deadlock recovery.
BANKER ALGORITHM
The Banker's algorithm is a resource allocation and deadlock avoidance
algorithm used in operating systems. It was developed by Edsger Dijkstra and
named after the concept of a banker who safely lends out money only if it
ensures that the borrower can repay.
Similarly, the Banker's algorithm ensures that the system does not enter an
unsafe state where deadlock might occur.
b. Find a process whose maximum resource needs can be satisfied with the available
resources.
c. Simulate the allocation of resources to that process and update the work vector.
d. Repeat the process until all processes can complete, or it is determined that no
safe sequence exists.