-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGiftsshop.c
259 lines (206 loc) · 5.15 KB
/
Giftsshop.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include<stdio.h>
#include<stdlib.h>
#define M_size 10 // Maximum Size Limit
int N; // Number Of Processess
// A Structure Consists of :-
// Pid :- Name Of The Process or the process id
// atime :- Arrival Time
// btime :- Burest Time
// ftime :- Finished Time
// stime :- Starting Time
// wtime :- Waiting Time
// trtime :- Total TurnAround Time
struct data {
int num;
char pid[5];
int atime;
int btime;
int rtime;
int ftime;
int stime;
int wtime;
int trtime;
};
// Variables Which Are Usefull For The P_que
// P_front :- Priority Queue Front
// P_rwar :- Priority Queue Rear
int P_front = -1, P_rear = -1;
// Declaration of Array of Structures
struct data P_que[M_size];
// check is a function which put data into the desire position
void check(struct data x) {
int i, j;
for (i = 0; i <= P_rear; i++) {
if (x.btime > P_que[i].btiem) {
for (j = P_rear + 1; j > i; j--) {
P_que[j] = P_que[j - 1];
}
P_que[i] = x;
return;
}
}
P_que[i] = x;
}
// PQ_push is a function which push data into the Priority Queue
void PQ_push(struct data x) {
if (P_front == -1 && P_rear == -1) {
P_front++;
P_rear++;
P_que[P_rear] = x;
return;
}
else {
check(x);
}
P_rear++;
}
// PQ_pop is a function which pop out the data P_frontom Priority Queue
void PQ_pop() {
int i;
if (P_front == -1 && P_rear == -1) {
return;
}
for (i = 0; i < P_rear; i++) {
P_que[i] = P_que[i + 1];
}
P_rear--;
if (P_rear == -1)
P_front = -1;
}
// PQ_empty is a function which tell Priority Queue is empty or not
int PQ_empty() {
return ((P_front == -1 && P_rear == -1));
}
// PQ_top is a function which returns the top of the Priority Queue
struct data PQ_top() {
return P_que[P_front];
}
int PQ_size() {
return P_rear - P_front;
}
void calc(struct data p[], int g[], int n) {
int i, j;
float Avg_W_time = 0, Avg_TR_time = 0;
for (i = 0; i < N; i++) {
for (j = n; j >= 0; j--) {
if (g[j] == p[i].num) {
p[i].ftime = j + 1;
break;
}
}
}
for (i = 0; i < N; i++) {
for (j = 0; j < n; j++) {
if (g[j] == p[i].num) {
p[i].stime = j;
break;
}
}
}
for (i = 0; i < N; i++) {
p[i].wtime = p[i].ftime - p[i].atime - p[i].btime;
p[i].trtime = p[i].wtime + p[i].btime;
Avg_W_time += p[i].wtime;
Avg_TR_time += p[i].trtime;
}
printf("Id \t ArrivalTime \t BurestTime \t WaitingTime \t TurnAroundTime \n");
for (i = 0; i < N; i++) {
printf("%d \t %d \t\t %d \t\t %d \t\t %d \n", p[i].num, p[i].atime, p[i].btime, p[i].wtime, p[i].trtime);
}
Avg_W_time /= N;
Avg_TR_time /= N;
printf("\n\n");
printf("Average Waiting Time And Average Turn Around Time \n\n");
printf("%f %f", Avg_W_time, Avg_TR_time);
printf("\n");
}
void sort1(struct data p[]) {
int i, j, mn;
struct data tmp;
for (i = 0; i < N - 1; i++) {
mn = i;
for (j = i + 1; j < N; j++) {
if (p[j].num < p[mn].num)
mn = j;
}
tmp = p[i];
p[i] = p[mn];
p[mn] = tmp;
}
}
void LRT(struct data p[]) {
int tt = 0;
tt += p[0].atime + p[0].btime;
for (int i = 1; i < N; i++) {
if (tt < p[i].atime)
tt = p[i].atime;
tt += p[i].btime;
}
int gant[tt], status = 0;
for (int i = 0; i < tt; i++)
gant[i] = -1;
struct data present;
for (int i = 0 ;i < tt; i++) {
for (int j = 0; j < N; j++) {
if (i == p[j].atime) {
PQ_push(p[j]);
}
}
if (status == 0) {
if (!PQ_empty()) {
present = PQ_top();
PQ_pop();
status = 1;
}
}
if (status == 1) {
present.btime--;
gant[i] = present.num;
if (present.btime == 0) {
status = 0;
}
}
}
for (int i = 0; i < tt; i++)
printf("%d ", gant[i]);
printf("\n");
sort1(p);
calc(p, gant, tt);
}
void sort(struct data p[]) {
int i, j, mn;
struct data tmp;
for (i = 0; i < N - 1; i++) {
mn = i;
for (j = i + 1; j < N; j++) {
if (p[j].at < p[mn].at)
mn = j;
}
tmp = p[i];
p[i] = p[mn];
p[mn] = tmp;
}
}
// It calculate the average waiting time and average turnaround time
int main() {
//("%d", &N);
N = 10;
struct data p[N];
int atime, btime;
for (int i = 0; i < N; i++) {
//scanf("%d%d%d", &p[i].num, &p[i].at, &p[i].bt);
//p[i].num = i;
p[i].num = i + 1;
at = rand() % 10 + 1;
p[i].atime = at;
bt = rand() % 20 + 1;
p[i].btime = btime;
}
sort(p);
for (int i = 0; i < N; i++) {
printf(" %d %d %d \n", p[i].num, p[i].atime, p[i].btime);
}
LRT(p);
printf("\n");
return 0;
}