0% found this document useful (0 votes)
32 views6 pages

A04 Os Exp07

The document outlines an experiment for demonstrating deadlock avoidance using the Banker's Algorithm in a C program. It explains the theory behind the algorithm, including the safety and resource request algorithms, and provides a practical example with a need matrix calculation. The conclusion emphasizes the importance of these concepts in managing concurrency and resource allocation in operating systems.

Uploaded by

muddubhai17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

A04 Os Exp07

The document outlines an experiment for demonstrating deadlock avoidance using the Banker's Algorithm in a C program. It explains the theory behind the algorithm, including the safety and resource request algorithms, and provides a practical example with a need matrix calculation. The conclusion emphasizes the importance of these concepts in managing concurrency and resource allocation in operating systems.

Uploaded by

muddubhai17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Operating System Lab 2025

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).

Data Structure required:


X: Indicates the total number of processes of the system.
Y: Indicates the total number of resources present in the system.

Available - [I: Y] indicate which resource is available.

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

Resource Request Algorithm


1. Check if the request made is valid or not.
if Request[i] <= Need ------------ Request is Valid then go to Step 2
else ------------ Abort
2. Check if the number of requested instances of each resource is less than the available
resources
if Request[i] <= Available -------- Go to Step 3
else --------- process P[i] has to wait till sufficient resources are available
3. Now, the algorithm assumes that the resources have been allocated and modifies the
data structure Available = Available – Request[i]
Allocation[i] = Allocation[i] + Request[i]

TEC[Artificial Intelligence and Data Science] Page 1


Operating System Lab 2025
Need[i] = Need[i] – Request[i]

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

Characteristics of Banker’s Algorithm


 Keep many resources that satisfy the requirement of at least one client
 Whenever a process gets all its resources, it needs to return them in a restricted
period.
 When a process requests a resource, it needs to wait
 The system has a limited number of resources
 Advance feature for max resource allocation

Disadvantage of Banker’s algorithm


 Does not allow the process to change its Maximum need while processing
 It allows all requests to be granted in restricted time, but one year is a fixed period for
that.
 All processes must know and state their maximum resource needs in advance.

Example:

Processes Allocation Max Available


ABC ABC ABC
P0 112 544 321
P1 212 433
P2 301 913
P3 020 864
P4 112 223

Considering the above processing table, Calculate the need matrix? Is the system in a
safe state?

TEC[Artificial Intelligence and Data Science] Page 2


Operating System Lab 2025
We can easily calculate the entries of need matrix using the formula:
(Need)i=(Max)i−(Allocation)i

Process Need
ABC
P0 432
P1 221
P2 612
P3 844
P4 111

Let us check for safe sequence:


1. For process P0, Need = (4, 3, 2) and Available = (3, 2, 1) Clearly, the resources
needed are more in number than the available ones. So, now the system will move to
process the next request.

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)

TEC[Artificial Intelligence and Data Science] Page 3


Operating System Lab 2025
8. Now again check for Process P0, Need = (4, 3, 2), and Available (9, 6, 6). Clearly, the
request for P0 is also granted.
Safe sequence: < P1,P4,P2,P3,P0>

PART B
(PART B : TO BE COMPLETED BY STUDENTS)

Roll. No : 04 Name: Raihan Shaikh

Class: (SE AI&DS) Batch: A-1

Date of Experiment: 03-03-25 Date of Submission: 20-3-25

Grade:

B.1 Software Code written by student:


#include <stdio.h>
int main()
{
int n, m, i, j, k;
n = 5;
m = 3;
int alloc[5][3] = { { 0, 1, 0 },
{ 2, 0, 0 },
{ 3, 0, 2 },
{ 2, 1, 1 },
{ 0, 0, 2 } };
int max[5][3] = { { 7, 5, 3 },
{3, 2, 2 },
{ 9, 0, 2 },
{ 2, 2, 2 },
{ 4, 3, 3 } };
int avail[3] = { 3, 3, 2 };
int f[n], ans[n], ind = 0;
for (k = 0; k < n; k++)
{
f[k] = 0;
}
int need[n][m];
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}

TEC[Artificial Intelligence and Data Science] Page 4


Operating System Lab 2025
int y = 0;
for (k = 0; k < 5; k++)
{
for (i = 0; i < n; i++)
{
if (f[i] == 0)
{
int flag = 0;
for (j = 0; j < m; j++)
{
if (need[i][j] > avail[j])
{
flag = 1;
break;
}
}
if (flag == 0)
{
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}
int flag = 1;
for(int i=0;i<n;i++)
{
if(f[i]==0)
{
flag=0;
printf("The following system is not safe");
break;
}
}
if(flag==1)
{
printf("Following is the SAFE Sequence\n");
for (i = 0; i < n - 1; i++)
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);
}
return (0);
}

TEC[Artificial Intelligence and Data Science] Page 5


Operating System Lab 2025

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.

TEC[Artificial Intelligence and Data Science] Page 6

You might also like