SlideShare a Scribd company logo
bankers algorithm in operating system.pptx
Deadlocks
Definition:
Deadlock exists among a set of processes if
◦Every process is waiting for an event
◦This event can be caused only by another process in the
set
◦Event is the acquire of release of another resource
Kansas 20th century law: “When two trains approach each other at
a crossing, both shall come to a full stop and neither shall start up
again until the other has gone”
 A set of blocked processes each holding
a resource and waiting to acquire a
resource held by another process in
the set.
 Example (hardware resources):
 System has 2 tape drives.
 P1 and P2 each hold one tape drive and
each
needs another one.
 Example (“software” resource:):
 semaphores A and B, initialized to 1
P1
wait(B)
wait(A)
P0
wait (A);
wait (B);
signal(B);
signal(A);
signal(A)
signal(B)
Deadlocks are a set of blocked processes each
holding a resource and waiting to acquire a
resource held by another process.
Deadlock in
OS
 Necessary conditions for deadlock to exist:
 Mutual Exclusion
 At least one resource must be held is in non-sharable mode
 Hold and wait
 There exists a process holding a resource, and waiting for
another
 No preemption
 Resources cannot be preempted
 Circular wait
 There exists a set of processes {P1, P2, … PN}, such that
 P1 is waiting for P2, P2 for P3, …. and PN for P1
All four conditions must hold for deadlock to occur
• Truck A has to
wait for truck B to
move
• Not
deadlocke
d
• Gridlock
 When a process requests an available resource, system must
decide if immediate allocation leaves the system in a safe state.
 A system is in a safe state only if there exists a safe sequence
<P1, P2, …, Pn> for the current allocation state such that for each
Pi, the resource requests that Pi can still make 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.
 State is safe because OS can definitely avoid deadlock
 by blocking any new requests until safe order is executed
 This avoids circular wait condition
 Process waits until safe state is guaranteed
 Suppose there are 12 tape
drives max need current usage could ask for
p0 10 5 5
p1 4 2 2
p2 9 2 7
3 drives remain
 current state is safe because a safe sequence exists:
<p1,p0,p2>
p1 can complete with current
resources p0 can complete with
current+p1
p2 can complete with current +p1+p0
 if p2 requests 1 drive, then it must
wait to avoid unsafe state.
 If a system is in safe state 
no deadlocks.
 If a system is in unsafe state

possibility of deadlock.
 Avoidance  ensure that a system
will never enter an unsafe state.
bankers algorithm in operating system.pptx
Avoidance algorithms
Single instance of a resource type. Use a
resource-allocation graph
Multiple instances of a resource type.
Use the banker’s algorithm
 Suppose total bank capital is 1000 MTL
 Current cash : 1000- (410+210) = 380
MTL
Customer
c
1
c
2
Max. Need
80
0
60
0
Present Loan
410
210
Claim
39
0
39
0

Definitions
Each process has a LOAN, CLAIM,
MAXIMUM NEED
 LOAN: current number of resources
held
 MAXIMUM NEED: total
number resources needed to
complete
 CLAIM: = (MAXIMUM - LOAN)
 Establish a LOAN ceiling (MAXIMUM NEED)
for each process
 MAXIMUM NEED < total number of
resources available (ie., capital)
 Total loans for a process must be less
than or equal to MAXIMUM NEED
 Loaned resources must be returned
back in finite time
1. Search for a process with a claim that can
satisfied using the current number of remaining
resources (ie., tentatively grant the claim)
2. If such a process is found then assume that it will
return the loaned resources.
3. Update the number of remaining resources
4. Repeat steps 1-3 for all processes and mark them
 DO NOT GRANT THE CLAIM if at least
one process can not be marked.
 Implementation
 A resource request is only allowed if
it results in a SAFE state
 The system is always maintained in a
SAFE state so eventually all requests
will be filled
Banker’s Algorithm
Banker’s algorithm is a deadlock avoidance
algorithm.
Each process must priori claim it’s maximum use: it
must declare the maximum number of instances of
each resource type that it may need.
The number may not exceed the total number
of resources in the system.
When a process requests a resource it may have to
wait.
When a process gets all its resources it must return
them in a finite amount of time.
 Suppose we know the “worst case” resource needs of
processes in advance
 A bit like knowing the credit limit on your credit
cards. (This is why they call it the Banker’s
Algorithm)
 Observation: Suppose we just give some process ALL
the resources it could need…
 Then it will execute to completion.
 After which it will give back the resources.
 Like a bank: If Visa just hands you all the money your credit
lines permit, at the end of the month, you’ll pay your entire
bill, right?
 So…
 A process pre-declares its worst-case needs
 Then it asks for what it “really” needs, a little at a
time
 The algorithm decides when to grant requests
 It delays a request unless:
 It can find a sequence of processes…
 …. such that it could grant their outstanding need…
 … so they would terminate…
 … letting it collect their resources…
 … and in this way it can execute everything to
completion!
Data Structures for the Banker’s Algorithm
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.
Safety Algorithm
1.Let Work and Finish be vectors of length m and n, respectively.
Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1.
2.Find an index i such that both:
(a) Finish [i] = false
(b) Need [i] Work (ith row of Need is component-wise less
then or equal to Work)
If no such i exists, go to step 4.
3.Work = Work + Allocation [i] (component-wise addition of
Work and ith row of
Allocation)
Finish[i] = true
Put Pi to the safety sequence
go to step 2.
4.If Finish [i] = true for all i, then
the system is in a safe state.
1. If request[i] > need[i] then
error (asked for too much)
2. If request[i] > available[i]
then wait (can’t supply it
now)
3. Resources are available to
satisfy the request
Let’s assume that we satisfy
the request. Then we would
have:
available = available - request[i]
allocation[i] = allocation [i] + request[i]
need[i] = need [i] - request [i]
Now, check if this would leave us in a safe
state:
Allocation Max Available
A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
this is a safe state: safe sequence <P1, P3,
P4, P2, P0>
Suppose that P1 requests (1,0,2)
- add it to P1’s allocation and subtract it from
Available
Allocation Max Available
A B C A B C A B C
P0 0 1 0 7 5 3 2 3 0
P1 3 0 2 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
This is still safe: safe seq <P1, P3, P4, P0,
P2> In this new state,P4 requests
(3,3,0)
not enough available resources
P0 requests (0,2,0)
let’s check resulting state
Allocation Max Available
A B
C
2 1 0
A
B
C
P0 0 3 0
P1 3 0 2
P2 3 0 2
P3 2 1 1
P4 0 0 2
A B C
7 5
3
3 2
2
9 0
2
2 2
2
4 3
3
This is unsafe state (why?)
So P0’s request will be denied
Problems with Banker’s Algorithm?
 Single resource
 at each request, consider whether granting will lead
to an
unsafe state - if so, deny
 is state after the notional grant still safe?
 are there enough resources to satisfy the demands of
some process
 if so, process is notionally allowed to proceed
 in due course, it is assumed to finish and return all its
resources
 process closest to its limit is the checked, and the steps
repeated
 if all processes can eventually run to completion, state
 Multiple resources
 m types of resource, n
processes
 vector comparison :
 A  B if Ai  Bi for 0 
i  m
 Advantages
 Allows jobs to proceed when a prevention
algorithm wouldn't
 Problems
 Requires there to be a fixed number of
resources
 What happens if a resource goes down?
 Does not allow the process to change its
Maximum need while processing
 processes rarely know in advance how many resources they will
need
 the number of processes changes as time progresses
 resources once available can disappear
 the algorithm assumes processes will return their resources within
a reasonable time
 processes may only get their resources after an arbitrarily long
delay
 practical use is therefore rare!
THANK
YOU

More Related Content

PDF
PPTX
Methods for handling deadlock
PPTX
OSLec14&15(Deadlocksinopratingsystem).pptx
PPT
Chapter 7 - Deadlocks
PPT
Deadlock principles in operating systems
PPT
Principles of Operating system and types
PPTX
Banker Algorithm in operating system.pptx
PPT
Methods for handling deadlock
OSLec14&15(Deadlocksinopratingsystem).pptx
Chapter 7 - Deadlocks
Deadlock principles in operating systems
Principles of Operating system and types
Banker Algorithm in operating system.pptx

Similar to bankers algorithm in operating system.pptx (20)

PDF
Chapter 5(five).pdf
PDF
Deadlock Avoidance - OS
PPTX
Gp1242 007 oer ppt
PPTX
OS - Unit 3 Deadlock (Bankers Algorithm).pptx
PDF
CH07.pdf
PPT
PPT
Module-2Deadlock.ppt
PPTX
Deadlock and Banking Algorithm
PPTX
Deadlock avoidance and prevention .. computer networking
PPT
deadlock avoidance
PPT
Ch8 OS
 
PPT
Deadlock
PPTX
Deadlock - An Operating System Concept.pptx
PPTX
CSS430_Deadlock.pptx
PPT
14th November - Deadlock Prevention, Avoidance.ppt
PPTX
Chapter 6 - Operating System Deadlock.pptx
PDF
Deadlock
PPTX
7308346-Deadlock.pptx
PPT
Operating System
Chapter 5(five).pdf
Deadlock Avoidance - OS
Gp1242 007 oer ppt
OS - Unit 3 Deadlock (Bankers Algorithm).pptx
CH07.pdf
Module-2Deadlock.ppt
Deadlock and Banking Algorithm
Deadlock avoidance and prevention .. computer networking
deadlock avoidance
Ch8 OS
 
Deadlock
Deadlock - An Operating System Concept.pptx
CSS430_Deadlock.pptx
14th November - Deadlock Prevention, Avoidance.ppt
Chapter 6 - Operating System Deadlock.pptx
Deadlock
7308346-Deadlock.pptx
Operating System
Ad

Recently uploaded (20)

PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Construction Project Organization Group 2.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPT
Mechanical Engineering MATERIALS Selection
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Digital Logic Computer Design lecture notes
PPTX
Sustainable Sites - Green Building Construction
PPTX
Welding lecture in detail for understanding
PPTX
OOP with Java - Java Introduction (Basics)
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
CH1 Production IntroductoryConcepts.pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Construction Project Organization Group 2.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Mechanical Engineering MATERIALS Selection
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
CYBER-CRIMES AND SECURITY A guide to understanding
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Digital Logic Computer Design lecture notes
Sustainable Sites - Green Building Construction
Welding lecture in detail for understanding
OOP with Java - Java Introduction (Basics)
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Ad

bankers algorithm in operating system.pptx

  • 2. Deadlocks Definition: Deadlock exists among a set of processes if ◦Every process is waiting for an event ◦This event can be caused only by another process in the set ◦Event is the acquire of release of another resource Kansas 20th century law: “When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up again until the other has gone”
  • 3.  A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set.  Example (hardware resources):  System has 2 tape drives.  P1 and P2 each hold one tape drive and each needs another one.  Example (“software” resource:):  semaphores A and B, initialized to 1 P1 wait(B) wait(A) P0 wait (A); wait (B); signal(B); signal(A); signal(A) signal(B)
  • 4. Deadlocks are a set of blocked processes each holding a resource and waiting to acquire a resource held by another process. Deadlock in OS
  • 5.  Necessary conditions for deadlock to exist:  Mutual Exclusion  At least one resource must be held is in non-sharable mode  Hold and wait  There exists a process holding a resource, and waiting for another  No preemption  Resources cannot be preempted  Circular wait  There exists a set of processes {P1, P2, … PN}, such that  P1 is waiting for P2, P2 for P3, …. and PN for P1 All four conditions must hold for deadlock to occur
  • 6. • Truck A has to wait for truck B to move • Not deadlocke d
  • 8.  When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state.  A system is in a safe state only if there exists a safe sequence <P1, P2, …, Pn> for the current allocation state such that for each Pi, the resource requests that Pi can still make 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.
  • 9.  State is safe because OS can definitely avoid deadlock  by blocking any new requests until safe order is executed  This avoids circular wait condition  Process waits until safe state is guaranteed
  • 10.  Suppose there are 12 tape drives max need current usage could ask for p0 10 5 5 p1 4 2 2 p2 9 2 7 3 drives remain  current state is safe because a safe sequence exists: <p1,p0,p2> p1 can complete with current resources p0 can complete with current+p1 p2 can complete with current +p1+p0  if p2 requests 1 drive, then it must wait to avoid unsafe state.
  • 11.  If a system is in safe state  no deadlocks.  If a system is in unsafe state  possibility of deadlock.  Avoidance  ensure that a system will never enter an unsafe state.
  • 13. Avoidance algorithms Single instance of a resource type. Use a resource-allocation graph Multiple instances of a resource type. Use the banker’s algorithm
  • 14.  Suppose total bank capital is 1000 MTL  Current cash : 1000- (410+210) = 380 MTL Customer c 1 c 2 Max. Need 80 0 60 0 Present Loan 410 210 Claim 39 0 39 0
  • 15.  Definitions Each process has a LOAN, CLAIM, MAXIMUM NEED  LOAN: current number of resources held  MAXIMUM NEED: total number resources needed to complete  CLAIM: = (MAXIMUM - LOAN)
  • 16.  Establish a LOAN ceiling (MAXIMUM NEED) for each process  MAXIMUM NEED < total number of resources available (ie., capital)  Total loans for a process must be less than or equal to MAXIMUM NEED  Loaned resources must be returned back in finite time
  • 17. 1. Search for a process with a claim that can satisfied using the current number of remaining resources (ie., tentatively grant the claim) 2. If such a process is found then assume that it will return the loaned resources. 3. Update the number of remaining resources 4. Repeat steps 1-3 for all processes and mark them
  • 18.  DO NOT GRANT THE CLAIM if at least one process can not be marked.  Implementation  A resource request is only allowed if it results in a SAFE state  The system is always maintained in a SAFE state so eventually all requests will be filled
  • 19. Banker’s Algorithm Banker’s algorithm is a deadlock avoidance algorithm. Each process must priori claim it’s maximum use: it must declare the maximum number of instances of each resource type that it may need. The number may not exceed the total number of resources in the system. When a process requests a resource it may have to wait. When a process gets all its resources it must return them in a finite amount of time.
  • 20.  Suppose we know the “worst case” resource needs of processes in advance  A bit like knowing the credit limit on your credit cards. (This is why they call it the Banker’s Algorithm)  Observation: Suppose we just give some process ALL the resources it could need…  Then it will execute to completion.  After which it will give back the resources.  Like a bank: If Visa just hands you all the money your credit lines permit, at the end of the month, you’ll pay your entire bill, right?
  • 21.  So…  A process pre-declares its worst-case needs  Then it asks for what it “really” needs, a little at a time  The algorithm decides when to grant requests  It delays a request unless:  It can find a sequence of processes…  …. such that it could grant their outstanding need…  … so they would terminate…  … letting it collect their resources…  … and in this way it can execute everything to completion!
  • 22. Data Structures for the Banker’s Algorithm 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.
  • 23. Safety Algorithm 1.Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish [i] = false for i = 0, 1, …, n- 1. 2.Find an index i such that both: (a) Finish [i] = false (b) Need [i] Work (ith row of Need is component-wise less then or equal to Work) If no such i exists, go to step 4. 3.Work = Work + Allocation [i] (component-wise addition of Work and ith row of Allocation) Finish[i] = true Put Pi to the safety sequence go to step 2. 4.If Finish [i] = true for all i, then the system is in a safe state.
  • 24. 1. If request[i] > need[i] then error (asked for too much) 2. If request[i] > available[i] then wait (can’t supply it now) 3. Resources are available to satisfy the request Let’s assume that we satisfy the request. Then we would have: available = available - request[i] allocation[i] = allocation [i] + request[i] need[i] = need [i] - request [i] Now, check if this would leave us in a safe state:
  • 25. Allocation Max Available A B C A B C A B C P0 0 1 0 7 5 3 3 3 2 P1 2 0 0 3 2 2 P2 3 0 2 9 0 2 P3 2 1 1 2 2 2 P4 0 0 2 4 3 3 this is a safe state: safe sequence <P1, P3, P4, P2, P0> Suppose that P1 requests (1,0,2) - add it to P1’s allocation and subtract it from Available
  • 26. Allocation Max Available A B C A B C A B C P0 0 1 0 7 5 3 2 3 0 P1 3 0 2 3 2 2 P2 3 0 2 9 0 2 P3 2 1 1 2 2 2 P4 0 0 2 4 3 3 This is still safe: safe seq <P1, P3, P4, P0, P2> In this new state,P4 requests (3,3,0) not enough available resources P0 requests (0,2,0) let’s check resulting state
  • 27. Allocation Max Available A B C 2 1 0 A B C P0 0 3 0 P1 3 0 2 P2 3 0 2 P3 2 1 1 P4 0 0 2 A B C 7 5 3 3 2 2 9 0 2 2 2 2 4 3 3 This is unsafe state (why?) So P0’s request will be denied Problems with Banker’s Algorithm?
  • 28.  Single resource  at each request, consider whether granting will lead to an unsafe state - if so, deny  is state after the notional grant still safe?  are there enough resources to satisfy the demands of some process  if so, process is notionally allowed to proceed  in due course, it is assumed to finish and return all its resources  process closest to its limit is the checked, and the steps repeated  if all processes can eventually run to completion, state
  • 29.  Multiple resources  m types of resource, n processes  vector comparison :  A  B if Ai  Bi for 0  i  m
  • 30.  Advantages  Allows jobs to proceed when a prevention algorithm wouldn't  Problems  Requires there to be a fixed number of resources  What happens if a resource goes down?  Does not allow the process to change its Maximum need while processing
  • 31.  processes rarely know in advance how many resources they will need  the number of processes changes as time progresses  resources once available can disappear  the algorithm assumes processes will return their resources within a reasonable time  processes may only get their resources after an arbitrarily long delay  practical use is therefore rare!