-
Notifications
You must be signed in to change notification settings - Fork 0
/
bankersalgorithm.c
87 lines (76 loc) · 2.11 KB
/
bankersalgorithm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
void main()
{
int numberofresources,numberofrows;
int allocated[10][5],maximum[10][5],available[5],need[10][5];
int flag,done[10];
printf("\nEnter the number of resources:\n");
scanf("%d",&numberofresources);
printf("Enter the number of rows that you wish to enter:\n");
scanf("%d",&numberofrows);
printf("Enter the allocated matrix in order:\n");
for(int i=0;i<numberofrows;i++)
{
done[i]=0;
for(int j=0;j<numberofresources;j++)
{
scanf("%d",&allocated[i][j]);
}
}
printf("Enter the maximum matrix in order:\n");
for(int i=0;i<numberofrows;i++)
{
for(int j=0;j<numberofresources;j++)
{
scanf("%d",&maximum[i][j]);
}
}
printf("Enter the available matrix:\n");
for(int i=0;i<numberofresources;i++)
{
scanf("%d",&available[i]);
}
printf("\n\nNeed Matrix:\n");
for(int i=0;i<numberofrows;i++)
{
for(int j=0;j<numberofresources;j++)
{
need[i][j]=maximum[i][j]-allocated[i][j];
printf("%d ",need[i][j]);
}
printf("\n");
}
for(int l=0;l<numberofrows;l++)
{
for(int i=0;i<numberofrows;i++)
{
flag=0;
if(done[i]==0)
{
for(int j=0;j<numberofresources;j++)
{
if(need[i][j] <= available[j])
{
flag++;
}
}
if(flag==numberofresources)
{
printf("Process P[%d] completed\n",i);
for(int k=0;k<numberofresources;k++)
{
available[k] = available[k] + allocated[i][k];
}
done[i]=1;
}
}
}
}
for(int i=0;i<numberofrows;i++)
{
if(done[i]==0)
{
printf("Deadlock occured at P[%d]\n",i);
}
}
}