-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel.c
66 lines (59 loc) · 1.53 KB
/
kernel.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
#include "kernel.h"
process* pool[POOLSIZE];
char ini, fim;
char kernelInit(void) {
ini = 0;
fim = 0;
return SUCCESS;
}
char kernelAddProc(process* newProc) {
//checking for free space
if (((fim + 1) % POOLSIZE) != ini) {
pool[fim] = newProc;
pool[fim]->start += newProc->period;
fim = (fim + 1) % POOLSIZE;
return SUCCESS;
}
return FAIL;
}
void kernelLoop(void) {
char next, j;
process * tempProc;
for (;;) {
//Do we have any process to execute?
if (ini != fim) {
//Find the process with the lowest timer
next = ini;
j = (ini + 1) % POOLSIZE;
while (j != fim) {
if (pool[j]->start < pool[next]->start) {
next = j;
}
j = (j + 1) % POOLSIZE;
}
//Exchanging processes positions
tempProc = pool[next];
pool[next] = pool[ini];
pool[ini] = tempProc;
//waiting for the process to be ready
while (pool[ini]->start > 0) {
//Great place for energy saving
}
if (pool[ini]->function() == REPEAT) {
kernelAddProc(pool[ini]);
}
ini = (ini + 1) % POOLSIZE;
}
}
}
#define MIN_INT -30000
void KernelClock(void) {
unsigned char i;
i = ini;
while (i != fim) {
if ((pool[i]->start)>(MIN_INT)) {
pool[i]->start--;
}
i = (i + 1) % POOLSIZE;
}
}