-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.c
68 lines (54 loc) · 1.71 KB
/
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
#include "scheduler.h"
#include "screen.h"
void scheduler_init() {
next_sch_pid = 0;
curr_sch_pid = 0;
}
process_t *get_next_process() {
process_t *next_process = processes[next_sch_pid];
curr_sch_pid = next_sch_pid;
next_sch_pid++;
next_sch_pid = next_sch_pid % process_count;
return next_process;
}
void scheduler(int eip, int edi, int esi, int ebp, int esp, int ebx,
int edx, int ecx, int eax) {
process_t *curr_process;
print("EAX = ");
printi(eax);
println();
curr_process = processes[curr_sch_pid];
next_process = get_next_process();
// saving the current process state
if (curr_process->state == RUNNING) {
curr_process->context.eax = eax;
curr_process->context.ebx = ebx;
curr_process->context.ecx = ecx;
curr_process->context.edx = edx;
curr_process->context.esp = esp;
curr_process->context.ebp = ebp;
curr_process->context.esi = esi;
curr_process->context.edi = edi;
curr_process->context.eip = eip;
}
curr_process->state = READY;
// next lines of code use inline assembly to load the next process
asm( " mov %0, %%eax; \
mov %0, %%ecx; \
mov %0, %%edx; \
mov %0, %%ebx; \
mov %0, %%esi; \
mov %0, %%edi;"
: : "r" ( next_process->context.eax ),
"r" ( next_process->context.ecx ),
"r" ( next_process->context.edx ),
"r" ( next_process->context.ebx ),
"r" ( next_process->context.esi ),
"r" ( next_process->context.edi )
);
next_process->state = RUNNING;
}
void run_next_process() {
asm("sti; \
jmp *%0" : : "r" (next_process->context.eip) );
}