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

A1 1

The document is a C program that implements the Banker's Algorithm for deadlock avoidance in operating systems. It calculates the Need Matrix based on user-provided Allocation and Max matrices, checks if the system is in a safe state, and outputs a safe sequence of processes if applicable. The program prompts the user for the number of processes, resources, and their respective matrices before performing the necessary calculations.

Uploaded by

thirdeye353
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)
5 views3 pages

A1 1

The document is a C program that implements the Banker's Algorithm for deadlock avoidance in operating systems. It calculates the Need Matrix based on user-provided Allocation and Max matrices, checks if the system is in a safe state, and outputs a safe sequence of processes if applicable. The program prompts the user for the number of processes, resources, and their respective matrices before performing the necessary calculations.

Uploaded by

thirdeye353
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
You are on page 1/ 3

#include<stdio.

h>
#include<string.h>
#include<stdlib.h>

#define false 0
#define true 1

int alloc[10][10],max[10][10],need[10][10],m,n,i,j,avl[10],finish[10],req[10];

void compute_need()
{
printf("\n The Need Matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
printf("%d\t",need[i][j]);
}
printf("\n");
}
}

void accept_data(int x[10][10])


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

void accept_avl()
{
for(i=0;i<n;i++)
{
scanf_s("%d",&avl[i]);
}
}

void check_system()
{
int ans[5],cnt=0,flag=0;
for(i=0;i<m;i++)
finish[i]=false;
while(true)
{
flag=false;
for(i=0;i<m;i++)
if(finish[i]==0)
{

printf("\n Trying for P%d :->",i);


if(isfeasible(i)==1)
{
flag=true;
printf("Process P%d granted resources:",i);
finish[i]=true;
ans[cnt++]=i;

for(j=0;j<n;j++)
{
avl[j]=avl[j]+alloc[i][j];
}
}
else
{
printf("\nProcess %d can not be granted resources\
n",i);
}
}

if(flag==false)
break;

if(flag==false)
break;
}
flag=true;
for(i=0;i<m;i++)
if(finish[i]==0)
flag=false;
if(flag==1)
{
printf("\n\nSystem is in Safe State\n\n");
printf("\nSafe Sequence is as follows:");
for(i=0;i<cnt;i++)
{
printf("P%d \t",ans[i]);
}
printf("\n");
}
else
{
printf("\nSystem is not in the safe state");
}

int isfeasible(int pno)


{
int cnt=0;
for(j=0;j<n;j++)
if(need[pno][j]<=avl[j])
cnt++;
if(cnt==n)
return 1;
else
return 0;
}

main()
{
printf("\nEnter the no. of processes:-");
scanf_s("%d",&m);
printf("\nEnter the no. of resources:-");
scanf_s("%d",&n);
printf("\n Enter Allocation Matrix:");
accept_data(alloc);
printf("\nEnter the Max Matrix:");
accept_data(max);
printf("\nEnter the available:");
accept_avl();
compute_need();
check_system();
}

You might also like