SlideShare a Scribd company logo
1 of 31
Download to read offline
DEADLOCK
T.Y.B.Sc. (CS)
By: Ms. Farhat Rahat Ali Shaikh
Content
› Deadlock
› Deadlock Necessary conditions
› Deadlock handling strategies
– Deadlock Prevention
– Deadlock Avoidance
› Banker’s Algorithm
– Deadlock Detection
› Resource Allocation Graph
› Wait – for – graph
2
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.
› Consider an example when two trains are coming toward each other on same track
and there is only one track, none of the trains can move once they are in front of
each other. Similar situation occurs in operating systems when there are two or
more processes hold some resources and wait for resources held by other(s). For
example, in the below diagram, Process 1 is holding Resource 1 and waiting for
resource 2 which is acquired by process 2, and process 2 is waiting for resource 1.
3
Deadlock Necessary
conditions
› Mutual Exclusion: One or more than one resource are non-sharable (Only one
process can use at a time)
› Hold and Wait: A process is holding at least one resource and waiting for resources.
› No Preemption: A resource cannot be taken from a process unless the process
releases the resource.
› Circular Wait: A set of processes are waiting for each other in circular form.
4
Deadlock handling strategies
› Deadlock Prevention: The system checks every transaction before it is executed to
make sure it doesn't lead the deadlock situations. Deadlocks can be prevented by
preventing at least one of the four required conditions:
– Mutual Exclusion: Resources shared such as read-only files do not lead to
deadlocks but resources, such as printers and tape drives, requires exclusive
access by a single process.
– Hold and Wait: In this condition processes must be prevented from holding one
or more resources while simultaneously waiting for one or more others.
– No Preemption: Preemption of process resource allocations can avoid the
condition of deadlocks, where ever possible.
– Circular Wait: Circular wait can be avoided if we number all resources, and
require that processes request resources only in strictly increasing(or decreasing)
order.
5
Deadlock handling strategies
› Deadlock Avoidance:
– The general idea behind deadlock avoidance is to prevent deadlocks from ever
happening, by preventing at least one of the above-mentioned conditions.
– This requires more information about each process, and tends to lead to low
device utilization.
– Deadlock avoidance is the simplest and most useful model that each process
declares the maximum number of resources of each type that it may need.
– The deadlock-avoidance algorithm helps you to dynamically assess the resource-
allocation state so that there can never be a circular-wait situation.
– Banker’s Algorithm is one of the strategy used to avoid deadlock.
6
Banker's Algorithm
› Banker's algorithm is a deadlock avoidance algorithm. It is named so because this
algorithm is used in banking systems to determine whether a loan can be granted or
not.
› Consider there are n account holders in a bank and the sum of the money in all of
their accounts is s. Every time a loan has to be granted by the bank, it subtracts the
loan amount from the total money the bank has. Then it checks if that difference is
greater than s. It is done because, only then, the bank would have enough money
even if all the n account holders draw all their money at once.
› Banker's algorithm works in a similar way in computers. Whenever a new process is
created, it must specify the maximum instances of each resource type that it needs,
exactly.
7
Banker's Algorithm
› Let us assume that there are n processes and m resource types. Some data structures
that are used to implement the banker's algorithm are:
– Available array : Available [j] = k
– Max matrix: Max [i][j] = k
– Allocation matrix: Allocation [i][j] = k
– Need matrix : Need[i][j] = Max [i][j] - Allocation [i][j]
› Banker’s Algorithm is further divided in two sub algorithms such as:
– Safety Algorithm: In order to apply the Banker's algorithm, we first need an
algorithm for determining whether or not a particular state is safe. This algorithm
determines if the current state of a system is safe or not.
– Resource Request Algorithm: This algorithm determines if a new request is safe,
and grants it only if it is safe to do so. When a request is made ( that does not
exceed currently available resources ), pretend it has been granted, and then see if
the resulting state is a safe one. If so, grant the request, and if not, deny the
request on basis of some conditions.
8
Banker's Algorithm› Example 1: Consider the given snapshot of the system. A system has 5 process and 3 types of
resources A, B, C.
9
A B C
P0 0 1 0
P1 2 0 0
P2 3 0 2
P3 2 1 1
P4 0 0 2
A B C
P0 7 5 0
P1 3 2 2
P2 9 0 2
P3 2 2 2
P4 4 3 3
Allocation Max
A B C
3 3 2
Available
Answer the following questions using Banker’s Algorithm:
Q1. What is the contents of Need Matrix?
Q2. Is the system in safe state?
Q3. If request from process P1 arrives as (1,0,2) can the request be granted immediately?
Banker's Algorithm
› Solution: Step 1: Need Matrix
Need[i][j] = Max [i][j] - Allocation [i][j]
10
7 5 0
3 2 2
9 0 2
2 2 2
4 3 3
= –
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Need[i][j] =
7 4 0
1 2 2
6 0 0
0 1 1
4 3 1
Banker's Algorithm
› Step 2: Safety Algorithm: Let Work and Finish be vectors of length m and n, respectively.
Initially,
Work = Available = {3, 3, 2}
Finish [i] = False (for i = 0, 1, 2, …., n – 1)
i.) Let i = 0, Finish [0] = F
Need (P0) ≤ Work
(7, 4, 0) ≤ (3, 3, 2) => False
11
Process P0 cannot be given resources. Hence, P0 has to wait.
ii.) Let i = 1, Finish [1] = F
Need (P1) ≤ Work
(1, 2, 2) ≤ (3, 3, 2) => True
Banker's Algorithm
12
Work = Work + Allocation (P1)
= (3, 3, 2) + (2, 0, 0)
Work = (5, 3, 2)
P1 releases resources after its execution.
Finish = {F, T, F, F, F}
Safe Sequence = {P1}
iii.) Let i = 2, Finish [2] = F
Need (P2) ≤ Work
(6, 0, 0) ≤ (5, 3, 2) => False
Process P2 cannot be given resources. Hence, P2 has to wait.
Banker's Algorithm
13
iv.) Let i = 3, Finish [3] = F
Need (P3) ≤ Work
(0, 1, 1) ≤ (5, 3, 2) => True
Work = Work + Allocation (P3)
= (5, 3, 2) + (2, 1, 1)
Work = (7, 4, 3)
P3 releases resources after its execution.
Finish = {F, T, F, T, F}
Safe Sequence = {P1,P3}
Banker's Algorithm
14
iv.) Let i = 4, Finish [4] = F
Need (P4) ≤ Work
(4, 3, 1) ≤ (7, 4, 3) => True
Work = Work + Allocation (P4)
= (7, 4, 3) + (0, 0, 2)
Work = (7, 4, 5)
P4 releases resources after its execution.
Finish = {F, T, F, T, T}
Safe Sequence = {P1,P3,P4}
Banker's Algorithm
15
vi.) Let i = 0, Finish [0] = F
Need (P0) ≤ Work
(7, 4, 3) ≤ (7, 4, 5) => True
Work = Work + Allocation (P0)
= (7, 4, 5) + (0, 1, 0)
Work = (7, 5, 5)
P0 releases resources after its execution.
Finish = {T, T, F, T, T}
Safe Sequence = {P1,P3,P4,P0}
Banker's Algorithm
16
vii.) Let i = 2, Finish [0] = F
Need (P2) ≤ Work
(6, 0, 0) ≤ (7, 5, 5) => True
Work = Work + Allocation (P0)
= (7, 5, 5) + (3, 0, 2)
Work = (10, 5, 7)
P2 releases resources after its execution.
Finish = {T, T, T, T, T}
Safe Sequence = {P1,P3,P4,P0,P2}
∴ Yes, The system is in safe state
Banker's Algorithm
17
Step 3: Resource Request Algorithm:
Request (P1) ≤ Need (P1)
(1, 0, 2) ≤ (1, 2, 2) => True
Request (P1) ≤ Available
(1, 0, 2) ≤ (3, 3, 2) => True
Then system pretends to fulfill request then modify resource allocation state as follows:
Available = Available – Request (P1)
= (3, 3, 2) – (1, 0, 2)
Available = (2, 3, 0)
Banker's Algorithm
18
Allocation (P1) = Allocation (P1) + Request (P1)
= (2, 0, 0) + (1, 0, 2)
Allocation (P1) = (3, 0, 2)
Need(P1) = Need (P1) – Request (P1)
= (1, 2, 2) – (1, 0, 2)
Need (P1) = (0, 2, 0)
Banker's Algorithm
› Example 1: Consider the given snapshot of the system. A system has 5 process and 4 types of
resources A, B, C, D. There are 3 instances of type A, 14 instances of type B, 12 instances of type
C and 12 instances of type D.
19
A B C D
P0 0 6 3 2
P1 0 0 1 2
P2 1 0 0 0
P3 1 3 5 4
P4 0 0 1 4
A B C D
P0 0 6 5 2
P1 0 0 1 2
P2 0 7 5 0
P3 2 3 5 6
P4 0 6 5 6
Allocation Max
Answer the following questions using Banker’s Algorithm:
Q1. What is the contents of Need Matrix?
Q2. Is the system in safe state?
Q3. If request from process P4 arrives as (0, 0, 4, 1) can the request be granted
immediately?
Banker's Algorithm
20
Solution:
Available = Total Resources - Total Allocation
= (3, 14, 12, 12) - (2, 9, 10, 12)
Available = (1, 5, 2, 0)
Solve the remaining as previous question.
Deadlock handling strategies
› Deadlock Detection:
– If deadlocks are not avoided, then another approach is to detect when they have
occurred and recover somehow.
– A deadlock occurrence can be detected by the resource scheduler. A resource
scheduler helps OS to keep track of all the resources which are allocated to
different processes. So, when a deadlock is detected, it can be resolved using
above two strategies.
– Deadlock detection can be done with the help of following technique:
› Resource allocation graph
› Wait – for – graph
21
Resource allocation graph
› If resource categories have only single instances of their resources, then deadlock
states can be detected by cycles in the resource-allocation graphs.
› In this case, unsafe states can be recognized and avoided by augmenting the resource-
allocation graph (RAG) with claim edges, which point from a process to a resource
that it may request in the future.
› In RAG vertices are two type –
– Process vertex – Every process will be represented as a process vertex. Generally,
the process will be represented with a circle.
– Resource vertex – Every resource will be represented as a resource vertex.
Generally, the resource will be represented with a rectangle. It is also two type –
› Single instance type resource – It represents as a box, inside the box, there will
be one dot. So the number of dots indicate how many instances are present of
each resource type.
› Multi-resource instance type resource – It also represents as a box, inside the
box, there will be many dots present.
22
Resource allocation graph
› There are two types of edges in RAG –
– Assign Edge – If you already assign a resource to a process then it is called Assign
edge.
– Request Edge – It means in future the process might want some resource to
complete the execution, that is called request edge.
23
Resource allocation graph
› Example 1 (Single instances RAG) – 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.
24
Resource allocation graph
› Example 2 (Multi-instances RAG) – From the given fig., it is not possible to say the
RAG is in a safe state or in an unsafe state. So to do that we use safe state algorithm.
25
Resource allocation graph
26
Example 1: Consider a system with 7 processes A through G and six types of resources
R through W with one resource for each type. Resource ownership is as follows
A holds R and wants S
B holds nothing but wants T
C holds nothing but wants S
D holds U and wants S and T
E holds T and wants V
F holds W and wants S
G holds V and wants U.
Is the system deadlocked, and if so, which processes are involved ?
Resource allocation graph
27
A holds R and wants S
B holds nothing but wants T
C holds nothing but wants S
D holds U and wants S and T
E holds T and wants V
F holds W and wants S
G holds V and wants U.
A
R
F
W
C
S
B
T
D
U
G E
V
Wait – for – graph
28
A
F
C
D
B
E
G
A holds R and wants S
B holds nothing but wants T
C holds nothing but wants S
D holds U and wants S and T
E holds T and wants V
F holds W and wants S
G holds V and wants U.
As cycle exists: D - > E -> G - > D
Resource allocation graph
29
Example 2: Consider the following sets P, R and E
P = {P1, P2, P3, P4}
R = {R1, R2, R3, R4}
E = {P1 -> R1 , P2 -> R3 , R1 -> P2, R3 - > P4, R2 - > P1, P4 - >R3}
Is the system deadlocked, and if so, which processes are involved ?
Deadlock
DEADLOCK
› The deadlock situation occurs when
one of the processes got blocked.
› Deadlock is an infinite process.
› Every Deadlock always has starvation.
› Deadlock happens then Mutual
exclusion, hold and wait. Here,
preemption and circular wait do not
occur simultaneously.
STARVATION
› Starvation is a situation where all the
low priority processes got blocked,
and the high priority processes
execute.
› Starvation is a long waiting but not an
infinite process.
› Every starvation doesn't necessarily
have a deadlock.
› It happens due to uncontrolled
priority and resource management.
30
THANK YOU
31

More Related Content

What's hot

Deadlock- System model, resource types, deadlock problem, deadlock characteri...
Deadlock- System model, resource types, deadlock problem, deadlock characteri...Deadlock- System model, resource types, deadlock problem, deadlock characteri...
Deadlock- System model, resource types, deadlock problem, deadlock characteri...Wakil Kumar
 
Deadlock and Banking Algorithm
Deadlock and Banking AlgorithmDeadlock and Banking Algorithm
Deadlock and Banking AlgorithmMD.ANISUR RAHMAN
 
Operating system Dead lock
Operating system Dead lockOperating system Dead lock
Operating system Dead lockKaram Munir Butt
 
Operating System Deadlock Galvin
Operating System  Deadlock GalvinOperating System  Deadlock Galvin
Operating System Deadlock GalvinSonali Chauhan
 
Deadlock detection & prevention
Deadlock detection & preventionDeadlock detection & prevention
Deadlock detection & preventionIkhtiarUddinShaHin
 
Mch7 deadlock
Mch7 deadlockMch7 deadlock
Mch7 deadlockwahab13
 
Deadlock avoidance (Safe State, Resource Allocation Graph Algorithm)
Deadlock avoidance (Safe State, Resource Allocation Graph Algorithm)Deadlock avoidance (Safe State, Resource Allocation Graph Algorithm)
Deadlock avoidance (Safe State, Resource Allocation Graph Algorithm)Shayek Parvez
 
Methods for handling deadlocks
Methods for handling deadlocksMethods for handling deadlocks
Methods for handling deadlocksA. S. M. Shafi
 
Mca ii os u-3 dead lock & io systems
Mca  ii  os u-3 dead lock & io systemsMca  ii  os u-3 dead lock & io systems
Mca ii os u-3 dead lock & io systemsRai University
 

What's hot (20)

Distributed deadlock
Distributed deadlockDistributed deadlock
Distributed deadlock
 
Deadlock- System model, resource types, deadlock problem, deadlock characteri...
Deadlock- System model, resource types, deadlock problem, deadlock characteri...Deadlock- System model, resource types, deadlock problem, deadlock characteri...
Deadlock- System model, resource types, deadlock problem, deadlock characteri...
 
Deadlock and Banking Algorithm
Deadlock and Banking AlgorithmDeadlock and Banking Algorithm
Deadlock and Banking Algorithm
 
Operating system Dead lock
Operating system Dead lockOperating system Dead lock
Operating system Dead lock
 
Operating System
Operating SystemOperating System
Operating System
 
Operating System Deadlock Galvin
Operating System  Deadlock GalvinOperating System  Deadlock Galvin
Operating System Deadlock Galvin
 
Deadlock detection & prevention
Deadlock detection & preventionDeadlock detection & prevention
Deadlock detection & prevention
 
Deadlock
DeadlockDeadlock
Deadlock
 
Mch7 deadlock
Mch7 deadlockMch7 deadlock
Mch7 deadlock
 
Deadlock avoidance (Safe State, Resource Allocation Graph Algorithm)
Deadlock avoidance (Safe State, Resource Allocation Graph Algorithm)Deadlock avoidance (Safe State, Resource Allocation Graph Algorithm)
Deadlock avoidance (Safe State, Resource Allocation Graph Algorithm)
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
 
Methods for handling deadlocks
Methods for handling deadlocksMethods for handling deadlocks
Methods for handling deadlocks
 
Chapter 7 - Deadlocks
Chapter 7 - DeadlocksChapter 7 - Deadlocks
Chapter 7 - Deadlocks
 
Deadlock
DeadlockDeadlock
Deadlock
 
7 Deadlocks
7 Deadlocks7 Deadlocks
7 Deadlocks
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
 
Deadlock
DeadlockDeadlock
Deadlock
 
Module3
Module3Module3
Module3
 
Mca ii os u-3 dead lock & io systems
Mca  ii  os u-3 dead lock & io systemsMca  ii  os u-3 dead lock & io systems
Mca ii os u-3 dead lock & io systems
 
Deadlock
DeadlockDeadlock
Deadlock
 

Similar to Deadlock

Similar to Deadlock (20)

OS - Unit 3 Deadlock (Bankers Algorithm).pptx
OS - Unit 3 Deadlock (Bankers Algorithm).pptxOS - Unit 3 Deadlock (Bankers Algorithm).pptx
OS - Unit 3 Deadlock (Bankers Algorithm).pptx
 
Gp1242 007 oer ppt
Gp1242 007 oer pptGp1242 007 oer ppt
Gp1242 007 oer ppt
 
Methods for handling deadlock
Methods for handling deadlockMethods for handling deadlock
Methods for handling deadlock
 
CH07.pdf
CH07.pdfCH07.pdf
CH07.pdf
 
Sucet os module_3_notes
Sucet os module_3_notesSucet os module_3_notes
Sucet os module_3_notes
 
OS Module-3 (2).pptx
OS Module-3 (2).pptxOS Module-3 (2).pptx
OS Module-3 (2).pptx
 
Module-2Deadlock.ppt
Module-2Deadlock.pptModule-2Deadlock.ppt
Module-2Deadlock.ppt
 
Deadlocks Part- III.pdf
Deadlocks Part- III.pdfDeadlocks Part- III.pdf
Deadlocks Part- III.pdf
 
OSLec14&15(Deadlocksinopratingsystem).pptx
OSLec14&15(Deadlocksinopratingsystem).pptxOSLec14&15(Deadlocksinopratingsystem).pptx
OSLec14&15(Deadlocksinopratingsystem).pptx
 
Deadlock avoidance and prevention .. computer networking
Deadlock avoidance and prevention .. computer networkingDeadlock avoidance and prevention .. computer networking
Deadlock avoidance and prevention .. computer networking
 
6. Deadlock.ppt
6. Deadlock.ppt6. Deadlock.ppt
6. Deadlock.ppt
 
Operating System Deadlock
Operating System DeadlockOperating System Deadlock
Operating System Deadlock
 
Deadlocks Part- II.pdf
Deadlocks Part- II.pdfDeadlocks Part- II.pdf
Deadlocks Part- II.pdf
 
Module 3 Deadlocks.pptx
Module 3 Deadlocks.pptxModule 3 Deadlocks.pptx
Module 3 Deadlocks.pptx
 
Ch8 OS
Ch8 OSCh8 OS
Ch8 OS
 
DeadlockMar21.ppt
DeadlockMar21.pptDeadlockMar21.ppt
DeadlockMar21.ppt
 
7308346-Deadlock.pptx
7308346-Deadlock.pptx7308346-Deadlock.pptx
7308346-Deadlock.pptx
 
Chapter 5(five).pdf
Chapter 5(five).pdfChapter 5(five).pdf
Chapter 5(five).pdf
 
OS-Part-06.pdf
OS-Part-06.pdfOS-Part-06.pdf
OS-Part-06.pdf
 
Process Synchronization And Deadlocks
Process Synchronization And DeadlocksProcess Synchronization And Deadlocks
Process Synchronization And Deadlocks
 

Recently uploaded

ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxLimon Prince
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint23600690
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaEADTU
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhleson0603
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code ExamplesPeter Brusilovsky
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17Celine George
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital ManagementMBA Assignment Experts
 

Recently uploaded (20)

ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 

Deadlock

  • 1. DEADLOCK T.Y.B.Sc. (CS) By: Ms. Farhat Rahat Ali Shaikh
  • 2. Content › Deadlock › Deadlock Necessary conditions › Deadlock handling strategies – Deadlock Prevention – Deadlock Avoidance › Banker’s Algorithm – Deadlock Detection › Resource Allocation Graph › Wait – for – graph 2
  • 3. 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. › Consider an example when two trains are coming toward each other on same track and there is only one track, none of the trains can move once they are in front of each other. Similar situation occurs in operating systems when there are two or more processes hold some resources and wait for resources held by other(s). For example, in the below diagram, Process 1 is holding Resource 1 and waiting for resource 2 which is acquired by process 2, and process 2 is waiting for resource 1. 3
  • 4. Deadlock Necessary conditions › Mutual Exclusion: One or more than one resource are non-sharable (Only one process can use at a time) › Hold and Wait: A process is holding at least one resource and waiting for resources. › No Preemption: A resource cannot be taken from a process unless the process releases the resource. › Circular Wait: A set of processes are waiting for each other in circular form. 4
  • 5. Deadlock handling strategies › Deadlock Prevention: The system checks every transaction before it is executed to make sure it doesn't lead the deadlock situations. Deadlocks can be prevented by preventing at least one of the four required conditions: – Mutual Exclusion: Resources shared such as read-only files do not lead to deadlocks but resources, such as printers and tape drives, requires exclusive access by a single process. – Hold and Wait: In this condition processes must be prevented from holding one or more resources while simultaneously waiting for one or more others. – No Preemption: Preemption of process resource allocations can avoid the condition of deadlocks, where ever possible. – Circular Wait: Circular wait can be avoided if we number all resources, and require that processes request resources only in strictly increasing(or decreasing) order. 5
  • 6. Deadlock handling strategies › Deadlock Avoidance: – The general idea behind deadlock avoidance is to prevent deadlocks from ever happening, by preventing at least one of the above-mentioned conditions. – This requires more information about each process, and tends to lead to low device utilization. – Deadlock avoidance is the simplest and most useful model that each process declares the maximum number of resources of each type that it may need. – The deadlock-avoidance algorithm helps you to dynamically assess the resource- allocation state so that there can never be a circular-wait situation. – Banker’s Algorithm is one of the strategy used to avoid deadlock. 6
  • 7. Banker's Algorithm › Banker's algorithm is a deadlock avoidance algorithm. It is named so because this algorithm is used in banking systems to determine whether a loan can be granted or not. › Consider there are n account holders in a bank and the sum of the money in all of their accounts is s. Every time a loan has to be granted by the bank, it subtracts the loan amount from the total money the bank has. Then it checks if that difference is greater than s. It is done because, only then, the bank would have enough money even if all the n account holders draw all their money at once. › Banker's algorithm works in a similar way in computers. Whenever a new process is created, it must specify the maximum instances of each resource type that it needs, exactly. 7
  • 8. Banker's Algorithm › Let us assume that there are n processes and m resource types. Some data structures that are used to implement the banker's algorithm are: – Available array : Available [j] = k – Max matrix: Max [i][j] = k – Allocation matrix: Allocation [i][j] = k – Need matrix : Need[i][j] = Max [i][j] - Allocation [i][j] › Banker’s Algorithm is further divided in two sub algorithms such as: – Safety Algorithm: In order to apply the Banker's algorithm, we first need an algorithm for determining whether or not a particular state is safe. This algorithm determines if the current state of a system is safe or not. – Resource Request Algorithm: This algorithm determines if a new request is safe, and grants it only if it is safe to do so. When a request is made ( that does not exceed currently available resources ), pretend it has been granted, and then see if the resulting state is a safe one. If so, grant the request, and if not, deny the request on basis of some conditions. 8
  • 9. Banker's Algorithm› Example 1: Consider the given snapshot of the system. A system has 5 process and 3 types of resources A, B, C. 9 A B C P0 0 1 0 P1 2 0 0 P2 3 0 2 P3 2 1 1 P4 0 0 2 A B C P0 7 5 0 P1 3 2 2 P2 9 0 2 P3 2 2 2 P4 4 3 3 Allocation Max A B C 3 3 2 Available Answer the following questions using Banker’s Algorithm: Q1. What is the contents of Need Matrix? Q2. Is the system in safe state? Q3. If request from process P1 arrives as (1,0,2) can the request be granted immediately?
  • 10. Banker's Algorithm › Solution: Step 1: Need Matrix Need[i][j] = Max [i][j] - Allocation [i][j] 10 7 5 0 3 2 2 9 0 2 2 2 2 4 3 3 = – 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Need[i][j] = 7 4 0 1 2 2 6 0 0 0 1 1 4 3 1
  • 11. Banker's Algorithm › Step 2: Safety Algorithm: Let Work and Finish be vectors of length m and n, respectively. Initially, Work = Available = {3, 3, 2} Finish [i] = False (for i = 0, 1, 2, …., n – 1) i.) Let i = 0, Finish [0] = F Need (P0) ≤ Work (7, 4, 0) ≤ (3, 3, 2) => False 11 Process P0 cannot be given resources. Hence, P0 has to wait. ii.) Let i = 1, Finish [1] = F Need (P1) ≤ Work (1, 2, 2) ≤ (3, 3, 2) => True
  • 12. Banker's Algorithm 12 Work = Work + Allocation (P1) = (3, 3, 2) + (2, 0, 0) Work = (5, 3, 2) P1 releases resources after its execution. Finish = {F, T, F, F, F} Safe Sequence = {P1} iii.) Let i = 2, Finish [2] = F Need (P2) ≤ Work (6, 0, 0) ≤ (5, 3, 2) => False Process P2 cannot be given resources. Hence, P2 has to wait.
  • 13. Banker's Algorithm 13 iv.) Let i = 3, Finish [3] = F Need (P3) ≤ Work (0, 1, 1) ≤ (5, 3, 2) => True Work = Work + Allocation (P3) = (5, 3, 2) + (2, 1, 1) Work = (7, 4, 3) P3 releases resources after its execution. Finish = {F, T, F, T, F} Safe Sequence = {P1,P3}
  • 14. Banker's Algorithm 14 iv.) Let i = 4, Finish [4] = F Need (P4) ≤ Work (4, 3, 1) ≤ (7, 4, 3) => True Work = Work + Allocation (P4) = (7, 4, 3) + (0, 0, 2) Work = (7, 4, 5) P4 releases resources after its execution. Finish = {F, T, F, T, T} Safe Sequence = {P1,P3,P4}
  • 15. Banker's Algorithm 15 vi.) Let i = 0, Finish [0] = F Need (P0) ≤ Work (7, 4, 3) ≤ (7, 4, 5) => True Work = Work + Allocation (P0) = (7, 4, 5) + (0, 1, 0) Work = (7, 5, 5) P0 releases resources after its execution. Finish = {T, T, F, T, T} Safe Sequence = {P1,P3,P4,P0}
  • 16. Banker's Algorithm 16 vii.) Let i = 2, Finish [0] = F Need (P2) ≤ Work (6, 0, 0) ≤ (7, 5, 5) => True Work = Work + Allocation (P0) = (7, 5, 5) + (3, 0, 2) Work = (10, 5, 7) P2 releases resources after its execution. Finish = {T, T, T, T, T} Safe Sequence = {P1,P3,P4,P0,P2} ∴ Yes, The system is in safe state
  • 17. Banker's Algorithm 17 Step 3: Resource Request Algorithm: Request (P1) ≤ Need (P1) (1, 0, 2) ≤ (1, 2, 2) => True Request (P1) ≤ Available (1, 0, 2) ≤ (3, 3, 2) => True Then system pretends to fulfill request then modify resource allocation state as follows: Available = Available – Request (P1) = (3, 3, 2) – (1, 0, 2) Available = (2, 3, 0)
  • 18. Banker's Algorithm 18 Allocation (P1) = Allocation (P1) + Request (P1) = (2, 0, 0) + (1, 0, 2) Allocation (P1) = (3, 0, 2) Need(P1) = Need (P1) – Request (P1) = (1, 2, 2) – (1, 0, 2) Need (P1) = (0, 2, 0)
  • 19. Banker's Algorithm › Example 1: Consider the given snapshot of the system. A system has 5 process and 4 types of resources A, B, C, D. There are 3 instances of type A, 14 instances of type B, 12 instances of type C and 12 instances of type D. 19 A B C D P0 0 6 3 2 P1 0 0 1 2 P2 1 0 0 0 P3 1 3 5 4 P4 0 0 1 4 A B C D P0 0 6 5 2 P1 0 0 1 2 P2 0 7 5 0 P3 2 3 5 6 P4 0 6 5 6 Allocation Max Answer the following questions using Banker’s Algorithm: Q1. What is the contents of Need Matrix? Q2. Is the system in safe state? Q3. If request from process P4 arrives as (0, 0, 4, 1) can the request be granted immediately?
  • 20. Banker's Algorithm 20 Solution: Available = Total Resources - Total Allocation = (3, 14, 12, 12) - (2, 9, 10, 12) Available = (1, 5, 2, 0) Solve the remaining as previous question.
  • 21. Deadlock handling strategies › Deadlock Detection: – If deadlocks are not avoided, then another approach is to detect when they have occurred and recover somehow. – A deadlock occurrence can be detected by the resource scheduler. A resource scheduler helps OS to keep track of all the resources which are allocated to different processes. So, when a deadlock is detected, it can be resolved using above two strategies. – Deadlock detection can be done with the help of following technique: › Resource allocation graph › Wait – for – graph 21
  • 22. Resource allocation graph › If resource categories have only single instances of their resources, then deadlock states can be detected by cycles in the resource-allocation graphs. › In this case, unsafe states can be recognized and avoided by augmenting the resource- allocation graph (RAG) with claim edges, which point from a process to a resource that it may request in the future. › In RAG vertices are two type – – Process vertex – Every process will be represented as a process vertex. Generally, the process will be represented with a circle. – Resource vertex – Every resource will be represented as a resource vertex. Generally, the resource will be represented with a rectangle. It is also two type – › Single instance type resource – It represents as a box, inside the box, there will be one dot. So the number of dots indicate how many instances are present of each resource type. › Multi-resource instance type resource – It also represents as a box, inside the box, there will be many dots present. 22
  • 23. Resource allocation graph › There are two types of edges in RAG – – Assign Edge – If you already assign a resource to a process then it is called Assign edge. – Request Edge – It means in future the process might want some resource to complete the execution, that is called request edge. 23
  • 24. Resource allocation graph › Example 1 (Single instances RAG) – 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. 24
  • 25. Resource allocation graph › Example 2 (Multi-instances RAG) – From the given fig., it is not possible to say the RAG is in a safe state or in an unsafe state. So to do that we use safe state algorithm. 25
  • 26. Resource allocation graph 26 Example 1: Consider a system with 7 processes A through G and six types of resources R through W with one resource for each type. Resource ownership is as follows A holds R and wants S B holds nothing but wants T C holds nothing but wants S D holds U and wants S and T E holds T and wants V F holds W and wants S G holds V and wants U. Is the system deadlocked, and if so, which processes are involved ?
  • 27. Resource allocation graph 27 A holds R and wants S B holds nothing but wants T C holds nothing but wants S D holds U and wants S and T E holds T and wants V F holds W and wants S G holds V and wants U. A R F W C S B T D U G E V
  • 28. Wait – for – graph 28 A F C D B E G A holds R and wants S B holds nothing but wants T C holds nothing but wants S D holds U and wants S and T E holds T and wants V F holds W and wants S G holds V and wants U. As cycle exists: D - > E -> G - > D
  • 29. Resource allocation graph 29 Example 2: Consider the following sets P, R and E P = {P1, P2, P3, P4} R = {R1, R2, R3, R4} E = {P1 -> R1 , P2 -> R3 , R1 -> P2, R3 - > P4, R2 - > P1, P4 - >R3} Is the system deadlocked, and if so, which processes are involved ?
  • 30. Deadlock DEADLOCK › The deadlock situation occurs when one of the processes got blocked. › Deadlock is an infinite process. › Every Deadlock always has starvation. › Deadlock happens then Mutual exclusion, hold and wait. Here, preemption and circular wait do not occur simultaneously. STARVATION › Starvation is a situation where all the low priority processes got blocked, and the high priority processes execute. › Starvation is a long waiting but not an infinite process. › Every starvation doesn't necessarily have a deadlock. › It happens due to uncontrolled priority and resource management. 30