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
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
Method of Handling Deadlock
There are four approaches to dealing with deadlocks.
 Deadlock ignorance
 Deadlock avoidance
 Deadlock prevention
 Deadlock detection & Recovery
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.
Banker’s Algorithm
Process Allocation Max Available Remaining ->Max-allocate
R1 R2 R3 R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 0 1 0 7 5 3 3 3 2 7 4 3 Need<=work
+2 0 0
P2 2 0 0 3 2 2 5 3 2 1 2 2
+2 1 1
P3 3 0 2 9 0 2 7 4 3 6 0 0
+0 1 0
P4 2 1 1 2 2 2 7 5 3 0 1 1
+3 0 2 Safe Sequence
7 2 3 10 5 5 P2 -> P4 -> P1 ->P3
Given-
R1-10 R2-5 R3-5
Deadlock Prevention
Elimination of
• Mutual Exclusion
• Hold and Wait Condition
• No Preemption Condition
• Circular Condition
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.
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.
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
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
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
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
OS Presentation 1 (1).pptx
OS Presentation 1 (1).pptx

OS Presentation 1 (1).pptx

  • 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 forDeadlocks  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 HandlingDeadlock There are four approaches to dealing with deadlocks.  Deadlock ignorance  Deadlock avoidance  Deadlock prevention  Deadlock detection & Recovery
  • 5.
    Deadlock Avoidance  Thedeadlock 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.
  • 6.
    Banker’s Algorithm Process AllocationMax Available Remaining ->Max-allocate R1 R2 R3 R1 R2 R3 R1 R2 R3 R1 R2 R3 P1 0 1 0 7 5 3 3 3 2 7 4 3 Need<=work +2 0 0 P2 2 0 0 3 2 2 5 3 2 1 2 2 +2 1 1 P3 3 0 2 9 0 2 7 4 3 6 0 0 +0 1 0 P4 2 1 1 2 2 2 7 5 3 0 1 1 +3 0 2 Safe Sequence 7 2 3 10 5 5 P2 -> P4 -> P1 ->P3 Given- R1-10 R2-5 R3-5
  • 7.
    Deadlock Prevention Elimination of •Mutual Exclusion • Hold and Wait Condition • No Preemption Condition • Circular Condition
  • 8.
    Mutual Exclusion- We candeny 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 ensurethis 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  Aswe 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 ofEach 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 RequestAvailable 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