A04 Os Exp07
A04 Os Exp07
Experiment No.07
Aim:
Write a C program to demonstrate the concept of deadlock avoidance through Banker‟s
Algorithm
Outcome:
After successful completion of this experiment students will be able to,
Implement and analyze concepts of synchronization and deadlocks.
Theory:
Banker’s Algorithm:
Bankers algorithm in Operating System is used to avoid deadlock and for resource
allocation safely to each process in the system. As the name suggests, it is mainly used in
the banking system to check whether the loan can be sanctioned to a person or not.
Bankers algorithm in OS is a combination of two main algorithms: safety algorithm (to
check whether the system is in a safe state or not) and resource request algorithm (to
check how the system behaves when a process makes a resource request).
Max - [l:X,l: Y]: Expression of the maximum number of resources of type j or process i
Allocation - [l:X,l:Y]. Indicate where process you have received a resource of type j
Need - Express how many more resources can be allocated in the future
Safety Algorithm:
1. Initialize Work = Available
Finish[i] =false for i = 0, 1, … , n— 1.
2. Check the availability status for each type of resources i:
if Need[i] <= Work && Finish[i] == false ----------- Select the process P[i] and Go
to Step 3
else ------------ Go to Step 4
3. The selected process in Step 2 is executed and resources allocated to it gets free.
Work and finish are updated.
Work = Work + Allocation[i]
Finish[i] = true
After Step 3 go to step 2.
4. If all the processes are executed in some sequence then it is said to be a safe state
if Finish[i]==true for all i, then the system is said to be in a safe state
Example:
Considering the above processing table, Calculate the need matrix? Is the system in a
safe state?
Process Need
ABC
P0 432
P1 221
P2 612
P3 844
P4 111
2. For Process P1, Need = (2, 2, 1) and Available = (3, 2, 1) Clearly, resources needed are
less than equal to the available resources within the system. Hence, request of P1 is
granted.
Available=Available+Allocation = (3, 2, 1) + (2, 1, 2) = (5, 3, 3) (New Available)
3. For Process P2, Need = (6, 1, 2) and Available = (5, 3, 3) Clearly, the resources needed
are more in number than the available ones. So, now the system will move to process the
next request.
4. For Process P3, Need = (8, 4, 4) and Available = (5, 3, 3) Clearly, the resources needed
are more in number than the available ones. So, now the system will move to process the
next request.
5. For Process P4, Need = (1, 1, 1) and Available = (5, 3, 3) Clearly, resources needed are
less than equal to the available resources within the system. Hence, request of P4 is
granted.
Available=Available+Allocation = (5, 3, 3) + (1, 1, 2) = (6, 4, 5) (New Available)
6. Now again check for Process P2, Need = (6, 1, 2) and Available = (6, 4, 5) Clearly,
resources needed are less than equal to the available resources within the system. Hence,
request of P2 is granted.
Available=Available+Allocation =(6,4,5)+(3,0,1)=(9,4,6) (NewAvailable)
7. Now again check for Process P3, Need = (8, 4, 4) and Available = (9, 4, 6) Clearly,
resources needed are less than equal to the available resources within the system. Hence,
request of P3 is granted.
Available=Available+Allocation =(9,4,6)+(0,2,0)=(9,6,6)(NewAvailable)
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
Grade:
OUTPUT:
Conclusion: The concepts of deadlock avoidance through Banker’s Algorithm and the
Dining Philosophers Problem provide valuable insights into handling concurrency and
resource allocation in computer systems.