You are on page 1of 11

2

Operating System Laboratory


Paper Code: PCCCS403

PROJECT

Department of Computer Science&


Engineering B. Tech
2nd year 4th Semester
Name – Barun Kumar Mahato
Abhishek Mukherjee
Bubai Barman
Section - C
Class Roll. – 231 , 197 , 191
EnrollmentNo. – 22022002002051
22022002002035
22022002002029
Contents

Sl. No Topic Page No.


1 Introduction 5
2 Working 5
3 Implementation 6 - 12
4 Conclusion 13

2
Introduction: -
The Banker's algorithm is a resource allocation and deadlock avoidance algorithm used
in computer operating systems. It was developed by Edsger Dijkstra and is named after
the banking industry's loan allocation process. The algorithm is used to ensure that a
system remains in a safe state by checking the resources available and the needs of the
processes running on the system. The Banker's algorithm is based on the principle that a
process will not request more resources than it needs to complete its execution. It works
by simulating the resource allocation process and predicting future resource requests to
ensure that they will not lead to a deadlock. The algorithm is widely used in operating
systems to ensure the system's safety and prevent deadlocks, which can cause the system
to crash. The Banker's algorithm is a proven method for avoiding deadlocks in systems,
and its effectiveness has made it a valuable tool for managing resources in complex
systems. In this report, we will discuss the Banker's algorithm in detail, including its
working, advantages, and disadvantages.

Working: -
The algorithm works in the following way:
 The system initializes a data structure to keep track of the available resources, the
maximum demand of each process, and the current allocation of resources to each
process.
 When a process requests a resource, the algorithm checks if the request can be
granted by comparing the requested resources with the available resources.
 If the requested resources are less than or equal to the available resources, the
algorithm grants the request and updates the data structure.
 If the requested resources are not available, the process is put on hold until the
resources become available.
 If the request leads to an unsafe state, the request is denied, and the process is put
on hold until the resources become available.

3
Implementation: -
CASE 1:
// Banker's Algorithm
#include <stdio.h>
int main()
{
// P0, P1, P2, P3, P4 are the Process names here

int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
int alloc[5][3] = {{0, 1, 0}, // P0 // Allocation Matrix

{2, 0, 0}, // P1

{3, 0, 2}, // P2
{2, 1, 1}, // P3
{0, 0, 2}}; // P4

int max[5][3] = {{7, 5, 3}, // P0 // MAX Matrix

{3, 2, 2}, // P1

{9, 0, 2}, // P2
{2, 2, 2}, // P3
{4, 3, 3}}; // P4

int avail[3] = {3, 3, 2}; // Available Resources

int f[n], ans[n], ind = 0;


4
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];

}
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;
}
}
5
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]);
6
}
return (0);
}

Output: -

7
CASE 2:
// Banker's Algorithm
#include <stdio.h>
int main()
{
// P0, P1, P2, P3, P4 are the Process names here
int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
int alloc[5][3] = {{0, 1, 0}, // P0 // Allocation Matrix
{3, 0, 2}, // P1
{3, 0, 2}, // P2
{2, 1, 1}, // P3
{0, 0, 2}}; // P4

int max[5][3] = {{7, 5, 3}, // P0 // MAX Matrix


{3, 2, 2}, // P1
{9, 0, 2}, // P2
{2, 2, 2}, // P3
{4, 3, 3}}; // P4

int avail[3] = {3, 3, 2}; // Available Resources

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];
}
int y = 0;
for (k = 0; k < 5; k++)
{
for (i = 0; i < n; i++)
{
8
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);
}
9
Output: -

10
Conclusion: -
In conclusion, the Banker's algorithm is an essential resource allocation and deadlock
avoidance algorithm used in computer operating systems. Its primary objective is to
ensure the system's safety by checking the resources available and the needs of the
processes running on the system. The algorithm works by simulating the resource
allocation process and predicting future resource requests to prevent deadlocks from
occurring. The Banker's algorithm has several advantages, including ensuring the
system's safety, maximizing the number of processes executed simultaneously, and ease
of implementation. However, it also has some disadvantages, including the assumption
that the maximum resource needs of each process are known in advance and
computational expense in larger systems. Despite its limitations, the Banker's algorithm
is a proven method for avoiding deadlocks in systems, and its effectiveness has made it a
valuable tool for managing resources in complex systems.

11

You might also like