Banker's Algorithm
Goal:- C program For Banker's Algorithm.
Method:- Simple Rules Of Banker Algorithm.
Explanation:- Banker's Algorithm is a deadlock avoidance algorithm that checks for safe or unsafe state of a System after allocating resources to a process.
When a new process enters into system ,it must declare maximum no. of instances of each resource that it may need.After requesting operating system run banker's algorithm to check whether after allocating requested resources,system goes into deadlock state or not. If yes then it will deny the request of resources made by process else it allocate resources to that process.
No. of requested resources (instances of each resource) may not exceed no. of available resources in operating system and when a process completes it must release all the requested and already allocated resources.
For implementing Banker's algorithm we should have pre-knowledge about three things:-
- How many instance of each resource a process could request. (Max)
- How many instance of each resource is already allocated to that process(Allocated)
- How many instance of each resource is already available(Available).
We can calculate need of each process from above information:-
Need=Max - Allocated.
If need<=available then request will be accepted otherwise it is denied and it will check for next process in waiting queue.(See Life Cycle Of a Process)
Safe or Unsafe State:- A system is in Safe state if its all process finish its execution or if any process is unable to aquire its all requested resources then system will be in Unsafe state.
Example:- To understand banker's algorithm,we consider a system with four processes P1 through P4 and three resource type A,B and C.Resource type has 10 instances,B has 5 instances and C has total 7 instances.
Current Scenario:-
Table-1 |
Need[P2] < Available..So P2 we allocate resources to P2. After Completion of P2 ,Scenario...
Table-2 |
Now Need[P4] < Available so P4 will complete its execution .After that--
Table-3 |
So P1 will execute and after that P3 will take resources.
Safe Sequence is :- P2 , P4, P1,P3 (we can obtain one more sequence if we allocate resources to P3 before P1 .)
Program:-
Output:-
If you want to share more information about topic discussed above or find anything incorrect Please do comments.
For more Programs Stay Connected..Like us on Facebook.
This code is not accurate.The code for safe sequence is not working properly for 3 processes
ReplyDelete