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

Experiment:2.1: Write A Program To Simulate Bankers Algorithm For The Purpose of Deadlock Avoidance

The document describes an experiment on implementing the Banker's algorithm for deadlock avoidance. The Banker's algorithm tests whether allocating resources to a new process would keep the system in a safe state where all existing processes could complete their tasks. It models resource allocation and checks if allocating to the new process would leave enough available resources for the others. The document outlines the key steps of the algorithm including representing available resources, maximum/current allocation per process, and determining the safe sequence of processes. It then provides sample C++ code to simulate the Banker's algorithm.

Uploaded by

Ankit Thakur
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)
211 views6 pages

Experiment:2.1: Write A Program To Simulate Bankers Algorithm For The Purpose of Deadlock Avoidance

The document describes an experiment on implementing the Banker's algorithm for deadlock avoidance. The Banker's algorithm tests whether allocating resources to a new process would keep the system in a safe state where all existing processes could complete their tasks. It models resource allocation and checks if allocating to the new process would leave enough available resources for the others. The document outlines the key steps of the algorithm including representing available resources, maximum/current allocation per process, and determining the safe sequence of processes. It then provides sample C++ code to simulate the Banker's algorithm.

Uploaded by

Ankit Thakur
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

Course Name: Real Time System Lab Course code: CSP - 457

Experiment:2.1

A i m : - Write a program to simulate Bankers algorithm for the purpose of deadlock


avoidance

Tools/Software Required: Dev C++


Description:-

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

Banker’s algorithm is named so because it is used in banking system to check whether loan
can be sanctioned to a person or not. Suppose there are n number of account holders in a
bank and the total sum of their money is S. If a person applies for a loan then the bank first
subtracts the loan amount from the total money that bank has and if the remaining amount is
greater than S then only the loan is sanctioned. It is done because if all the account holders
come to withdraw their money then the bank can easily do it.

In other words, the bank would never allocate its money in such a way that it can no longer
satisfy the needs of all its customers. The bank would try to be in safe state always.

Pseudo code/Algorithms/Flowchart/Steps:
Suppose n is the number of processes, and m is the number of each type of resource used in a
computer system.

1. Available: It is an array of length 'm' that defines each type of resource available in the
system. When Available[j] = K, means that 'K' instances of Resources type R[j] are
available in the system.
2. Max: It is a [n x m] matrix that indicates each process P[i] can store the maximum
number of resources R[j] (each type) in a system.

Name: Amiteshvar Singh UID: 18BCS1842


Course Name: Real Time System Lab Course code: CSP - 457
3. Allocation: It is a matrix of m x n orders that indicates the type of resources currently
allocated to each process in the system. When Allocation [i, j] = K, it means that process
P[i] is currently allocated K instances of Resources type R[j] in the system.
4. Need: It is an M x N matrix sequence representing the number of remaining resources for
each process. When the Need[i] [j] = k, then process P[i] may require K more instances
of resources type Rj to complete the assigned work. Nedd[i][j] = Max[i][j] -
Allocation[i][j].
5. Finish: It is the vector of the order m. It includes a Boolean value (true/false) indicating
whether the process has been allocated to the requested resources, and all resources
have been released after finishing its task.

Implementation:
// Banker's Algorithm
#include <iostream>
using namespace std;

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

Name: Amiteshvar Singh UID: 18BCS1842


Course Name: Real Time System Lab Course code: CSP - 457
{ 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;


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

Name: Amiteshvar Singh UID: 18BCS1842


Course Name: Real Time System Lab Course code: CSP - 457
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;
}
}
}
}

Name: Amiteshvar Singh UID: 18BCS1842


Course Name: Real Time System Lab Course code: CSP - 457

int flag = 1;

// To check if sequence is safe or not


for(int i = 0;i<n;i++)
{
if(f[i]==0)
{
flag = 0;
cout << "The given sequence is not safe";
break;
}
}

if(flag==1)
{
cout << "Following is the SAFE Sequence" << endl;
for (i = 0; i < n - 1; i++)
cout << " P" << ans[i] << " ->";
cout << " P" << ans[n - 1] <<endl;
}

Name: Amiteshvar Singh UID: 18BCS1842


Course Name: Real Time System Lab Course code: CSP - 457
return (0);
}
Output:

LEARNING OUTCOMES: -

The students will be able to understand

i. The use of this algorithm

ii. Why is it named Banker’s Algorithm?

iii. Applications, Uses, Implementation.

Name: Amiteshvar Singh UID: 18BCS1842

You might also like