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

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 

Recently uploaded (20)

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

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