Banker's Algorithm in operating system |Deadlock Avoidance|LearnTopicWise




Banker’s Algorithm in Operating System- Deadlock avoidance

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 a “safe-state” check to test for possible activities, before deciding whether allocation should be allowed to continue.

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.

 ProcessAllocation Max Need Available
 A   B   C A   B   C A    B   C 
 P10    1    07    5    3  3    3    2
 P22    0    0 3    2    2  
 P33    0    2 9    0    2  
 P42    1    14    2    2  
 P50    0    25    3    3  

Solution-

Now we solve the above-given question with the help of  "Banker Algorithm" that is used for "Deadlock Avoidance".

Step-1   First, we find the "Need Matrix" OR "Remaining Need Matrix" for finding the need matrix, we shall subtract Max Need from the Allocation. 

The formula is-  
          Remaining Need Matrix= Max Need - Allocation



Step-2   Second, Now we find remaining "Available" need for process P1, P2, P3, P4, For this, we will check the following condition.

Condition-  
  
If ( Remaining Need<= Available Need)
 {
     printf(Available Need + Allocation);
  }
  else
  {
     Printf("Check next process");
   }

 


Now we will check condition -   

 If(743<=332)
 {
    Printf(332+010);
  }
  else
 {
    printf("Check next process");
  }

The above condition is false so we will check the next process, the table will be the same as you can see below.

 
  Now again we will check condition -   

 If(122<=332)
 {
    Printf(332+200);
  }
  else
 {
    printf("Check next process");
  }
          
 Now you can see that the above condition is true so we will print or write the addition of (332+200) into the 4th column and 4th row that name is "Available", the table will be changed as you can see below.



Now available need become 532, So we will compare newly available need from the 
remaining need in forwarding direction.

Now again we will check condition -   

 If(600<=532)
 {
    Printf(532+302);
  }
  else
 {
    printf("Check next process");
  }
               
Condition is false, check the next process the table will be the same no change is required as you can see below.



 Now again we will check condition -    

If(211<=532)
 {
    Printf(532+211);
  }
  else
 {
    printf("Check next process");
  }

Condition is true, write the addition of (532+211) into the "Available" column and the table will be changed as you can see below.

  
The newly Available Need is 743.

 Now again we will check condition -  

If(531<=743)
 {
    Printf(743+002);
  }
  else
 {
    printf("Check next process");
  }           

Condition is true, Printf(743+002)
 



The newly Available Need is 745.

 Now again we will check condition -   

If(743<=745)
 {
    Printf(745+010);
  }
  else
 {
    printf("Check next process");
  }                 

 Condition is true,  Printf(745+010)

  


The newly Available Need is 755.

Now we check remaining process P3 because all process enters in safe sequence except P1.

 Now again we will check condition -   

If(600<=755)
 {
    Printf(755+302);
  }
  else
 {
    printf("Check next process");
  }                 
         
 Condition is true,   Printf(755+302)



Here the safe sequence is-  <P2, P4, P5, P1, P3>

Banker's algorithm is also known as a Safety algorithmBanker’s algorithm is named so because it is used in the banking system.

Comments

Post a Comment

If you have any doubt relating to this topic please tell us.

Popular posts from this blog

Cybersecurity|| Protect Your Computing Devices From Viruses, Hackers, Spies|| LearnTopicWise

Cybersecurity|| How to crack Wifi password?|| Techniques of cracking WiFi password|| LearnTopicWise

Stopping By Woods On A Snowy Evening- Robert Frost || Lesson- 9 Poetry || Class- 12 English || UP-BOARD || LearnTopicWise

Cybersecurity|| What is meant by your data?|| Explain different types of data|| LearnTopicWise