Banker’s algorithm in operating system (OS) -Advantages – Disadvantages

What is Banker’s algorithm

Banker’s algorithm is an algorithm to avoid deadlock and to allocate resources to the processes safely. Banker’s algorithm helps the operating system to successfully share the resources among all the processes. 

Let’s discuss with an example;

Examples of bankers algorithm

Example 1

bankers algorithm Operating systems Step by step
Figure: bankers algorithm Operating systems –  Step by step

Example 2

ProcessTotal instances of Resource ATotal instances of Resource BTotal instances of Resource C
P0010
P1200
P2302
P3211
P4002

Table: Showing resources already processes occupies

Note: PCB manage the memory by keeping a record of the allocated resources and free resources.

Explanation of the table

Process P0 already has 0 resource instances of A, 1 resource instance of B, and similarly 0 resource instance of C.

Process P1 already has 2 resource instances of A, 0 resource instance of B, and similarly 0 resource instance of C.

Process P2 already have 3 resource instances of A, 0 resource instance of B, and similarly 2 resource instance of C.

Process P3 already has 2 resource instances of A, 1 resource instance of B, and similarly 1 resource instance of C.

Process P4 already have 0 resource instances of A, 0 resource instance of B, and similarly 2 resource instance of C.

ProcessTotal instances of Resource ATotal instances of Resource BTotal instances of Resource C
P0753
P1322
P2902
P3222
P4433

Table: Showing maximum resources required for complete execution.

Explanation of the table

Process P0 requires a maximum of 7 resource instances of A, 51 resource instances of B, and similarly 3 resource instances of C for the successful execution.
Process P1 requires a maximum of 3 resource instances of A, 2 resource instance of B, and similarly 2 resource instances of C for the successful execution.
Process P2 requires a maximum of 9 resource instances of A, 0 resource instance of B, and similarly 2 resource instances of C for the successful execution.
Process P3 requires a maximum of 2 resource instances of A, 2 resource instance of B, and similarly 2 resource instances of C for the successful execution.
Process P4 requires a maximum of 4 resource instances of A, 3 resource instance of B, and similarly 3 resource instances of C for the successful execution.

ProcessTotal instances of Resource ATotal instances of Resource BTotal instances of Resource C
P0743
P1122
P2600
P3011
P4431

Table: Showing needed resources to each process for execution.

We can get the needed resources with the help of the following formula;

Needed resources = maximum resources – allocated resources

 ProcessTotal instances of Resource ATotal instances of Resource BTotal instances of Resource C
P0332
P1532
P2532
P3743
P4745
P0755
P21057

Table: Showing available free resources for each process.

Now compare available free resources of each process with its needed resources. The needed matrix should less than/equal as compared to the available matrix for process execution.

First iteration:

P0=finish[p0]=False              can’t execute because if we assign free resources 

to P0, and we add the free resources of P0 with

needed resources of P0, it still does not fulfill the

the requirement of maximum resources needed to P0 for

it’s execution.

P1=FINISH[P1]=TRUE         can execute

P2=FINISH[P2]=False          can’t execute

P3=FINSH[P3]=TRUE          can execute

P4=FINISH[P4]=TRUE         can execute

2nd iteration:

P0=FINISH[P0]=TRUE          can execute

P2=FINISH[P2]=TRUE         can execute

 

Example 3

Allocated Resources

ABC
P1201
P2526
P3315
P4409

 Maximum Resources

ABC
P1354
P2928
P3575
P48610

Needed Resources

ABC
P1153
P2402
P3260
P4461

Free Resources

ABC
372

P1= fails ( as the number of needed resources are greater than the free resources)

P2=fails ( as the number of needed resources are greater than the free resources)

P3= works and executes

After the execution of P3, it releases its allocated resources as well so that the number of free resources increased.

  Free Resources

ABC
687

P4= works and executes

After the execution of P4, it releases its allocated resources to increased the number of free resources.

Free ResourceP1

ABC
10816

P1= works and executes

After the execution of P1, it releases its allocated resources to increased the number of free resources.

Free Resources

ABC
12817

P2= works and executes

After the execution of P2, it releases its allocated resources to increased the number of free resources.

Free Resources

ABC
151023

These are the free resources when all the processes execute successfully.

Click Below to Download Implementation file

Bankers algorithm implementation Operating Systems (OS)

Advantages of Banker’s algorithm

Banker’s algorithm Avoids deadlock and it is less restrictive than deadlock prevention.

Disadvantages of Banker’s algorithm

It only works with a fixed number of resources and processes.

Banker Algorithm code in C

Here, i am sharing with you the banker algorithm code in C.

Output

banker algorithm code in c

Frequently Asked Questions (FAQ)

Who developed the banker’s algorithm?
Edsger Dijkstra developed the banker’s algorithm.

Excercise

Answer the following questions using the bankers algorithm.

  • System is in safe state or not?
  • How you will apply bankers algorithm on the given data.
AllocationMaxAvailable
A B C DA B C DA B C D
P1411144147741
P171415454
P441174716
P717141444
P414747665

Subscribe for Friendship

Latest posts by Prof. Fazal Rehman Shamil (see all)

Buy advertisement space on T4Tutorials

For more details email [email protected]