You are on page 1of 5

GAGAN SHRIVASTAVA

304164
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,i,j,all[10][10],max[10][10],ned[10][10],av[10],d[10],req[10],f;
int p=0,flag=0;
char x;
clrscr();
printf("\nEnter the number of processes");
scanf("%d",&m);
printf("\nEnter number of resources");
scanf("%d",&n);
printf("\nEnter elements of allocation matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&all[i][j]);
}
}
printf("\nEnter elements of maximum matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("\nEnter the value of available matrix\n");
for(i=0;i<n;i++)
{
scanf("\t\t%d",&av[i]);
}
printf("\nThe allocation matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t\t",all[i][j]);
}
printf("\n");
}
printf("\nThe maximum matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t\t",max[i][j]);
}
printf("\n");
}
printf("\nThe available matrix is\n");
for(i=0;i<n;i++)
{
printf("\t%d",av[i]);
}

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
ned[i][j]=max[i][j]-all[i][j];
}
}
printf("\nThe need matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t\t",ned[i][j]);
}
printf("\n");
}
back1:printf("\nEnter the process to be executed\n");
scanf("%d",&f);
back:printf("\nEnter the request\n");
for(i=0;i<n;i++)
{
scanf("%d",&req[i]);
}
for(i=0;i<n;i++)
{
if(req[i]<=ned[f-1][i])
{
flag++;
}
}
if(flag!=0)
{
for(j=0;j<n;j++)
{
all[f-1][j]+=req[j];
ned[f-1][j]-=req[j];
av[j]-=req[j];
}
}
else
{
printf("\nRequest is invalid\n");
goto back;
}
for(j=0;j<n;j++)
{
if(av[j]<0)
{
p++;
}
}
if(p!=0)
printf("\nProcess could not be completed due to deadlock\n");
else
{
printf("\nModified need matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{

printf("%d\t\t",ned[i][j]);
}
printf("\n");
}
printf("\nModified available matrix is\n");
for(i=0;i<n;i++)
{
printf("%d\t\t",av[i]);
}
printf("\nModified allocated matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t\t",all[i][j]);
}
printf("\n");
}
for(i=0;i<n;i++)
{
av[i]+=all[f-1][i];
all[f-1][i]=0;
}
printf("\nAfter completion of process\n");
printf("\nModified need matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t\t",ned[i][j]);
}
printf("\n");
}
printf("\nModified available matrix is\n");
for(i=0;i<n;i++)
{
printf("%d\t\t",av[i]);
}
printf("Modified allocated matrix is");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t\t",all[i][j]);
}
printf("\n");
}
}
printf("Press y to enter another process");
x=getch();
if(x=='y')
goto back1;
getch();
}
/*
**********************OUTPUT**************************
0
0
3
0

2
2
1
1
0
0
2
Enter elements of maximum matrix
7
5
3
3
2
2
9
0
2
2
2
2
4
3
3
Enter the value of available matrix
3
3
2
The allocation matrix is
0
1
2
0
3
0
2
1
0
0

0
0
2
1
2

The maximum matrix is


7
5
3
2
9
0
2
2
4
3

3
2
2
2
3

The available matrix is


3
3
2
The need matrix is
7
4
1
2
6
0
0
1
4
3

3
2
0
1
1

Enter the process to be executed


1
Enter the request
1
1
1

Modified need matrix is


6
3
1
2
6
0
0
1
4
3
Modified available matrix is
2
2
Modified allocated matrix is
1
2
2
0
3
0
2
1
0
0

2
2
0
1
1
1
1
0
2
1
2

After completion of process


Modified need matrix is
6
3
1
2
6
0
0
1
4
3

2
2
0
1
1

Modified available matrix is


3
4
2
Modified allocated matrix is
0
0
0
2
0
0
3
0
2
2
1
1
0
0
2
Press y to enter another process
*/

You might also like