This repository was archived by the owner on Nov 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathso_scheduler.c
420 lines (348 loc) · 11.9 KB
/
so_scheduler.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "scheduler_struct.h"
/* strcuctura pentru planificarea thread-urilor */
static SO_SCHEDULER scheduler;
/* functia care ruleaza algoritmul de planificare */
static void algorithm_scheduler(SO_SCHEDULER scheduler,
THREAD_STATE futureState,
int io)
{
int rc = -1;
/*
* Cazul in care un thread se blocheaza si asteapta dispozitivul io
*/
if (futureState == WAITING) {
SO_THREAD wait_thread = NULL;
/* Thread-ul care executa instructiunea curenta */
SO_THREAD currentThread = scheduler->running_thread;
/* Thread-ul care urmeaza a fi planificat */
SO_THREAD futureThread = topPQueue(scheduler->ready_threads);
/* Verificam daca exista un thread in sistem */
if (currentThread == NULL || futureThread == NULL)
return;
/* se reinitializeaza timpul de rulare al thread-ului curent */
currentThread->time_task = 0;
/* Verificam daca exista thread-uri pentru dispozitivul io */
if (scheduler->wait_threads[io] == NULL)
scheduler->wait_threads[io] = createList();
/* Thread-ul curent trece in starea de wait */
currentThread->state = WAITING;
/* Se adauga thread-ul curent in lista de waiting pentru io */
rc = addElement(scheduler->wait_threads[io], currentThread);
/* Se alege un thread pentru rulare */
wait_thread = (SO_THREAD) popPQueue(scheduler->ready_threads);
if (wait_thread == NULL)
return;
/* Se initializeaza scheduler cu thread-ul curent */
scheduler->running_thread = futureThread;
/* Se trece in starea running pentru thread-ul curent */
scheduler->running_thread->state = RUNNING;
/* Se elibereaza thread-ul care va rula */
rc = sem_post(&futureThread->sem_wait);
DIE(rc != 0, "error sem_post");
/* Thread-ulcurent este pus in asteptare */
rc = sem_wait(¤tThread->sem_wait);
DIE(rc != 0, "error sem_wait");
} else if (futureState == READY) {
/*
* Cazul in care un thread este in starea ready, se verifica
* daca exista un thread cu prioritate mai mare pentru inlocuire
*/
/* Thread-ul care executa instructiunea curenta */
SO_THREAD currentThread = scheduler->running_thread;
/* Thread-ul care urmeaza a fi planificat */
SO_THREAD futureThread = topPQueue(scheduler->ready_threads);
DIE(currentThread == NULL && futureThread == NULL,
"error call ready");
/* Daca este la apelul primului fork */
if (currentThread == NULL) {
/* Se alege un thread pentru rulare */
popPQueue(scheduler->ready_threads);
scheduler->running_thread = futureThread;
/* Se elibereaza thread-ul care va rula */
rc = sem_post(&futureThread->sem_wait);
DIE(rc != 0, "error sem_post");
return;
}
/* Timpul thread-ului curent creste cu un proces */
currentThread->time_task++;
/* Daca nu exista un alt thread in sistem */
if (futureThread == NULL) {
if (currentThread->time_task == scheduler->time_quantum)
currentThread->time_task = 0;
return;
}
/*
* Thread-ul curent are prioritatea mai mare,
* isi continua executia
*/
if (currentThread->priority > futureThread->priority) {
if (currentThread->time_task == scheduler->time_quantum)
currentThread->time_task = 0;
return;
}
/*
* Thread-ul curent are aceeasi
* prioritate cu thread-ul planificat
*/
if (currentThread->priority == futureThread->priority)
if (currentThread->time_task < scheduler->time_quantum)
return;
/* Thread-ul curent isi reinitializeaza timpul de executie */
currentThread->time_task = 0;
/* Se adauga threadul curent in lista ready */
rc = pushPQueue(scheduler->ready_threads,
currentThread->priority,
currentThread);
DIE(rc != 0, "error add element");
/* Se scoate thread-ul cu prioritate din coada */
popPQueue(scheduler->ready_threads);
/* Se alege thread-ul care ruleaza */
scheduler->running_thread = futureThread;
/* Se elibereaza thread-ul care va rula */
rc = sem_post(&futureThread->sem_wait);
DIE(rc != 0, "error sem_post");
/* Se pune in asteptare thread-ul curent */
rc = sem_wait(¤tThread->sem_wait);
DIE(rc != 0, "error sem_wait");
} else if (futureState == TERMINATED) {
/*
* Cazul in care thread-ul curent este adaugat in lista
* thread-urilor care si-au terminat executia si se
* verifica daca exista thread-uri in sistem pentru a
* astepta terminarea acestora.
*/
/* Thread-ul care executa instructiunea curenta */
SO_THREAD currentThread = scheduler->running_thread;
/* Thread-ul care urmeaza a fi planificat */
SO_THREAD futureThread = topPQueue(scheduler->ready_threads);
/* Thread-ul curent se adauga in lista de terminare */
rc = addElement(scheduler->finish_threads, currentThread);
DIE(rc != 0, "error add element");
/* Se verifica daca exista alte thread-uri pentru terminare */
if (futureThread == NULL) {
rc = sem_post(&scheduler->sem_finish);
DIE(rc != 0, "error sem_post");
return;
}
/* Se alege un thread pentru rulare */
popPQueue(scheduler->ready_threads);
/* Se planifica urmatorul thread in scheduler */
scheduler->running_thread = futureThread;
/* Se elibereaza thread-ul care va rula */
rc = sem_post(&futureThread->sem_wait);
DIE(rc != 0, "error sem_post");
}
}
/*
* Functia pe care o executa thread-urile la planificare
*/
static void *thread_start(void *thread_info)
{
int rc = -1;
SO_THREAD thread = (SO_THREAD) thread_info;
/* Ne indica ca thread-ul respectiv este gata */
rc = sem_post(&thread->sem_create);
DIE(rc != 0, "error sem_post");
/* Se pune in asteptare pentru a fi planificat */
rc = sem_wait(&thread->sem_wait);
DIE(rc != 0, "sem_wait");
/* Se ruleaza functia care consuma timp */
thread->handler(thread->priority);
/*
* Se apeleaza algoritmul pentru un thread
* care si-a terminat executia
*/
algorithm_scheduler(scheduler, TERMINATED, -1);
return NULL;
}
int so_init(unsigned int time_quantum, unsigned int io)
{
int rc, i;
/* Se verifica parametrii daca sunt valizi */
if (scheduler != NULL || io > SO_MAX_NUM_EVENTS || time_quantum <= 0)
return -1;
/* Se aloca memorie pentru structura scheduler */
scheduler = malloc(sizeof(struct so_scheduler));
DIE(scheduler == NULL, "ENOMEM");
/*
* Se aloca memorie pentru vectorul liste
* cu thread-urile in asteptare
*/
scheduler->wait_threads = malloc(io * sizeof(struct linkedList));
DIE(scheduler->wait_threads == NULL, "ENOMEM");
/* Se initializeaza fiecare lista la NULL */
for (i = 0; i < io; i++)
scheduler->wait_threads[i] = NULL;
/* Se initializeaza semaforul care indica terminarea programului */
rc = sem_init(&scheduler->sem_finish, 0, 0);
DIE(rc == -1, "error sem_init");
scheduler->maximum_io = io;
scheduler->time_quantum = time_quantum;
scheduler->running_thread = NULL;
scheduler->active_thread = 0;
/* Se creaza coada de prioritati pentru thread-urile din starea ready */
scheduler->ready_threads = initPQueue();
/* Se creaza lista cu thread-urile din starea finish */
scheduler->finish_threads = createList();
return 0;
}
tid_t so_fork(so_handler *func, unsigned int priority)
{
tid_t id_thread;
SO_THREAD thread = NULL;
int rc = -1;
/* Se verifica daca parametrii sunt valizi */
if (func == NULL || priority > SO_MAX_PRIO)
return INVALID_TID;
/* Se aloca memorie pentru noul thread */
thread = malloc(sizeof(struct so_thread));
DIE(thread == NULL, "ENOMEM");
thread->priority = priority;
thread->handler = func;
thread->time_task = 0;
thread->state = NEW;
scheduler->active_thread = 1;
/* Se initializeaza semaforul pentru thread-ul creat */
rc = sem_init(&thread->sem_create, 0, 0);
DIE(rc != 0, "error sem_init");
rc = sem_init(&thread->sem_wait, 0, 0);
DIE(rc != 0, "error sem_init");
/* Se creaza thread-ul cu rutina data ca parametru */
rc = pthread_create(&id_thread, NULL, thread_start, thread);
if (rc != 0) {
sem_destroy(&thread->sem_create);
free(thread);
return INVALID_TID;
}
thread->id_thread = id_thread;
/* Se adauga thread-ul curent in coada ready pentru planificare */
rc = pushPQueue(scheduler->ready_threads, thread->priority, thread);
DIE(rc != 0, "error add element");
/* Se asteapta ca thread-ul curent sa fie in starea ready */
rc = sem_wait(&thread->sem_create);
DIE(rc != 0, "error sem_wait");
/* Se apeleaza algoritm-ul pentru cazul READY pentru planificare */
algorithm_scheduler(scheduler, READY, -1);
return id_thread;
}
int so_wait(unsigned int io)
{
/* Se verifica daca parametrii sunt valizi */
if (io >= scheduler->maximum_io || io < 0)
return -1;
/* Se apeleaza algoritmul de planificare pentru cazul WAIT */
algorithm_scheduler(scheduler, WAITING, io);
return 0;
}
int so_signal(unsigned int io)
{
int ret = -1;
int number_threads = 0;
int maximum_prio = -1;
LinkedList wait_threads = NULL;
SO_THREAD wait_thread = NULL;
/* Se verifica daca sunt valizi parametrii */
if (io >= scheduler->maximum_io || io < 0)
return -1;
/* Se verifica daca lista din wait este valida */
if (scheduler->wait_threads == NULL ||
scheduler->wait_threads[io] == NULL)
return 0;
number_threads = getSize(scheduler->wait_threads[io]);
if (number_threads < 0)
return 0;
/*
* Adaugam toate thread-urile care asteapta,
* trezite de dispozitivul io
*/
wait_threads = scheduler->wait_threads[io];
while (number_threads > 0) {
wait_thread = (SO_THREAD) popElement(wait_threads);
if (maximum_prio < wait_thread->priority)
maximum_prio = wait_thread->priority;
ret = pushPQueue(scheduler->ready_threads,
wait_thread->priority,
wait_thread);
if (ret != 0)
return -1;
number_threads--;
}
/* Daca exista un thread cu prioritate mai mare pentru replanificare */
if (maximum_prio > scheduler->running_thread->priority)
algorithm_scheduler(scheduler, READY, -1);
else if (scheduler->running_thread->time_task ==
scheduler->time_quantum) {
scheduler->running_thread->time_task = 0;
algorithm_scheduler(scheduler, READY, -1);
} else
algorithm_scheduler(scheduler, READY, -1);
return 0;
}
void so_exec(void)
{
/* Se trimite spre planificare thread-ul curent pentru executie */
if (scheduler->running_thread->time_task == scheduler->time_quantum) {
scheduler->running_thread->time_task++;
algorithm_scheduler(scheduler, READY, -1);
return;
}
algorithm_scheduler(scheduler, READY, -1);
}
void so_end(void)
{
int rc, i;
NodeList finish_thread = NULL;
QNode ready_thread = NULL;
SO_THREAD thread = NULL;
if (scheduler == NULL)
return;
/* Se asteapta sa termine toate thread-urile create */
if (scheduler->active_thread == 1) {
rc = sem_wait(&scheduler->sem_finish);
DIE(rc != 0, "error sem_wait");
}
finish_thread = scheduler->finish_threads->head;
/* Facem join pe toate thread-urile care nu si-au terminat executia */
while (finish_thread != NULL) {
thread = (SO_THREAD) finish_thread->value;
rc = pthread_join(thread->id_thread, NULL);
DIE(rc != 0, "error pthread_join");
finish_thread = finish_thread->next;
}
/* Eliberam memoria pentru toate thread-urile din lista finish */
finish_thread = scheduler->finish_threads->head;
while (finish_thread != NULL) {
thread = (SO_THREAD) finish_thread->value;
rc = sem_destroy(&thread->sem_wait);
DIE(rc != 0, "sem_destroy");
rc = sem_destroy(&thread->sem_create);
DIE(rc != 0, "sem_destroy");
free(thread);
finish_thread = finish_thread->next;
}
destructList(scheduler->finish_threads);
/* Eliberam memoria pentru toate threadu-urile din lista ready */
ready_thread = scheduler->ready_threads->head;
while (ready_thread != NULL) {
thread = (SO_THREAD) ready_thread->data;
rc = sem_destroy(&thread->sem_wait);
DIE(rc != 0, "sem_destroy");
rc = sem_destroy(&thread->sem_create);
DIE(rc != 0, "sem_destroy");
free(thread);
ready_thread = ready_thread->next;
}
clearPQueue(scheduler->ready_threads);
rc = sem_destroy(&scheduler->sem_finish);
DIE(rc != 0, "sem_destroy");
for (i = 0; i < scheduler->maximum_io; i++) {
if (scheduler->wait_threads[i] != NULL)
destructList(scheduler->wait_threads[i]);
}
free(scheduler->wait_threads);
free(scheduler);
scheduler = NULL;
}