Deadlock occurs when a set of processes are blocked because each process is holding a resource and waiting for another resource held by another process. There are four necessary conditions for deadlock: mutual exclusion, hold and wait, no preemption, and circular wait. There are four approaches to handling deadlock: ignore, avoid, prevent, and detect and recover. Deadlock avoidance algorithms aim to keep the system in a safe state by considering available and allocated resources. Banker's algorithm is a deadlock avoidance technique that maintains a safe sequence of pending processes.
What is deadlock and how to prevent, detect, and recover from it
1.
2. What is deadlock
• 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.
P
1
P
2
R1 R2
3. Necessary Condition for Deadlocks
Mutual Exclusion: If two process cannot share same resource
at same time.
Hold and Wait: A process wait for some resources while
holding any another resource at the same time.
No pre-emption: The process which once scheduled will be
executed till completion.
Circular wait: All the processes must be waiting for the
resource in a cyclic manner.
R1 R2 R3
P
1
P
2
P
3
4. Method of Handling Deadlock
There are four approaches to dealing with deadlocks.
Deadlock ignorance
Deadlock avoidance
Deadlock prevention
Deadlock detection & Recovery
5. Deadlock Avoidance
The deadlock avoidance algorithm is designed to minimize
the chances of deadlock occurring.
The resources currently available.
The recources currently allocated to each process.
Deadlock can be avoided by maintaining the system always in
safe state.
Safe sequence : The sequence in which the pending process
can successfully executed.
8. Mutual Exclusion-
We can deny this situation by simple protocol i.e by
“By Converting all non shareable resources to shareable resources”
Hold and wait-
We can deny this situation with the following protocols-
1. A process can request the resources only when the process has
none.
2. Each process to request and be allocated all its resources before it
begins execution.
9. No Preemption-
To ensure this condition does not hold we use the following protocols-
1. We preempt the desired resources from the waiting process and
allocate them to the requesting process.
Circular Wait-
We ensure that the circular wait must not happen so we apply a simple
solution-
1. Numbering all the resouce type and each process request
resources in an increasing order of ennumeration.
10. Deadlock Detection
As we can understand from name here we check safety and detect if any deadlock
has occurred.
Here DeadLock can be occurred and can be detected using different approaches.
1. Active Approach
2. Lazy Approach
Algorithms used for deadlock detection and recovery are-
a) For Single Instance of each resource type
b) For Multi instance of resource type
11. Single Instance of Each Resource Type
P1 P2 P3
P4
P5
R1 R3 R4
R2 R5
Resource Allocation Graph
P4
P2
P1 P3
P5
Corresponding wait for graph
Cycle
Occurs
12. Process Allocation Request Available
A B C A B C A B C
P1 0 1 0 0 0 0 0 0 0
P2 2 0 0 2 0 2 0 1 0
P3 3 0 3 0 0 0 3 1 3
P4 2 1 1 1 0 0 5 2 4
5 2 6
P5 0 0 2 0 0 2 7 2 6
Given-
A-7 B-2 C-6
+7 2 6
Request < =Available
Available= Available +
Allocated of used
process
SAFE SEQUENCE-
<P1, P3, P4 ,P5 ,P2>
•No Deadlock will
Occur if we follow
this sequence for
allocating resources .
For Multi Instance of Resource Type
13. Recovery From Deadlock
1. Process Termination-
• Abort all deadlock processes
• Abort one process at a time until the deadlock cycle is eliminated.
2. Resource Preemption-
• Selecting a victim
• Rollback
• Starvation