-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bnaker.c
72 lines (61 loc) · 1.61 KB
/
Bnaker.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
#include <stdio.h>
struct process{
int A,B,C;
};
int main()
{
int n ,A,B,C ;
printf("Enter no of process");
scanf("%d",&n);
struct process all[n];
struct process max[n];
struct process need[n];
int isexecute[n];
for(int i= 0;i<n;i++) isexecute[i]=0;
int ans[n];
for(int i =0 ;i< n;i++){
printf("Enter required and allocated resources of p %d : " ,i);
scanf("%d%d%d", &max[i].A,&max[i].B,&max[i].C);
scanf("%d%d%d", &all[i].A,&all[i].B,&all[i].C);
need[i].A = max[i].A - all[i].A;
need[i].B = max[i].B - all[i].B;
need[i].C = max[i].C - all[i].C;
}
printf("Enter available resources : ");
scanf("%d%d%d", &A,&B,&C);
int idx =0;
for(int i =0 ;i<n;i++){
for(int j=0;j<n;j++){
if(isexecute[j]==0){
if(need[j].A<=A && need[j].B<=B && need[j].C<=C){
A+= all[j].A;
B+= all[j].B;
C+= all[j].C;
ans[idx] = j;
idx++;
isexecute[j] = 1;
break;
}
}
}
}
for(int i = 0 ; i < n ; i++)
{
if(isexecute[i] == 0)
{
printf("No Safe Sequence is Possible");
return 0;
}
}
for(int i = 0 ; i < n ; i++)
{
printf("%d\t%d\t%d\t\n", need[i].A, need[i].B, need[i].C);
}
printf("The Safe Sequence is - ");
for(int i = 0 ; i < n-1 ; i++)
{
printf("P %d -> ", ans[i]);
}
printf("P %d", ans[n-1]);
return 0;
}