/
main.c
122 lines (100 loc) · 2.48 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
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
#include "sched.h"
#include "input.h"
#include "disk.h"
inline int min(int t1, int t2, int t3)
{
int min = t1;
if(t2 < min)
min = t2;
if(t3 < min)
min = t3;
return min;
}
int main(int argc, char *argv[])
{
int time = 10; /* cpu time */
int t = 0; /* time interval */
READY_QUEUE rq;
QUEUE wq, oq;
init_q(&wq);
init_q(&oq);
init_rq(&rq);
struct run_task rt;
rt.status = IDLE;
rt.set = FALSE;
rt.rproc = NULL;
TASK *arr_prog;
arr_prog = read_prog();
int t1, t2, t3;
while(TRUE){
t1 = nex_arr_prog(arr_prog, time);
t2 = nex_tslice(&rt);
t3 = next_io(&wq);
assert(t1 >= 0);
assert(t2 >= 0);
assert(t3 >= 0);
t = min(t1, t2, t3);
time += t;
if(rt.status == RUNNING)
update_run_task(&rt, t);
update_io_time(&wq, t);
/* after 1ms switch out, put the running process into correct queue */
if(!rt.set && rt.status == SWITCH_OUT)
settle_proc(&rt, &rq, &oq, time);
if(rt.status == IDLE || rt.status == SWITCH_OUT){
// put process on CPU cost 1 ms
if(sched_compl_io(&wq))
handle_compl_io(&rq, &wq, &oq, time);
while(sched_new_prog(arr_prog, time)){
push_rq(&rq, arr_prog);
arr_prog = read_prog();
}
time += MIN_T;
update_io_time(&wq, MIN_T);
if(!put_proc_on_cpu(&rt, &rq)){
rt.status = IDLE;
if(!peek(&wq) && !arr_prog)
break;
}
}
if(sched_take_out_proc(&rt, &wq, arr_prog, time))
take_out_proc(&rt);
if(sched_compl_io(&wq))
handle_compl_io(&rq, &wq, &oq, time);
if(sched_nex_io(&rt))
new_io(&wq, &rt);
while(sched_new_prog(arr_prog, time)){
push_rq(&rq, arr_prog);
arr_prog = read_prog();
}
}
printf("Current Time: %d\n", time);
TASK *last;
int num = 0;
long tot_compl_time = 0;
int min_compl_time = INF;
int max_compl_time = 0;
int tot_cpu_time = 0;
int tmp_time = 0;
while(len(&oq)){
last = dequeue(&oq);
assert(last->c_io <= 0);
num++;
tmp_time = last->t_finish - last->t_arrive;
assert(tmp_time > 0);
tot_compl_time += tmp_time;
if(tmp_time < min_compl_time)
min_compl_time = tmp_time;
if(tmp_time > max_compl_time)
max_compl_time = tmp_time;
tot_cpu_time += last->t_exec;
// printf("Prog Name: %s\n", last->cmd);
// printf("Finish Time: %d\n", last->t_finish);
}
printf("ACT = %f\n", ((double)tot_compl_time) / num);
printf("Min = %d\n", min_compl_time);
printf("Max = %d\n", max_compl_time);
printf("Throughput = %f\n", num / ((double)(time / 1000)));
printf("Utilization = %f\n",((double)tot_cpu_time) / time);
return 1;
}