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

C Program for Bankers Algorithm Simulation

This document contains a C program that implements the Banker's Algorithm for deadlock avoidance in operating systems. It prompts the user to input the number of processes, resources, allocation matrix, maximum matrix, and available resources, then calculates the NEED matrix and determines a safe sequence of process execution. The program outputs the safe sequence if one exists, ensuring that resources are allocated without leading to deadlock.

Uploaded by

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

C Program for Bankers Algorithm Simulation

This document contains a C program that implements the Banker's Algorithm for deadlock avoidance in operating systems. It prompts the user to input the number of processes, resources, allocation matrix, maximum matrix, and available resources, then calculates the NEED matrix and determines a safe sequence of process execution. The program outputs the safe sequence if one exists, ensuring that resources are allocated without leading to deadlock.

Uploaded by

sn0385313
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Program-5

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


#include <stdio.h>
int main()
{

int n, m, i, j, k, y,alloc[20][20],max[20][20],avail[50],ind=0;

printf("Enter the no of Proceses:");

scanf("%d",&n);

printf("Enter the no of Resources:");

scanf("%d",&m);

printf("Enter the Allocation Matrix:");

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

for (j = 0; j < m; j++)

scanf("%d",&alloc[i][j]);

printf("Enter the Max Matrix:");

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

for (j = 0; j < m; j++)

scanf("%d",&max[i][j]);

printf("Enter the Available Matrix");

for(i=0;i<m;i++)

scanf("%d",&avail[i]);

int finish[n], safesequence[n],work[m],need[n][m];

//calculating NEED matrix

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

for (j = 0; j < m; j++)

need[i][j] = max[i][j] - alloc[i][j];

printf("NEED matrix is");

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

{
printf("\n");

for (j = 0; j < m; j++)

printf(" %d ",need[i][j]);

for(i=0;i<m;i++)

work[i]=avail[i];

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

finish[i] = 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] > work[j])

flag = 1;

break;

}
}

if (flag == 0) {

safesequence[ind++] = i;

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

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

finish[i] = 1;
}

printf("\nFollowing is the SAFE Sequence\n");

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

printf(" P%d ", safesequence[i]);

You might also like