master Banker's Algorithm with solved question + PYQs

Here are 17 questions on Bankers algorithms . Each question is presented in a table format. each question is solved in explained form.

Jump To Question

What Is Banker's Algorithm in Operating System ?

there are three common approaches to dealing with deadlock in operating system

  1. prevention
  2. detection
  3. avoidance

The Banker's Algorithm is a resource allocation and deadlock avoidance algorithm that tests whether the system can allocate resources to each process in a safe sequence.

Why is it Called "Banker's Algorithm" ?

The name “Banker’s Algorithm” comes from a real-world banking scenario that was explained by the famous computer scientist Edsger Dijkstra.

  1. A bank has a fixed amount of money (cash).
  2. Customers (processes) can request loans (resources).
  3. Each customer has a maximum limit (how much loan they might request in total).

The bank wants to ensure that it never gets into a situation where it can't fulfill the needs of any of its customers — even in the worst case.

or

The bank must ensure that it never gives out too much money, or it might be unable to fulfill future requests.because If a customer later needs more money, but the bank has already loaned out most of its cash, it might not be able to help — this is a dangerous or unsafe situation. –––> this is similar to a deadlock.

So The Bank Must Avoid a Dangerous or unsafe Situation and to avoid dangerous situations, the bank only gives a loan if it can still safely meet the possible future needs of all other customers.

The algorithm is called the “Banker’s Algorithm” because it behaves like a cautious banker (a banker never gives out loans without keeping enough cash to support all customers). Similarly, the OS simulates whether it can still fulfill the maximum demands of all other processes before granting a request and only allocates resources if it is safe to do so.This method helps the OS avoid deadlock, and keep the system running smoothly

Banker's Algorithm in Operating System Terms

Now we will understand in detail how Bankers algorithm works in the operating system

  1. In an OS, resources refer to things like: CPU time (processor execution time), Memory (RAM), Disk space, Input/Output devices (like printers, scanners), Files or semaphores These are limited in number, and the OS has to allocate them wisely among processes.
  2. Before a process begins execution, it tells the OS the maximum number of each type of resource it might need.
    🔸 This is like a customer telling the bank: “The most loan I might ever need is ₹10,000.”
    This information allows the OS to plan ahead and avoid allocating resources that could cause future deadlocks.
  3. The OS maintains a list of how many instances of each resource are currently available.
    Example: 5 printers, 3 CPUs, 10 memory blocks free.
    When a process makes a request, the OS first checks if the requested resources are available right now.
  4. It then looks at the remaining needs (i.e., how much more each process might request later) by subtracting:
    Maximum need − Currently allocated
    This helps the OS understand the worst-case demands of all processes in the system.
  5. Before actually giving the resources, the OS performs a simulation:
    • It temporarily pretends that the resources have been given.
    • Then it checks: Can all processes still finish eventually, one after the other, if needed?
    This is known as the "safety check".
  6. If the simulation shows that the system will remain safe then, The OS grants the resource request and The process continues its work.
  7. But if the system might go into an unsafe state (possible future deadlock) then , The OS denies the request, even if the resources are currently free.

What are the Method to Solve Banker's Algorithm ?

  • Step 1: Identify the available resources and maximum demand of each process.
  • Step 2: Calculate the "Need" matrix by subtracting the allocated resources from the maximum demand for each process.
  • Step 3: Check if the system has a safe state by allocating resources based on the "Need" matrix while ensuring the system remains in a safe state.
  • Step 4: Allocate resources to processes only if they satisfy the "Need" matrix and resources are available.
  • Step 5: If a safe sequence is found, allocate resources; if not, resources remain unallocated to prevent deadlock.

What are the Advantages and Disadvantages of Banker's Algorithm

Advantages Disadvantages
Effective in preventing deadlocks Works only with known resources and maximum demand
Ensures the system remains in a safe state Complex and time-consuming calculations
Prevents the starvation of processes Not suitable for systems with unpredictable resources
Maintains system stability and availability Requires significant memory and CPU time
Identifies safe and unsafe states Inapplicable to real-time systems
Applicable in systems with fixed resources Does not allow resource sharing flexibility

Why Banker's Algorithm is Better than Other deadlock avoidance Algorithms?

Banker's Algorithm is better than other deadlock prevention algorithms as it dynamically checks for safe and unsafe states based on each process's resource needs, ensuring efficient resource utilization. Unlike static resource allocation methods, it provides a safe sequence, preventing deadlock while allowing processes to request resources flexibly. This adaptability makes it effective in multi-process systems with stable resources.

question 1 : Answer the question using banker's algorithms.
consider the following snapshot(Available in table form) of a system:

Available
R1 R2 R3 R4
0 2 1 1
Process Current Allocation Maximum Demand
R1 R2 R3 R4 R1 R2 R3 R4
P1 2 0 1 2 2 1 1 3
P2 2 1 2 0 2 4 3 2
P3 1 0 3 3 4 2 4 3
P4 2 3 4 3 4 3 5 4
P5 1 3 3 2 2 5 4 2
P6 0 1 1 0 1 2 1 1

Is the system is in Safe state? If yes, then find out the safe sequences.

solution :

All Formulas

Resources = Available + Σ (allocation)

Available = Resources - Σ (allocation)

Need = maximum - Allocation

It is in safe state
the safe sequences are :

after assigning the resources to any process we do this calculation for finding the available resources

available = available + Allocation[process that you assign]

or

[new available resources] = [available resource before assigning] + [value of process at allocation time].

we assign the available resources to P1 then available resource is

new available resources = (0,2,1,1) + (2,0,1,2) = (2,2,2,3)


bankers algorithms 1st question solution

after assigning the resources to process P1 we do this calculation

available = available + Allocation[P1] = (0,2,1,1) + (2,0,1,2) = (2,2,2,3)


bankers algorithms 1st question solution

bankers algorithms 1st question solution

bankers algorithms 1st question solution

one of the safe sequences is :- P1 > P4 > P2 > P3 > P5 > P6

Question 2 : consider the following snapshot(Available in table form) of a system:

Available
R1 R2 R3 R4
3 1 1 1
Process Current Allocation Maximum Demand
R1 R2 R3 R4 R1 R2 R3 R4
P1 1 0 1 2 1 3 3 3
P2 1 3 0 0 2 4 3 0
P3 2 0 3 4 4 3 5 6
P4 2 1 2 2 4 1 2 3
P5 0 3 3 2 0 6 5 2
P6 1 2 4 4 4 3 5 4

Answer the following question using the Banker's algorithm :

(a) Is the system in a safe state ?

(b) If a request from process p3 arrives for (0, 1, 1, 0), can the request be granted immediately?

Previous Year Question (PYQs) of Bankers Algorithms

Previous Year class test question from session 2022-24 on bankers algorithms in operating system

Previous Year class test question from session 2023-25 on bankers algorithms in operating system

Previous Year class test question from session 2024-26 on bankers algorithms in operating system

List of books that help you learn the Bankers algorithm question

List of books that help you learn the Bankers algorithm question