-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
91 lines (72 loc) · 2.65 KB
/
main.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
#include <limits.h>
#include <stdio.h>
#include "scheduling_sim.h"
#include "scheduling_utility.h"
// ---------------------------------------------------------------------------
// Example schedulers
// ---------------------------------------------------------------------------
process_t* fcfs(const int __attribute__((unused)) timestep,
processes_t* processes) {
process_t* selected = NULL;
int min_arrival_time = INT_MAX;
for (int i = 0; i < processes->num_processes; ++i) {
process_t* proc = processes->procs[i];
if (proc->yielded) {
proc->user1 = 0;
continue;
}
if (proc->user1 == 1) {
return proc;
}
if (proc->arrival_time < min_arrival_time) {
min_arrival_time = proc->arrival_time;
selected = proc;
}
}
if (selected == NULL) {
selected = processes->procs[0];
}
selected->user1 = 1;
return selected;
}
process_t* round_robin(const int __attribute__((unused)) timestep,
processes_t* processes_struct) {
process_t** processes = processes_struct->procs;
int num_processes = processes_struct->num_processes;
process_t* selected = processes[0];
int next_idx = 1 % num_processes;
for (int i = 0; i < num_processes; ++i) {
if (processes[i]->user1 == 1) {
selected = processes[i];
next_idx = (i + 1) % num_processes;
break;
}
}
selected->user1 = 0;
process_t* next = processes[next_idx];
if (next != selected) {
next->user1 = 1;
}
return selected;
}
// ---------------------------------------------------------------------------
// Implement your schedulers here
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
int main(int argc, char** argv) {
if (argc < 2) {
fprintf(stderr, "Error: Usage: %s <filename>\nExample: %s input.csv\n",
argv[0], argv[0]);
return EXIT_FAILURE;
}
processes_t* procs = read_simulation_data(argv[1]);
print_simulation_data(stdout, procs);
// -----------------------------------------------------------------------
// Insert calls to your schedulers here
// -----------------------------------------------------------------------
print_schedule(stdout, compute_schedule(procs, fcfs), procs);
print_schedule(stdout, compute_schedule(procs, round_robin), procs);
// -----------------------------------------------------------------------
free_processes(procs);
return EXIT_SUCCESS;
}