Banker’s Algorithm | Operating System - Computer Science Engineering (CSE) PDF Download

Banker’s Algorithm in Operating System

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.

Why Banker’s algorithm is named so?
Banker’s algorithm is named so because it is used in banking system to check whether loan can be sanctioned to a person or not. Suppose there are n number of account holders in a bank and the total sum of their money is S. If a person applies for a loan then the bank first subtracts the loan amount from the total money that bank has and if the remaining amount is greater than S then only the loan is sanctioned. It is done because if all the account holders comes to withdraw their money then the bank can easily do it.
In other words, the bank would never allocate its money in such a way that it can no longer satisfy the needs of all its customers. The bank would try to be in safe state always.

Following Data structures are used to implement the Banker’s Algorithm:
Let ‘n’ be the number of processes in the system and ‘m’ be the number of resources types.

Available: 

  • It is a 1-d array of size ‘m’ indicating the number of available resources of each type.
  • Available[ j ] = k means there are ‘k’ instances of resource type Rj

Max:

  • It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process in a system.
  • Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource type Rj.

Allocation:

  • It is a 2-d array of size ‘n*m’ that defines the number of resources of each type currently allocated to each process.
  • Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of resource type Rj

Need:

  • It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each process.
  • Need [ i,   j ] = k means process Pi currently need ‘k’ instances of resource type Rj
  • for its execution.
  • Need [ i,   j ] = Max [ i,   j ] – Allocation [ i,   j ]

Allocationi specifies the resources currently allocated to process Pi and Needi specifies the additional resources that process Pi may still request to complete its task.
Banker’s algorithm consists of Safety algorithm and 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

Example:
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:

Banker’s Algorithm | Operating System - Computer Science Engineering (CSE)

In simple terms, it checks if allocation of any resource will lead to deadlock or not, OR is it safe to allocate a resource to a process and if not then resource is not allocated to that process. Determining a safe sequence(even if there is only 1) will assure that system will not go into deadlock.
Banker’s algorithm is generally used to find if a safe sequence exist or not. But here we will determine the total number of safe sequences and print all safe sequences.

The data structure used are: 

  • Available vector
  • Max Matrix
  • Allocation Matrix
  • Need Matrix

Example:

Input: 

Banker’s Algorithm | Operating System - Computer Science Engineering (CSE)


Banker’s Algorithm | Operating System - Computer Science Engineering (CSE)

Output: Safe sequences are:
P2--> P4--> P1--> P3
P2--> P4--> P3--> P1
P4--> P2--> P1--> P3
P4--> P2--> P3--> P1
There are total 4 safe-sequences 

Explanation: 
Total resources are R1 = 10, R2 = 5, R3 = 7 and allocated resources are R1 = (0 + 2 + 3 + 2 =) 7, R2 = (1 + 0 + 0 + 1 =) 2, R3 = (0 + 0 + 2 + 1 =) 3. Therefore, remaining resources are R1 = (10 – 7 =) 3, R2 = (5 – 2 =) 3, R3 = (7 – 3 =) 4.
Remaining available = Total resources – allocated resources
and
Remaining need = max – allocated

Banker’s Algorithm | Operating System - Computer Science Engineering (CSE)

So, we can start from either P2 or P4. We can not satisfy remaining need from available resources of either P1 or P3 in first or second attempt step of Banker’s algorithm. There are only four possible safe sequences. These are:
P2–> P4–> P1–> P3
P2–> P4–> P3–> P1
P4–> P2–> P1–> P3
P4–> P2–> P3–> P1

The document Banker’s Algorithm | Operating System - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Operating System.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
10 videos|99 docs|33 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Banker’s Algorithm - Operating System - Computer Science Engineering (CSE)

1. What is the Banker's Algorithm in Operating System?
Ans. The Banker's Algorithm is a resource allocation and deadlock avoidance algorithm used in operating systems. It is designed to ensure that the system can allocate resources to multiple processes in a safe and deadlock-free manner.
2. How does the Banker's Algorithm work?
Ans. The Banker's Algorithm works by simulating the allocation of resources to processes and checking if it leads to a safe state, i.e., a state where all processes can complete their execution without causing a deadlock. It uses a series of checks to determine if a resource request can be granted or if it would potentially lead to a deadlock.
3. What is the purpose of the Banker's Algorithm?
Ans. The main purpose of the Banker's Algorithm is to prevent deadlocks in a system by carefully managing the allocation of resources to processes. It allows the system to make informed decisions about resource allocation based on the current state and future resource requirements of each process.
4. How does the Banker's Algorithm handle resource requests?
Ans. When a process requests additional resources, the Banker's Algorithm checks if granting the request would result in a safe state. It considers the available resources, the maximum needs of other processes, and the resources currently allocated to each process. If granting the request maintains the system's safety, the resources are allocated; otherwise, the process is made to wait until it can be granted safely.
5. What are the advantages of using the Banker's Algorithm?
Ans. The advantages of using the Banker's Algorithm include: - Deadlock prevention: It ensures that resources are allocated in a way that avoids deadlocks. - Resource optimization: It allows for efficient utilization of resources by considering the needs of all processes. - Safety: It guarantees that the system will always be in a safe state, preventing the occurrence of deadlocks. - Flexibility: It can handle various resource allocation scenarios and adapt to changes in the system's resource availability.
10 videos|99 docs|33 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

mock tests for examination

,

Viva Questions

,

practice quizzes

,

shortcuts and tricks

,

MCQs

,

Banker’s Algorithm | Operating System - Computer Science Engineering (CSE)

,

Exam

,

Semester Notes

,

Sample Paper

,

Objective type Questions

,

Banker’s Algorithm | Operating System - Computer Science Engineering (CSE)

,

Free

,

video lectures

,

Important questions

,

Banker’s Algorithm | Operating System - Computer Science Engineering (CSE)

,

Previous Year Questions with Solutions

,

past year papers

,

ppt

,

Extra Questions

,

Summary

,

pdf

,

study material

;