-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.c
196 lines (161 loc) · 4.72 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
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
#include "scheduler.h"
#include "mmu.h"
p_queue_t *move_to_end_of_queue(p_queue_t *old_queue) {
p_queue_t *current = old_queue;
if (old_queue == NULL) {
return NULL;
}
while (current->next != NULL) {
current = current->next;
}
current->next = old_queue;
old_queue->prev = current;
old_queue = old_queue->next;
old_queue->prev = NULL;
current->next->next = NULL;
return old_queue;
}
process_t *create_process(int mem_size, uint8_t pid) {
process_t *new_process = malloc(sizeof(process_t));
new_process->pid = pid;
new_process->mem_size = mem_size;
new_process->mem_start = -1;
new_process->state = NEW;
new_process->time_quantum = 0;
new_process->time_used = 0;
new_process->time_waiting = 0;
new_process->file_d = pid % 5;
switch (pid % 5) {
case 0:
new_process->time_remaining = 10;
break;
case 1:
new_process->time_remaining = 5;
break;
case 2:
new_process->time_remaining = 15;
break;
case 3:
new_process->time_remaining = 8;
break;
case 4:
new_process->time_remaining = 9;
break;
default:
exit(1);
}
return new_process;
}
bool can_allocate_mem(process_t **process) {
int8_t mem_start = first_fit_mem((*process)->mem_size);
if (mem_start == -1) {
return false;
}
(*process)->mem_start = mem_start;
return true;
}
p_queue_t *add_process_to_queue(p_queue_t *old_queue,
process_t *process_to_add) {
p_queue_t *new_queue = malloc(sizeof(p_queue_t));
p_queue_t *current = old_queue;
new_queue->process = process_to_add;
new_queue->next = NULL;
if (old_queue == NULL) {
new_queue->prev = NULL;
return new_queue;
}
while (current->next != NULL) {
current = current->next;
}
new_queue->prev = current;
current->next = new_queue;
return old_queue;
}
p_queue_t *toggle_next_process_state(p_queue_t *queue) {
p_queue_t *current = queue;
if (queue == NULL) {
return NULL;
}
current->process->state = RUNNING;
return queue;
}
p_queue_t *run_process(p_queue_t *old_queue, sched_info_t sched_info) {
p_queue_t *current = old_queue;
if (old_queue == NULL) {
return NULL;
}
if (current->process->mem_start == -1) {
if (can_allocate_mem(&(current->process))) {
current->process->state = READY;
} else {
current->process->time_waiting++;
return move_to_end_of_queue(old_queue);
}
}
if (sched_info.algorithm == FIFO) {
if (current->process->time_remaining > 0) {
current->process->time_remaining--;
current->process->time_used++;
current->process->state = RUNNING;
}
if (current->process->time_remaining > 0) {
return old_queue;
}
return kill_process(current->process->pid, old_queue);
} else {
if (current->process->time_quantum < sched_info.time_quantum) {
current->process->time_quantum++;
current->process->time_remaining--;
current->process->time_used++;
current->process->state = RUNNING;
}
if (current->process->time_remaining > 0) {
if (current->process->time_quantum >= sched_info.time_quantum) {
current->process->time_quantum = 0;
current->process->state = READY;
old_queue = move_to_end_of_queue(old_queue);
old_queue = toggle_next_process_state(old_queue);
}
return old_queue;
}
return kill_process(current->process->pid, old_queue);
}
}
p_queue_t *remove_process_from_queue(p_queue_t *queue, p_queue_t *process) {
p_queue_t *current = queue;
if (process->prev == NULL) {
queue = process->next;
if (queue != NULL) {
queue->prev = NULL;
}
free(process);
return queue;
}
while (current->next != process) {
current = current->next;
}
current->next = process->next;
if (process->next != NULL) {
process->next->prev = current;
}
free(process);
return queue;
}
p_queue_t *kill_process(int32_t pid, p_queue_t *queue) {
p_queue_t *current = queue;
if (queue == NULL) {
return NULL;
}
while (current->process->pid != pid) {
current = current->next;
if (current == NULL) {
return queue;
}
}
free_mem(current->process->mem_start, current->process->mem_size);
queue = remove_process_from_queue(queue, current);
/*
current->process->state = TERMINATED;
*/
return queue;
}