-
Notifications
You must be signed in to change notification settings - Fork 0
/
sch_func.c
346 lines (300 loc) · 6.89 KB
/
sch_func.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
/*Describe the execution flow
of a process
*/
typedef struct execution{
int start;
int end;
struct execution* next;
} execution;
/*Process information*/
typedef struct {
char name; //process name
int t_arr; //arrival time
int t_exe; //execution time
int t_rem; //remaining time
int t_end; //end time
execution* exe;
} process;
/*Describe an element in the List
contain a process and a pointer to the next element
*/
typedef struct elem {
process proc;
struct elem* next;
} elem;
/*Describe a List
containt the size of the list and a pointer to the first element
*/
typedef struct {
int size;
elem* first;
} List;
/*Add a new execution to a process*/
void add_exe(process* p, int start, int end){
if(p->exe == NULL){
p->exe = malloc(sizeof(execution));
p->exe->start = start;
p->exe->end = end;
p->exe->next = NULL;
return;
}
execution* e = p->exe;
while(e->next != NULL)
e = e->next;
if(start == e->end){
e->end = end;
return;
}
e->next = malloc(sizeof(execution));
e->next->start = start;
e->next->end = end;
e->next->next = NULL;
}
/*Create a new element at the end of the list
which contain the process proc
also increment the size
*/
void add(List* processes, process proc){
elem *e,*next;
e = malloc(sizeof(elem));
e->proc = proc;
e->next = NULL;
if (processes->first == NULL) {
processes->first = e;
processes->size++;
return;
}
if (processes->first->next == NULL){
processes->first->next = e;
processes->size++;
return;
}
next = processes->first;
while(next->next->next != NULL)
next = next->next;
next->next->next = e;
processes->size++;
return;
}
/*Returns the process of the first element in the list
or NULL if it's empty
also decrement the size if not empty
*/
process* pop(List* processes){
if(processes->size == 0)
return NULL;
process* p = malloc(sizeof(process));
elem* e = processes->first;
processes->first = e->next;
processes->size--;
memcpy(p, &e->proc, sizeof(process));
free(e);
return p;
}
/*Switch two process*/
void switch_proc(process* p1, process* p2){
process* tmp = malloc(sizeof(process));
memcpy(tmp, p1, sizeof(process));
memcpy(p1, p2, sizeof(process));
memcpy(p2, tmp, sizeof(process));
free(tmp);
}
/*Sort the list of processes according to
their arrival time
*/
void sort_arr(List* processes){
int size = processes->size;
if(size <= 1)
return;
elem* e;
char sorted = 0;
while(!sorted){
sorted = 1;
e = processes->first;
size--;
for(int i=0; i<size; i++){
if(e->proc.t_arr > e->next->proc.t_arr){
switch_proc(&e->proc, &e->next->proc);
sorted = 0;
}
e = e->next;
}
}
}
/*Sort the list of processes according to
their execution time
*/
void sort_exe(List* processes){
int size = processes->size;
if(size <= 1)
return;
elem* e;
char sorted = 0;
while(!sorted){
sorted = 1;
e = processes->first;
size--;
for(int i=0; i<size; i++){
if(e->proc.t_exe > e->next->proc.t_exe){
switch_proc(&e->proc, &e->next->proc);
sorted = 0;
}
e = e->next;
}
}
}
/*Sort the list of processes according to
their remaining time
*/
void sort_rem(List* processes){
int size = processes->size;
if(size <= 1)
return;
elem* e;
char sorted = 0;
while(!sorted){
sorted = 1;
e = processes->first;
size--;
for(int i=0; i<size; i++){
if(e->proc.t_rem > e->next->proc.t_rem){
switch_proc(&e->proc, &e->next->proc);
sorted = 0;
}
e = e->next;
}
}
}
/*Get only the process that already arrived
from processes and add them to arrived
also return the number of process that arrived
*/
int get_arrived(List* processes, List* arrived, int time){
process* p;
sort_arr(processes);
int count = 0;
while(processes->size){
if(processes->first->proc.t_arr <= time){
p = pop(processes);
add(arrived,*p);
count++;
}else break;
}
return count;
}
/*Scheduele the process using
the Fist Come First Served algorithm
*/
process* fcfs(List* processes){
int time = 0;
int i = 0;
process* p = NULL;
process* result = malloc(sizeof(process) * processes->size);
sort_arr(processes);
while(processes->size){
p = pop(processes);
while(time < p->t_arr) //no process has arrived
time++;
add_exe(p, time, time + p->t_exe);
time += p->t_exe;
p->t_end = time;
p->t_rem = 0;
memcpy(result + i, p, sizeof(process));
i++;
}
return result;
}
/*Scheduele the process using
the Shortest Job First algorithm
*/
process* sjf(List* processes){
int time = 0;
int i = 0;
List* arrived = malloc(sizeof(List));
arrived->size = 0;
arrived->first = NULL;
process* p = NULL;
process* result = malloc(sizeof(process) * processes->size);
while(processes->size || arrived->size){
get_arrived(processes, arrived, time);
sort_exe(arrived);
p = pop(arrived);
if(p == NULL){//no process has arrived
time++;
continue;
}
add_exe(p, time, time + p->t_exe);
time += p->t_exe;
p->t_end = time;
p->t_rem = 0;
memcpy(result + i, p, sizeof(process));
i++;
}
return result;
}
/*Scheduele the process using
the Shortest Remaining Time First algorithm
*/
process* srtf(List* processes){
int time = 0;
int i = 0;
List* arrived = malloc(sizeof(List));
arrived->size = 0;
arrived->first = NULL;
process* p = NULL;
process* result = malloc(sizeof(process) * processes->size);
while(processes->size || arrived->size || p != NULL){
if(p != NULL)//last processes didn't end
add(arrived, *p);
get_arrived(processes, arrived, time);
sort_rem(arrived);
p = pop(arrived);
if(p == NULL){//no process has arrived
time++;
continue;
}
add_exe(p, time, time + 1);
time++;
p->t_rem--;
if(p->t_rem == 0){//process ended
p->t_end = time;
memcpy(result + i, p, sizeof(process));
p = NULL;
i++;
}
}
return result;
}
/*Scheduele the process using
the Round Robin algorithm
*/
process* rr(List* processes, int quantum){
int time = 0;
int t_used, i = 0;
List* arrived = malloc(sizeof(List));
arrived->size = 0;
arrived->first = NULL;
process* p = NULL;
process* result = malloc(sizeof(process) * processes->size);
while(processes->size || arrived->size || p != NULL){
get_arrived(processes, arrived, time);
if (p != NULL)//last process didn't end
add(arrived, *p);
p = pop(arrived);
if(p == NULL){//no process has arrived
time++;
continue;
}
t_used = (p->t_rem < quantum) ? p->t_rem : quantum;
add_exe(p, time, time + t_used);
time += t_used;
p->t_rem -= t_used;
if(p->t_rem == 0){//process ended
p->t_end = time;
memcpy(result + i, p, sizeof(process));
p = NULL;
i++;
}
}
return result;
}