-
Notifications
You must be signed in to change notification settings - Fork 0
/
bankers
136 lines (134 loc) · 2.3 KB
/
bankers
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include<stdio.h>
struct pro{
int all[10],max[10],need[10];
int flag;
};
int i,j,pno,r,nr,id,k=0,safe=0,exec,count=0,wait=0,max_err=0;
struct pro p[10];
int aval[10],seq[10];
void safeState()
{
while(count!=pno){
safe = 0;
for(i=0;i<pno;i++){
if(p[i].flag){
exec = r;
for(j=0;j<r;j++)
{
if(p[i].need[j]>aval[j]){
exec =0;
}
}
if(exec == r){
for(j=0;j<r;j++){
aval[j]+=p[i].all[j];
}
p[i].flag = 0;
seq[k++] = i;
safe = 1;
count++;
}
}
}
if(!safe)
{
printf("System is in Unsafe State\n");
break;
}
}
if(safe){
printf("\n\nSystem is in safestate \n");
printf("Safe State Sequence \n");
for(i=0;i<k;i++)
printf("P[%d] ",seq[i]);
printf("\n\n");
}
}
void reqRes(){
printf("\nRequest for new Resourses");
printf("\nProcess id ? ");
scanf("%d",&id);
printf("Enter new Request details ");
for(i=0;i<r;i++){
scanf("%d",&nr);
if( nr <= p[id].need[i])
{
if( nr <= aval[i]){
aval[i] -= nr;
p[id].all[i] += nr;
p[id].need[i] -= nr;
}
else
wait = 1;
}
else
max_err = 1;
}
if(!max_err && !wait)
safeState();
else if(max_err){
printf("\nProcess has exceeded its maximum usage \n");
}
else{
printf("\nProcess need to wait\n");
}
}
void main()
{
printf("Enter no of process ");
scanf("%d",&pno);
printf("Enter no. of resourses ");
scanf("%d",&r);
printf("Enter Available Resourse of each type ");
for(i=0;i<r;i++){
scanf("%d",&aval[i]);
}
printf("\n\n---Resourse Details---");
for(i=0;i<pno;i++){
printf("\nResourses for process %d\n",i);
printf("\nAllocation Matrix\n");
for(j=0;j<r;j++){
scanf("%d",&p[i].all[j]);
}
printf("Maximum Resourse Request \n");
for(j=0;j<r;j++){
scanf("%d",&p[i].max[j]);
}
p[i].flag = 1;
}
// Calcualting need
for(i=0;i<pno;i++){
for(j=0;j<r;j++){
p[i].need[j] = p[i].max[j] - p[i].all[j];
}
}
//Print Current Details
printf("\nProcess Details\n");
printf("Pid\t\tAllocattion\t\tMax\t\tNeed\n");
for(i=0;i<pno;i++)
{
printf("%d\t\t",i);
for(j=0;j<r;j++){
printf("%d ",p[i].all[j]);
}
printf("\t\t");
for(j=0;j<r;j++){
printf("%d ",p[i].max[j]);
}
printf("\t\t");
for(j=0;j<r;j++){
printf("%d ",p[i].need[j]);
}
printf("\n");
}
//Determine Current State in Safe State
safeState();
int ch=1;
do{
printf("Request new resourse ?[0/1] :");
scanf("%d",&ch);
if(ch)
reqRes();
}while(ch!=0);
//end:printf("\n");
}