0% found this document useful (0 votes)
4 views3 pages

008 - Banker's Algorithm

The document contains a C program implementing the Banker's Algorithm for resource allocation in operating systems. It defines the allocation and maximum matrices for five processes and three resources, calculates the need matrix, and determines a safe sequence for process execution. The program outputs the safe sequence of processes if one exists.

Uploaded by

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

008 - Banker's Algorithm

The document contains a C program implementing the Banker's Algorithm for resource allocation in operating systems. It defines the allocation and maximum matrices for five processes and three resources, calculates the need matrix, and determines a safe sequence for process execution. The program outputs the safe sequence of processes if one exists.

Uploaded by

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

Name:

M.Ahsan Iqbal
Registration No:
FA20-BCS-008
Bankers Algorithms
#include<stdio.h>
#include<pthread.h>
Int main()
{
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
{ 3, 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);
}

You might also like