Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions bankersalgorithm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//C program for Banker's Algorithm

//https://github.com/shubham-01-star
I
#include <stdio.h>

int main()

{

// P0, P1, P2, P3, P4 are the names of Process

int n, r, i, j, k;

n = 5; // Indicates the Number of processes

r = 3; //Indicates the Number of resources

int alloc[5][3] = { { 0, 0, 1 }, // P0 // This is Allocation Matrix

{ 5, 0, 0 }, // P1

{ 1, 0, 1 }, // P2

{ 2, 3, 2 }, // P3

{ 0, 0, 3 } }; // P4

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

{ 3, 2, 2 }, // P1

{ 8, 0, 2 }, // P2

{ 2, 1, 2 }, // P3

{ 5, 2, 3 } }; // P4

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

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

for (k = 0; k < n; k++) {

f[k] = 0;

}

int need[n][r];

for (i = 0; i < n; i++) {

for (j = 0; j < r; 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 < r; j++) {

if (need[i][j] > avail[j]){

flag = 1;

break;

}

}

if (flag == 0) {

ans[ind++] = i;

for (y = 0; y < r; y++)

avail[y] += alloc[i][y];

f[i] = 1;

}

}

}

}

printf("Th SAFE Sequence is as follows\n");

for (i = 0; i < n - 1; i++)

printf(" P%d ->", ans[i]);

printf(" P%d", ans[n - 1]);

return (0);

}