SlideShare a Scribd company logo
1 of 19
Operating Systems
Unit 3
CPU Scheduling:
1. Scheduling Concepts,
2. Performance Criteria,
3. Process States,
4. Process Transition Diagram,
5. Schedulers,
6. Process Control Block (PCB),
7. Process address space,
8. Process identification information,
9. Threads and their management,
10. Scheduling Algorithms,
11. Multiprocessor Scheduling.
Deadlock:
1. System model,
2. Deadlock characterization,
3. Prevention,
4. Avoidance and detection,
5. Recovery from deadlock.
Deadlock : System model
• 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 the same track and there is only one track,
none of the trains can move once they are in front of
each other.
• Deadlock can arise if the following four conditions hold
simultaneously (Necessary Conditions)
• Mutual Exclusion: Two or more resources are non-
shareable (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.
Understanding Conditions for Deadlock
Mutual Exclusion: When two people meet in the landings, they
can’t just walk through because there is space only for one
person. This condition allows only one person (or process) to
use the step between them (or the resource) is the first
condition necessary for the occurrence of the deadlock.
Hold and Wait: When the two people refuse to retreat and hold
their ground, it is called holding. This is the next necessary
condition for deadlock.
No Preemption: For resolving the deadlock one can simply
cancel one of the processes for other to continue. But the
Operating System doesn’t do so. It allocates the resources to
the processors for as much time as is needed until the task is
completed. Hence, there is no temporary reallocation of the
resources. It is the third condition for deadlock.
Circular Wait: When the two people refuse to retreat and wait
for each other to retreat so that they can complete their task, it
is called circular wait. It is the last condition for deadlock to
occur.
Resource-Allocation Graph
• In RAG vertices are two type –
• 1. Process vertex – Every process will be represented as a process vertex. Generally, the
process will be represented with a circle.
• 2. Resource vertex – Every resource will be represented as a resource vertex. 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.
• A set of vertices V and a set of edges E. V is partitioned into two types:
• P = {P1, P2, …, Pn}, the set consisting of all the processes in the system
• R = {R1, R2, …, Rm}, the set consisting of all resource types in the system
• request edge – directed edge Pi  Rj
• assignment edge – directed edge Rj  Pi
Example of a Resource Allocation Graph
With A Deadlock With A Cycle But
No Deadlock
• If graph contains no cycles :
• no deadlock
• If graph contains a cycle :
• if only one instance per
resource type, then
deadlock
• if several instances per
resource type, possibility
of deadlock
Methods for Handling Deadlocks
• Two ways to ensure that the system will never enter a deadlock state:
• Deadlock prevention
• Deadlock avoidance
• System is in safe state if there exists a sequence <P1,
P2, …, Pn> of ALL the processes in the systems such
that for each Pi, the resources that Pi can still request
can be satisfied by
currently available resources + resources held by
all the Pj, with j < I
That is:
• If Pi resource needs are not immediately available,
then Pi can wait until all Pj have finished
• When Pj is finished, Pi can obtain needed resources,
execute, return allocated resources, and terminate
• When Pi terminates, Pi+1 can obtain its needed
resources, and so on
Deadlock avoidance
• Single instance of a resource type
• Use a resource-allocation graph
• Multiple instances of a resource type
• Use the banker’s algorithm
Banker’s Algorithm
• The banker’s algorithm is a resource allocation and deadlock avoidance algorithm
that tests for safety by simulating the allocation for predetermined maximum
possible amounts of all resources, then makes an “s-state” check to test for possible
activities, before deciding whether allocation should be allowed to continue.
• Let n = number of processes, and m = number of resources types.
• Available: Vector of length m. If available [j] = k, there are k instances of resource
type Rj available
• Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k
instances of resource type Rj
• Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k
instances of Rj
• Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to
complete its task
Need [i,j] = Max[i,j] – Allocation [i,j]
Banker’s Algorithm
• Banker’s algorithm consists of
1. Safety algorithm and
2. Resource request algorithm
• Safety Algorithm: The algorithm for finding out
whether or not a system is in a safe state
can be described as follows:
1) Let Work and Finish be vectors of length
‘m’ and ‘n’ respectively.
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
2) Find an i such that both
a) Finish[i] = false
b) Needi <= Work
if no such i exists goto step (4)
3) Work = Work + Allocation[i]
Finish[i] = true
goto step (2)
4) if Finish [i] = true for all i
then the system is in a safe state
• Resource-Request Algorithm : Let Requesti be the request
array for process Pi. Requesti [j] = k means process Pi
wants k instances of resource type Rj. When a request for
resources is made by process Pi, the following actions are
taken:
1) If Requesti <= Needi
Goto step (2) ; otherwise, raise an error condition,
since the process has exceeded its maximum claim.
2) If Requesti <= Available
Goto step (3); otherwise, Pi must wait, since the
resources are not available.
3) Have the system pretend to have allocated the requested
resources to process Pi by modifying the state as
follows:
• Available = Available – Requesti
• Allocationi = Allocationi + Requesti
• Needi = Needi– Requesti
Bankers Algorithm Example 1
• Consider the following snapshot of a system-
• Answer the following questions using the Banker’s algorithm-
• (i) What is the content of the matrix need?
• (ii) Is the system in a safe state?
• (iii) If a request from process P1 arrives for (0,4,2,0), can the request be granted
immediately?
Bankers Algorithm Example
Process Allocation Max Available Need
A B C D A B C D A B C D A B C D
P0 0 0 1 2 0 0 1 2 1 5 2 0 0 0 0 0
P1 1 0 0 0 1 7 5 0 1 5 3 2 0 7 5 0
P2 1 3 5 4 2 3 5 6 1 5 3 2 1 0 0 2
P3 0 6 3 2 0 6 5 2 2 8 8 6 0 0 2 0
P4 0 0 1 4 0 6 5 6 2 14 11 8 0 6 4 2
P1 1 0 0 0 1 7 5 0 2 14 12 12 0 7 5 0
• Step 1-Need =Max-Allocation, make a matrix of need column.(Written in green below)
• Step 2-Check Need <= Available (check individual variables of both corresponding columns).
• If available < need then write same available as in last row. For example for P1- Need B(7)> Available B(5)
(See in red) and so system is not in safe state.
• If Available > Need then write next Process availability as:
• Available= Available + Allocation (written in yellow)
• So safe sequence is (P0,P2,P3,P4,P0)
Bankers Algorithm Example 1
• Step3 : To Check if a request from process P1 arrives for (0,4,2,0), can the request be granted
immediately
• Compare Given Allocation with Corresponding Need: For P1 (0,4,2,0)< (0,7,5,0)
• Compare Given Allocation with Corresponding Available: For P1 (0,4,2,0) <(1,5,2,0)
• So new Available= (1,5,2,0)-(0,4,2,0)=(1,1,0,0)
• And so new Allocation= (1,0,0,0)+(0,4,2,0)=(1,4,2,0)
• And so new need for P1=(0,7,5,0) – (0,4,2,0) =(0,3,3,0)
• Now replace the previous values of process P1 with new values:
Process Allocation Max Available Need
A B C D A B C D A B C D A B C D
P0 0 0 1 2 0 0 1 2 1 1 0 0 0 0 0 0
P1 1 4 2 0 1 7 5 0 0 3 3 0
P2 1 3 5 4 2 3 5 6 1 0 0 2
P3 0 6 3 2 0 6 5 2 0 0 2 0
P4 0 0 1 4 0 6 5 6 0 6 4 2
• Step 4: Now repeat Step1 t- Step 2 and find safe sequence.
Bankers Algorithm Example 2
• Consider a system with five processes Po through P4 and three resource types A,
B, and C. Resource type A has 10 instances, resource type B has 5 instances, and
resource type C has 7 instances. Suppose that, at time T0 , the following
snapshot of the system has been taken:
Solution:
Available instances of A = Total – Allocated = 10 –
(0+2+3+2+0) = 3
Similarly, Available instances of B = Total – Allocated = 5
– (1+0+0+1+0) = 3
Finally, Available instances of C = Total – Allocated = 7 –
(0+0+2+1+2) = 2
Next, Calculate Need by using the formula
Need = Max – Allocation
………………………A B C
Need for P0 = 7 4 3
Bankers Algorithm Example 2
• Now find out any process whose Need can
be satisfied with Available. P1 is one such
process. So, Add P1 to safe sequence <P1>.
• Update the Available by using formula
Available = Available + Allocation of
process added to safe sequence (P1)
So, new Available is 5 3 2
• Next, find out another process whose Need
is less than the updated Available i.e., 5 3 2.
P3 is one such process. Hence, Add P3
to safe sequence <P1, P3>.
• Now, Update Available by adding Allocation
of P3 to current Available.
• Proceed in the same manner. If all the
process can be added to the safe sequence
then the system is in safe state otherwise
not.
Bankers Algorithm Example 3
• Considering a system with five processes P0 through P4 and three resources of
type A, B, C. Resource type A has 10 instances, B has 5 instances and type C has
7 instances. Suppose at time t0 following snapshot of the system has been
taken:
A) What will be the content of the Need matrix?
Need [i, j] = Max [i, j] – Allocation [i, j]
Bankers Algorithm Example 3
• Is the system in a safe state? If Yes, then what is the safe sequence?
Bankers Algorithm Example 3
• What will happen if process P1 requests one additional instance of resource type
A and two instances of resource type C?
Bankers Algorithm Example 3

More Related Content

What's hot

Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Ra'Fat Al-Msie'deen
 
8 memory management strategies
8 memory management strategies8 memory management strategies
8 memory management strategiesDr. Loganathan R
 
Operating system 11 system calls
Operating system 11 system callsOperating system 11 system calls
Operating system 11 system callsVaibhav Khanna
 
Deadlock and Banking Algorithm
Deadlock and Banking AlgorithmDeadlock and Banking Algorithm
Deadlock and Banking AlgorithmMD.ANISUR RAHMAN
 
System call (Fork +Exec)
System call (Fork +Exec)System call (Fork +Exec)
System call (Fork +Exec)Amit Ghosh
 
Three Address code
Three Address code Three Address code
Three Address code Pooja Dixit
 
The Maximum Subarray Problem
The Maximum Subarray ProblemThe Maximum Subarray Problem
The Maximum Subarray ProblemKamran Ashraf
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design) Tasif Tanzim
 

What's hot (20)

Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"
 
Compiler Design Unit 2
Compiler Design Unit 2Compiler Design Unit 2
Compiler Design Unit 2
 
8 memory management strategies
8 memory management strategies8 memory management strategies
8 memory management strategies
 
Operating system 11 system calls
Operating system 11 system callsOperating system 11 system calls
Operating system 11 system calls
 
BANKER'S ALGORITHM
BANKER'S ALGORITHMBANKER'S ALGORITHM
BANKER'S ALGORITHM
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Deadlock and Banking Algorithm
Deadlock and Banking AlgorithmDeadlock and Banking Algorithm
Deadlock and Banking Algorithm
 
Segmentation in operating systems
Segmentation in operating systemsSegmentation in operating systems
Segmentation in operating systems
 
System call (Fork +Exec)
System call (Fork +Exec)System call (Fork +Exec)
System call (Fork +Exec)
 
CPU Scheduling Algorithms
CPU Scheduling AlgorithmsCPU Scheduling Algorithms
CPU Scheduling Algorithms
 
Three Address code
Three Address code Three Address code
Three Address code
 
Compiler Design Unit 5
Compiler Design Unit 5Compiler Design Unit 5
Compiler Design Unit 5
 
The Maximum Subarray Problem
The Maximum Subarray ProblemThe Maximum Subarray Problem
The Maximum Subarray Problem
 
SCHEDULING ALGORITHMS
SCHEDULING ALGORITHMSSCHEDULING ALGORITHMS
SCHEDULING ALGORITHMS
 
Shortest Job First
Shortest Job FirstShortest Job First
Shortest Job First
 
Operations on Processes
Operations on ProcessesOperations on Processes
Operations on Processes
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
Loop optimization
Loop optimizationLoop optimization
Loop optimization
 
Bankers algorithm
Bankers algorithmBankers algorithm
Bankers algorithm
 
Loops in flow
Loops in flowLoops in flow
Loops in flow
 

Similar to OS - Unit 3 Deadlock (Bankers Algorithm).pptx

14th November - Deadlock Prevention, Avoidance.ppt
14th November - Deadlock Prevention, Avoidance.ppt14th November - Deadlock Prevention, Avoidance.ppt
14th November - Deadlock Prevention, Avoidance.pptUnknown664473
 
Deadlock in Real Time operating Systempptx
Deadlock in Real Time operating SystempptxDeadlock in Real Time operating Systempptx
Deadlock in Real Time operating Systempptxssuserca5764
 
Methods for handling deadlock
Methods for handling deadlockMethods for handling deadlock
Methods for handling deadlocksangrampatil81
 
Module 3 Deadlocks.pptx
Module 3 Deadlocks.pptxModule 3 Deadlocks.pptx
Module 3 Deadlocks.pptxshreesha16
 
Module-2Deadlock.ppt
Module-2Deadlock.pptModule-2Deadlock.ppt
Module-2Deadlock.pptKAnurag2
 
deadlocks.pptx
deadlocks.pptxdeadlocks.pptx
deadlocks.pptxJOHNZHOU52
 
OSLec14&15(Deadlocksinopratingsystem).pptx
OSLec14&15(Deadlocksinopratingsystem).pptxOSLec14&15(Deadlocksinopratingsystem).pptx
OSLec14&15(Deadlocksinopratingsystem).pptxssusere16bd9
 
Ch8 OS
Ch8 OSCh8 OS
Ch8 OSC.U
 
Deadlock Algorithms 3.pptx
Deadlock Algorithms 3.pptxDeadlock Algorithms 3.pptx
Deadlock Algorithms 3.pptxAbuBakkarShayan
 
6. Deadlock_1640227623705.pptx
6. Deadlock_1640227623705.pptx6. Deadlock_1640227623705.pptx
6. Deadlock_1640227623705.pptxshrijanasharma3
 

Similar to OS - Unit 3 Deadlock (Bankers Algorithm).pptx (20)

14th November - Deadlock Prevention, Avoidance.ppt
14th November - Deadlock Prevention, Avoidance.ppt14th November - Deadlock Prevention, Avoidance.ppt
14th November - Deadlock Prevention, Avoidance.ppt
 
Deadlocks Part- II.pdf
Deadlocks Part- II.pdfDeadlocks Part- II.pdf
Deadlocks Part- II.pdf
 
Deadlock
DeadlockDeadlock
Deadlock
 
deadlock part5 unit 2.ppt
deadlock part5 unit 2.pptdeadlock part5 unit 2.ppt
deadlock part5 unit 2.ppt
 
Deadlocks Part- III.pdf
Deadlocks Part- III.pdfDeadlocks Part- III.pdf
Deadlocks Part- III.pdf
 
Deadlock in Real Time operating Systempptx
Deadlock in Real Time operating SystempptxDeadlock in Real Time operating Systempptx
Deadlock in Real Time operating Systempptx
 
Ch7 deadlocks
Ch7   deadlocksCh7   deadlocks
Ch7 deadlocks
 
Os5
Os5Os5
Os5
 
Methods for handling deadlock
Methods for handling deadlockMethods for handling deadlock
Methods for handling deadlock
 
Module 3 Deadlocks.pptx
Module 3 Deadlocks.pptxModule 3 Deadlocks.pptx
Module 3 Deadlocks.pptx
 
Deadlock
DeadlockDeadlock
Deadlock
 
CH07.pdf
CH07.pdfCH07.pdf
CH07.pdf
 
Module-2Deadlock.ppt
Module-2Deadlock.pptModule-2Deadlock.ppt
Module-2Deadlock.ppt
 
Ch07 deadlocks
Ch07 deadlocksCh07 deadlocks
Ch07 deadlocks
 
deadlocks.pptx
deadlocks.pptxdeadlocks.pptx
deadlocks.pptx
 
OSLec14&15(Deadlocksinopratingsystem).pptx
OSLec14&15(Deadlocksinopratingsystem).pptxOSLec14&15(Deadlocksinopratingsystem).pptx
OSLec14&15(Deadlocksinopratingsystem).pptx
 
Ch8 OS
Ch8 OSCh8 OS
Ch8 OS
 
Deadlock Algorithms 3.pptx
Deadlock Algorithms 3.pptxDeadlock Algorithms 3.pptx
Deadlock Algorithms 3.pptx
 
6. Deadlock_1640227623705.pptx
6. Deadlock_1640227623705.pptx6. Deadlock_1640227623705.pptx
6. Deadlock_1640227623705.pptx
 
Gp1242 007 oer ppt
Gp1242 007 oer pptGp1242 007 oer ppt
Gp1242 007 oer ppt
 

Recently uploaded

(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 

Recently uploaded (20)

(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 

OS - Unit 3 Deadlock (Bankers Algorithm).pptx

  • 2. Unit 3 CPU Scheduling: 1. Scheduling Concepts, 2. Performance Criteria, 3. Process States, 4. Process Transition Diagram, 5. Schedulers, 6. Process Control Block (PCB), 7. Process address space, 8. Process identification information, 9. Threads and their management, 10. Scheduling Algorithms, 11. Multiprocessor Scheduling. Deadlock: 1. System model, 2. Deadlock characterization, 3. Prevention, 4. Avoidance and detection, 5. Recovery from deadlock.
  • 3. Deadlock : System model • 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 the same track and there is only one track, none of the trains can move once they are in front of each other. • Deadlock can arise if the following four conditions hold simultaneously (Necessary Conditions) • Mutual Exclusion: Two or more resources are non- shareable (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. Understanding Conditions for Deadlock Mutual Exclusion: When two people meet in the landings, they can’t just walk through because there is space only for one person. This condition allows only one person (or process) to use the step between them (or the resource) is the first condition necessary for the occurrence of the deadlock. Hold and Wait: When the two people refuse to retreat and hold their ground, it is called holding. This is the next necessary condition for deadlock. No Preemption: For resolving the deadlock one can simply cancel one of the processes for other to continue. But the Operating System doesn’t do so. It allocates the resources to the processors for as much time as is needed until the task is completed. Hence, there is no temporary reallocation of the resources. It is the third condition for deadlock. Circular Wait: When the two people refuse to retreat and wait for each other to retreat so that they can complete their task, it is called circular wait. It is the last condition for deadlock to occur.
  • 5. Resource-Allocation Graph • In RAG vertices are two type – • 1. Process vertex – Every process will be represented as a process vertex. Generally, the process will be represented with a circle. • 2. Resource vertex – Every resource will be represented as a resource vertex. 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. • A set of vertices V and a set of edges E. V is partitioned into two types: • P = {P1, P2, …, Pn}, the set consisting of all the processes in the system • R = {R1, R2, …, Rm}, the set consisting of all resource types in the system • request edge – directed edge Pi  Rj • assignment edge – directed edge Rj  Pi
  • 6. Example of a Resource Allocation Graph With A Deadlock With A Cycle But No Deadlock • If graph contains no cycles : • no deadlock • If graph contains a cycle : • if only one instance per resource type, then deadlock • if several instances per resource type, possibility of deadlock
  • 7. Methods for Handling Deadlocks • Two ways to ensure that the system will never enter a deadlock state: • Deadlock prevention • Deadlock avoidance • System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the processes in the systems such that for each Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j < I That is: • If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished • When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate • When Pi terminates, Pi+1 can obtain its needed resources, and so on
  • 8. Deadlock avoidance • Single instance of a resource type • Use a resource-allocation graph • Multiple instances of a resource type • Use the banker’s algorithm
  • 9. Banker’s Algorithm • The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the allocation for predetermined maximum possible amounts of all resources, then makes an “s-state” check to test for possible activities, before deciding whether allocation should be allowed to continue. • Let n = number of processes, and m = number of resources types. • Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj available • Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances of resource type Rj • Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj • Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task Need [i,j] = Max[i,j] – Allocation [i,j]
  • 10. Banker’s Algorithm • Banker’s algorithm consists of 1. Safety algorithm and 2. Resource request algorithm • Safety Algorithm: The algorithm for finding out whether or not a system is in a safe state can be described as follows: 1) Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively. Initialize: Work = Available Finish[i] = false; for i=1, 2, 3, 4….n 2) Find an i such that both a) Finish[i] = false b) Needi <= Work if no such i exists goto step (4) 3) Work = Work + Allocation[i] Finish[i] = true goto step (2) 4) if Finish [i] = true for all i then the system is in a safe state • Resource-Request Algorithm : Let Requesti be the request array for process Pi. Requesti [j] = k means process Pi wants k instances of resource type Rj. When a request for resources is made by process Pi, the following actions are taken: 1) If Requesti <= Needi Goto step (2) ; otherwise, raise an error condition, since the process has exceeded its maximum claim. 2) If Requesti <= Available Goto step (3); otherwise, Pi must wait, since the resources are not available. 3) Have the system pretend to have allocated the requested resources to process Pi by modifying the state as follows: • Available = Available – Requesti • Allocationi = Allocationi + Requesti • Needi = Needi– Requesti
  • 11. Bankers Algorithm Example 1 • Consider the following snapshot of a system- • Answer the following questions using the Banker’s algorithm- • (i) What is the content of the matrix need? • (ii) Is the system in a safe state? • (iii) If a request from process P1 arrives for (0,4,2,0), can the request be granted immediately?
  • 12. Bankers Algorithm Example Process Allocation Max Available Need A B C D A B C D A B C D A B C D P0 0 0 1 2 0 0 1 2 1 5 2 0 0 0 0 0 P1 1 0 0 0 1 7 5 0 1 5 3 2 0 7 5 0 P2 1 3 5 4 2 3 5 6 1 5 3 2 1 0 0 2 P3 0 6 3 2 0 6 5 2 2 8 8 6 0 0 2 0 P4 0 0 1 4 0 6 5 6 2 14 11 8 0 6 4 2 P1 1 0 0 0 1 7 5 0 2 14 12 12 0 7 5 0 • Step 1-Need =Max-Allocation, make a matrix of need column.(Written in green below) • Step 2-Check Need <= Available (check individual variables of both corresponding columns). • If available < need then write same available as in last row. For example for P1- Need B(7)> Available B(5) (See in red) and so system is not in safe state. • If Available > Need then write next Process availability as: • Available= Available + Allocation (written in yellow) • So safe sequence is (P0,P2,P3,P4,P0)
  • 13. Bankers Algorithm Example 1 • Step3 : To Check if a request from process P1 arrives for (0,4,2,0), can the request be granted immediately • Compare Given Allocation with Corresponding Need: For P1 (0,4,2,0)< (0,7,5,0) • Compare Given Allocation with Corresponding Available: For P1 (0,4,2,0) <(1,5,2,0) • So new Available= (1,5,2,0)-(0,4,2,0)=(1,1,0,0) • And so new Allocation= (1,0,0,0)+(0,4,2,0)=(1,4,2,0) • And so new need for P1=(0,7,5,0) – (0,4,2,0) =(0,3,3,0) • Now replace the previous values of process P1 with new values: Process Allocation Max Available Need A B C D A B C D A B C D A B C D P0 0 0 1 2 0 0 1 2 1 1 0 0 0 0 0 0 P1 1 4 2 0 1 7 5 0 0 3 3 0 P2 1 3 5 4 2 3 5 6 1 0 0 2 P3 0 6 3 2 0 6 5 2 0 0 2 0 P4 0 0 1 4 0 6 5 6 0 6 4 2 • Step 4: Now repeat Step1 t- Step 2 and find safe sequence.
  • 14. Bankers Algorithm Example 2 • Consider a system with five processes Po through P4 and three resource types A, B, and C. Resource type A has 10 instances, resource type B has 5 instances, and resource type C has 7 instances. Suppose that, at time T0 , the following snapshot of the system has been taken: Solution: Available instances of A = Total – Allocated = 10 – (0+2+3+2+0) = 3 Similarly, Available instances of B = Total – Allocated = 5 – (1+0+0+1+0) = 3 Finally, Available instances of C = Total – Allocated = 7 – (0+0+2+1+2) = 2 Next, Calculate Need by using the formula Need = Max – Allocation ………………………A B C Need for P0 = 7 4 3
  • 15. Bankers Algorithm Example 2 • Now find out any process whose Need can be satisfied with Available. P1 is one such process. So, Add P1 to safe sequence <P1>. • Update the Available by using formula Available = Available + Allocation of process added to safe sequence (P1) So, new Available is 5 3 2 • Next, find out another process whose Need is less than the updated Available i.e., 5 3 2. P3 is one such process. Hence, Add P3 to safe sequence <P1, P3>. • Now, Update Available by adding Allocation of P3 to current Available. • Proceed in the same manner. If all the process can be added to the safe sequence then the system is in safe state otherwise not.
  • 16. Bankers Algorithm Example 3 • Considering a system with five processes P0 through P4 and three resources of type A, B, C. Resource type A has 10 instances, B has 5 instances and type C has 7 instances. Suppose at time t0 following snapshot of the system has been taken: A) What will be the content of the Need matrix? Need [i, j] = Max [i, j] – Allocation [i, j]
  • 17. Bankers Algorithm Example 3 • Is the system in a safe state? If Yes, then what is the safe sequence?
  • 18. Bankers Algorithm Example 3 • What will happen if process P1 requests one additional instance of resource type A and two instances of resource type C?