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

Lab5: Develop A C Program To Simulate Bankers Algorithm For Deadlock Avoidance

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)
44 views3 pages

Lab5: Develop A C Program To Simulate Bankers Algorithm For Deadlock Avoidance

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/ 3

Lab5: Develop a C program to simulate Bankers Algorithm for DeadLock Avoidance.

#include<stdio.h>
int main()
{
int n, m, i, j, k;
int allocation[10][10];
int max[10][10];
int available[10];
int need[10][10];
int finish[10];
int seq[10];

// Number of processes
printf("Enter the number of processes: ");
scanf("%d", &n);

// Number of resources
printf("Enter the number of resources: ");
scanf("%d", &m);

printf("Enter the allocation matrix:\n");


for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
scanf("%d", &allocation[i][j]);
}
}

printf("Enter the maximum matrix:\n");


for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
scanf("%d", &max[i][j]);
}
}

printf("Enter the available resources:\n");


for (i = 0; i < m; i++)
{
scanf("%d", &available[i]);
}

// Initialize finish array


for (i = 0; i < n; i++)
{
finish[i] = 0;
}
// Calculate need matrix
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
need[i][j] = max[i][j] - allocation[i][j];
}
}

int y = 0, count=0;
for (k = 0; k < n; k++)
{
for (i = 0; i < n; i++)
{
if (finish[i] == 0)
{
int flag = 0;
for (j = 0; j < m; j++)
{
if (need[i][j] > available[j])
{
flag = 1;
break;
}
}
if (flag == 0)
{
seq[count++] = i;
for (y = 0; y < m; y++)
available[y] = available[y] + allocation[i][y];
finish[i] = 1;
}
}
}
}

// Display safe sequence


int flag = 1;
for (int i = 0; i < n; i++)
{
if (finish[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 ; i++)
printf("P%d \t", seq[i]);
}
printf("\n");
return 0;
}

You might also like