-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinal_system_Queue_Array_random_data1223.c
363 lines (351 loc) · 12.2 KB
/
Final_system_Queue_Array_random_data1223.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define SIZE 10 //the size of data
typedef struct node{
int arrivaltime;
int bursttime;
int ID;
}storage;
typedef struct array{
storage *q;
}array;
typedef struct QType{
int count;
int front;
int rear;
int limit;
struct array*queue;
}QType;
QType * newQueue(void);
void enQ(QType *,int ,int ,int );
void deQ(QType *);
//void delQueue(QType**);
void RoundRobin(QType *,int );
void SJF_non(QType *,int );
void first_come_first_serve(QType *,int);
void Shortest_job_first_preemptive(QType *,int);
void sort(QType*);
void shift(QType*);
int main()
{
srand(time(NULL));
int i,total_burst = 0,total_bt = 0;
QType *Q = newQueue();
QType *Q1 = newQueue();
QType *Q2 = newQueue();
QType *Q3 = newQueue();
int burst[SIZE]={0};
int arrival[SIZE]={0};
int ID[SIZE] = {0};
for(i=0;i<SIZE;i++)
{
burst[i]=rand()%20+1;
}
for(i=1;i<SIZE;i++)
{
arrival[i]=arrival[i-1]+rand()%5+1;
}
for(i=0;i<SIZE;i++)
{
ID[i]=i+1;
}
printf("Process\t\tBurst time\tArrival time\n");
for(i=0;i<SIZE;i++)
{
printf("P%2d\t\t%d\t\t%d\n",1+i,burst[i],arrival[i]);
}
for(i=0;i<SIZE;i++){ //fcfs1.1&1.2,sjf_non,sjf_p scheduling
enQ(Q,arrival[i],burst[i],ID[i]); //enQ to Job Queue
enQ(Q1,arrival[i],burst[i],ID[i]); //enQ to Job Queue
enQ(Q2,arrival[i],burst[i],ID[i]); //enQ to Job Queue
enQ(Q3,arrival[i],burst[i],ID[i]); //enQ to Job Queue
total_burst=total_burst+burst[i];
}
first_come_first_serve(Q2,total_burst);
RoundRobin(Q,total_burst);
SJF_non(Q1,total_burst);
Shortest_job_first_preemptive(Q3,total_burst);
return 0;
}
QType * newQueue(void)
{
int i;
QType * Q;
Q = malloc (sizeof(QType));
Q->queue =malloc(sizeof(struct array));
Q->queue->q = malloc(sizeof(struct node)*SIZE);
for (i=0; i<SIZE; i++){
Q->queue->q[i].arrivaltime=0; // initialize
Q->queue->q[i].bursttime=0; // initialize
Q->queue->q[i].ID=0; // initialize
}
Q->front = 0; // initialize
Q->rear = -1; // initialize
Q->count = 0; // initialize
Q->limit = SIZE; // initialize
return Q;
}
void enQ(QType *Q,int arrival,int burst,int id)
{
Q->rear++; //queue+1 at rear
Q->queue->q[Q->rear].arrivaltime = arrival;
Q->queue->q[Q->rear].bursttime = burst;
Q->queue->q[Q->rear].ID = id;
Q->count++; //queue+1
}
void deQ(QType *Q)
{
Q->queue->q[Q->front].arrivaltime = 0;
Q->queue->q[Q->front].bursttime = 0;
Q->queue->q[Q->front].ID = 0;
Q->count--; //queue-1
Q->front++; //queue-1 at front
}
void first_come_first_serve(QType * Q1,int total_time) //fcfs sort by P1~P10
{
QType*Q2=newQueue();
int i=0,j=0;
int time=0,A=0,wait=0;
double avg;
printf("\nNo.1 first_come_first_serve\n\n");
for(time=0;time<total_time;)
{
if(Q1->queue->q[i].arrivaltime<=time&&i<SIZE)
{
enQ(Q2,Q1->queue->q[i].arrivaltime,Q1->queue->q[i].bursttime,Q1->queue->q[i].ID); //enQ to Ready Queue
wait=time-Q2->queue->q[i].arrivaltime;
printf("P%2d waiting time : %2d\n",Q2->queue->q[i].ID,wait);
A=wait+A; //A is total waiting time
time=time+Q2->queue->q[i].bursttime; //time is each process's waiting time
deQ(Q2);
i++;
}
else time++;
}
avg=(double)A/SIZE; //avg is average waiting time
printf("\nAverage Waiting Time : %.2f\n\n",avg);
}
void RoundRobin(QType *Q1,int total_burst)
{
printf("\nNO.2 Round Robin \n\n");
QType*Q2=newQueue();
int time,tq=4,tw=0;
int wait;
int B[SIZE+1]={0};
int i=0,j=0,k=0;
int A=0;
int flag=0;
double avg;
for(time=0;time<total_burst;)
{
if(Q1->queue->q[i].arrivaltime<=time&&i<SIZE) //judge whether process come in or not
{
k=0;
for(j=i;j<SIZE;j++)
{
if(Q1->queue->q[j].arrivaltime<=time)
{
enQ(Q2,Q1->queue->q[j].arrivaltime,Q1->queue->q[j].bursttime,Q1->queue->q[j].ID); //enQ to Ready Queue
k++;
}
}
if(flag==0)
{
shift(Q2);
}
flag=0;
for(tw=0;tw<tq;tw++)
{
time++;
Q2->queue->q[Q2->front].bursttime--;
B[Q2->queue->q[Q2->front].ID]++;
if(Q2->queue->q[Q2->front].bursttime==0)
{
wait=time-Q2->queue->q[Q2->front].arrivaltime-B[Q2->queue->q[Q2->front].ID];
printf("P%2d waiting time : %2d\n",Q2->queue->q[Q2->front].ID,wait);
A=A+wait;
deQ(Q2);
flag=1;
break;
}
}
i=i+k;
}
else if(Q2->count!=0) //judge whether process come in or not
{
if(flag==0)
{
shift(Q2);
}
flag=0;
for(tw=0;tw<tq;tw++)
{
time++;
Q2->queue->q[Q2->front].bursttime--;
B[Q2->queue->q[Q2->front].ID]++;
if(Q2->queue->q[Q2->front].bursttime==0)
{
wait=time-Q2->queue->q[Q2->front].arrivaltime-B[Q2->queue->q[Q2->front].ID];
printf("P%2d waiting time : %2d\n",Q2->queue->q[Q2->front].ID,wait);
A=A+wait;
deQ(Q2);
flag=1;
break;
}
}
}
else time++;
}
avg=(double)A/SIZE;
printf("\nAverage Waiting Time : %.2f\n\n",avg);
}
void shift(QType*Q2)
{
int temp,i;
temp=Q2->queue->q[Q2->front].bursttime;
for(i=Q2->front;i<Q2->rear;i++)
{
Q2->queue->q[i].bursttime=Q2->queue->q[i+1].bursttime;
}
Q2->queue->q[Q2->rear].bursttime=temp;
temp=Q2->queue->q[Q2->front].arrivaltime;
for(i=Q2->front;i<Q2->rear;i++)
{
Q2->queue->q[i].arrivaltime=Q2->queue->q[i+1].arrivaltime;
}
Q2->queue->q[Q2->rear].arrivaltime=temp;
temp=Q2->queue->q[Q2->front].ID;
for(i=Q2->front;i<Q2->rear;i++)
{
Q2->queue->q[i].ID=Q2->queue->q[i+1].ID;
}
Q2->queue->q[Q2->rear].ID=temp;
}
void SJF_non(QType *Q1,int total_time)
{
QType*Q9 = newQueue();
int time=0,wait=0;
int A=0;
int B[SIZE+1]={0};
double avg;
int i=0,flag=1;
printf("\nNo.3 Shortest_job_first_non_preemptive\n\n");
for(time=0;time<total_time;) //calculate time = 0 to total time
{
if(Q1->queue->q[i].arrivaltime<=time&&i<SIZE) //judge whether process come in or not
{
enQ(Q9,Q1->queue->q[i].arrivaltime,Q1->queue->q[i].bursttime,Q1->queue->q[i].ID); //enQ to Ready Queue
if(flag==1)
{
sort(Q9); //sort by BT (the smallest BT do this scheduling first)
flag=0;
}
time++;
B[Q9->queue->q[Q9->front].ID]++; //calculate each process do for a few seconds
Q9->queue->q[Q9->front].bursttime--; //go to do process so the process's BT-1
if(Q9->queue->q[Q9->front].bursttime==0) //if the process have done,then go to print
{
wait=time-Q9->queue->q[Q9->front].arrivaltime-B[Q9->queue->q[Q9->front].ID]+1; //waiting time=the process's AT-the process's BT
A=A+wait; //A is total waiting time
printf("P%2d waiting time : %2d\n",Q9->queue->q[Q9->front].ID,wait);
deQ(Q9); //have done ,quit from Ready Queue
flag=1;
}
i++;
}
else if(Q9->count!=0) //if the process not come in,but there are process in ready Queue
{
if(flag==1)
{
sort(Q9); //sort by BT (the smallest BT do this scheduling first)
flag=0;
}
time++;
B[Q9->queue->q[Q9->front].ID]++; //calculate each process do for a few seconds
Q9->queue->q[Q9->front].bursttime--; //go to do process so the process's BT-1
if(Q9->queue->q[Q9->front].bursttime==0) //if the process have done,then go to print
{
wait=time-Q9->queue->q[Q9->front].arrivaltime-B[Q9->queue->q[Q9->front].ID]+1; //waiting time=the process's AT-the process's BT
A=A+wait; //A is total waiting time
printf("P%2d waiting time : %2d\n",Q9->queue->q[Q9->front].ID,wait);
deQ(Q9); //have done ,quit from Ready Queue
flag=1;
}
}
else time++;
}
avg=(double)A/SIZE; //avg is average waiting time
printf("\nAverage Waiting Time : %.2f\n\n",avg);
}
void Shortest_job_first_preemptive(QType *Q1,int total_time)
{
int time=0,wait=0;
int A=0;
int B[SIZE+1]={0};
int j;
double avg;
QType *Q2=newQueue();
int i=0;
printf("\nNo.4 Shortest_job_first_preemptive\n\n");
for(time=0;time<total_time;time++) //calculate time = 0 to total time
{
if(Q1->queue->q[i].arrivaltime<=time&&i<SIZE) //judge whether process come in or not
{
enQ(Q2,Q1->queue->q[i].arrivaltime,Q1->queue->q[i].bursttime,Q1->queue->q[i].ID); //enQ to Ready Queue
sort(Q2); //sort by BT (the smallest BT do this scheduling first)
B[Q2->queue->q[Q2->front].ID]++; //calculate each process do for a few seconds
Q2->queue->q[Q2->front].bursttime--; //go to do process so the process's BT-1
if(Q2->queue->q[Q2->front].bursttime==0) //if the process have done,then go to print
{
wait=time-Q2->queue->q[Q2->front].arrivaltime-B[Q2->queue->q[Q2->front].ID]+1; //waiting time=the process's AT-the process's BT
A=A+wait; //A is total waiting time
printf("P%2d waiting time : %2d\n",Q2->queue->q[Q2->front].ID,wait);
deQ(Q2); //have done ,quit from Ready Queue
}
i++;
}
else if(Q2->count!=0) //if the process not come in,but there are process in ready Queue
{
sort(Q2); //sort by BT (the smallest BT do this scheduling first)
B[Q2->queue->q[Q2->front].ID]++; //calculate each process do for a few seconds
Q2->queue->q[Q2->front].bursttime--; //go to do process so the process's BT-1
if(Q2->queue->q[Q2->front].bursttime==0) //if the process have done,then go to print
{
wait=time-Q2->queue->q[Q2->front].arrivaltime-B[Q2->queue->q[Q2->front].ID]+1; //waiting time=the process's AT-the process's BT
A=A+wait; //A is total waiting time
printf("P%2d waiting time : %2d\n",Q2->queue->q[Q2->front].ID,wait);
deQ(Q2); //have done ,quit from Ready Queue
}
}
}
avg=(double)A/SIZE; //avg is average waiting time
printf("\nAverage Waiting Time : %.2f\n\n",avg);
}
void sort (QType*Q) //sort by BT from small to big
{
int i=Q->front,j=0;
int temp;
if(Q->count!=0)
{
for(i=Q->front;i<(Q->count+Q->front)-1;i++)
{
for(j=i+1;j<(Q->count+Q->front);j++)
{
if((Q->queue->q[i].bursttime)>(Q->queue->q[j].bursttime)) //sort by BT from small to big
{
temp=Q->queue->q[i].bursttime;
Q->queue->q[i].bursttime=Q->queue->q[j].bursttime;
Q->queue->q[j].bursttime=temp;
temp=Q->queue->q[i].ID;
Q->queue->q[i].ID=Q->queue->q[j].ID;
Q->queue->q[j].ID=temp;
temp=Q->queue->q[i].arrivaltime;
Q->queue->q[i].arrivaltime=Q->queue->q[j].arrivaltime;
Q->queue->q[j].arrivaltime=temp;
}
}
}
}
}