-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.c
180 lines (165 loc) · 5.36 KB
/
control.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
#include "control.h"
void change_cpu(pid_t pid, int core){
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(core, &mask);
sched_setaffinity(pid, sizeof(mask), &mask);
return;
}
void increase_priority(pid_t pid){
struct sched_param p;
/* SCHED_IDLE should set priority to 0 */
p.sched_priority = 99;
sched_setscheduler(pid, SCHED_FIFO, &p);
return;
}
void decrease_priority(pid_t pid){
struct sched_param p;
/* SCHED_IDLE should set priority to 0 */
p.sched_priority = 1;
sched_setscheduler(pid, SCHED_FIFO, &p);
return;
}
int create_pro(process pro){
pid_t pid = fork();
if (pid < 0) {
perror("failed to fork");
return -1;
}
/*parent process*/
if(pid > 0){
decrease_priority(pid);
change_cpu(pid, 1);
printf("%s %d\n", pro.name, pid);
fflush(stdout);
return pid;
}
/*child process*/
else
{
struct timespec t_start, t_end;
char msg[200];
t_start = pro.t_start;
// syscall(GETTIME, &t_start);
for (int i = 0; i < pro.t_ex; i++) {
UNIT_TIME();
}
syscall(GETTIME, &t_end);
sprintf(msg, "%d %ld.%09ld %ld.%09ld\n", getpid(), t_start.tv_sec, t_start.tv_nsec, t_end.tv_sec, t_end.tv_nsec);
syscall(PRINTK, msg);
// printf("%s done!", pro.name);
exit(0);
}
}
int pq_pop(priority_q* pq, int* values){
if(pq -> len == 0){
perror("pop empty queue");
exit(0);
}
int ret = (pq -> q)[0];
(pq -> q)[0] = (pq -> q)[--(pq -> len)];
int i = 0;
int len = pq -> len;
while(i < len){
if(i*2 + 1 < len){
int root = (pq -> q)[i];
int left = (pq -> q)[i*2 + 1];
if(i*2 + 2 < len){
int right = (pq -> q)[i*2 + 2];
if(values[root] < values[left] && values[root] < values[right]) break;
else{
if(values[left] == values[right]){
if(values[left] == values[root]){
if(left < right && left < root){
(pq -> q)[i] = left;
(pq -> q)[i*2 + 1] = root;
i = i*2 + 1;
}
else if(left > right && right < root){
(pq -> q)[i] = right;
(pq -> q)[i*2 + 2] = root;
i = i*2 + 2;
}
else break;
}
else{
if(left < right){
(pq -> q)[i] = left;
(pq -> q)[i*2 + 1] = root;
i = i*2 + 1;
}
else if(left > right){
(pq -> q)[i] = right;
(pq -> q)[i*2 + 2] = root;
i = i*2 + 1;
}
else break;
}
}
else if(values[left] < values[right]){
if((values[left] < values[root]) || (values[left] == values[root] && left < root)){
(pq -> q)[i] = left;
(pq -> q)[i*2 + 1] = root;
i = i*2 + 1;
}
else break;
}
else{
if((values[right] < values[root]) || (values[right] == values[root] && right < root)){
(pq -> q)[i] = right;
(pq -> q)[i*2 + 2] = root;
i = i*2 + 2;
}
else break;
}
}
}
else{
if(values[left] < values[root]){
(pq -> q)[i] = left;
(pq -> q)[i*2 + 1] = root;
i = i*2 + 1;
}
else break;
}
}
else break;
}
return ret;
}
void pq_append(priority_q* pq, int n, int* values){
(pq -> q)[(pq -> len)] = n;
int i = (pq -> len);
(pq -> len)++;
while(i > 0){
int root = (pq -> q)[(i - 1) / 2];
int now = (pq -> q)[i];
if(values[now] < values[root] || (values[now] == values[root] && now < root)){
(pq -> q)[i] = root;
(pq -> q)[(i - 1) / 2] = now;
i = (i - 1) / 2;
}
else break;
}
return;
}
int q_pop(queue* q){
if(q -> len == 0){
perror("pop empty queue");
exit(0);
}
int ret = (q -> q)[q -> head];
(q -> head) = ((q -> head) + 1) % 20;
(q -> len)--;
return ret;
}
void q_append(queue* q, int n){
if(q -> len > 20){
perror("append full queue");
exit(0);
}
(q -> q)[(q -> tail)] = n;
(q -> tail) = ((q -> tail) + 1) % 20;
(q -> len)++;
return;
}