forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.c
291 lines (250 loc) · 7.06 KB
/
test.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//
// Created by nadav on 3/13/19.
//
#include "types.h"
#include "user.h"
#include "fcntl.h"
#define ROUND_ROBIN 1
#define PRIORITY 2
#define EXTENED_PRIORITY 3
struct perf {
int ctime; // Creation time
int ttime; // Termination time
int stime; // The total time spent in the SLEEPING state
int retime; // The total time spent in the RUNNABLE state
int rutime; // The total time spent in the RUNNING state
};
typedef boolean (test_runner)();
void run_test(test_runner *test, char *name) {
printf(1, "========== Test - %s: Begin ==========\n", name);
boolean result = (*test)();
if (result) {
printf(1, "========== Test - %s: Passed ==========\n", name);
} else {
printf(1, "========== Test - %s: Failed ==========\n", name);
}
}
boolean assert_equals(int expected, int actual, char *msg) {
if (expected != actual) {
printf(2, "Assert %s failed: expected %d but got %d\n", msg, expected, actual);
return false;
} else return true;
}
void print_perf(struct perf *performance) {
printf(1, "pref:\n");
printf(1, "\tctime: %d\n", performance->ctime);
printf(1, "\tttime: %d\n", performance->ttime);
printf(1, "\tstime: %d\n", performance->stime);
printf(1, "\tretime: %d\n", performance->retime);
printf(1, "\trutime: %d\n", performance->rutime);
printf(1, "\n\tTurnaround time: %d\n", (performance->ttime - performance->ctime));
}
int fact(int n) {
if (!n)
return 0;
return n * fact(n - 1);
}
unsigned long long fact2(unsigned long long n, unsigned long long k) {
start:
if (n == 1) {
return k;
} else {
--n;
k = k * n;
goto start;
}
}
int fib(int n) {
if (!n)
return 1;
return fib(n - 1) + fib(n - 2);
}
boolean test_exit_wait() {
int status;
boolean result = true;
int pid;
for (int i = 0; i < 20; ++i) {
pid = fork();
if (pid > 0) {
wait(&status);
result = result && assert_equals(i, status, "exit&wait");
status = -1;
} else {
sleep(3);
exit(i);
}
}
return result;
}
boolean test_detach() {
int status1;
int status2;
int status3;
int pid;
boolean result1;
boolean result2;
boolean result3;
pid = fork();
if (pid > 0) {
status1 = detach(pid);
result1 = assert_equals(0, status1, "detach - status1");
status2 = detach(pid);
result2 = assert_equals(-1, status2, "detach - status2");
status3 = detach(-10);
result3 = assert_equals(-1, status3, "detach - status3");
return result1 && result2 && result3;
} else {
sleep(100);
exit(0);
}
}
boolean test_policy_helper(int *priority_mod, int policy) {
int nProcs = 100;
int pid, status;
boolean result = true;
for (int i = 0; i < nProcs; ++i) {
pid = fork();
if (pid < 0) {
break;
} else if (pid == 0) {
if (priority_mod) {
if ((i % *(priority_mod)) == 0 && policy == PRIORITY) {
priority(1);
} else {
priority(i % (*priority_mod));
}
}
sleep(10);
exit(0);
}
}
for (int j = 0; j < nProcs; ++j) {
wait(&status);
result = result && assert_equals(0, status, "round robin");
}
return result;
}
boolean test_round_robin_policy() {
return test_policy_helper(null, null);
}
boolean test_priority_policy() {
int priority_mod = 10;
policy(PRIORITY);
boolean result = test_policy_helper(&priority_mod, PRIORITY);
policy(ROUND_ROBIN);
return result;
}
boolean test_extended_priority_policy() {
int priority_mod = 10;
policy(EXTENED_PRIORITY);
boolean result = test_policy_helper(&priority_mod, EXTENED_PRIORITY);
policy(ROUND_ROBIN);
return result;
}
boolean test_performance_helper(int *npriority) {
int pid1;
struct perf perf2;
pid1 = fork();
if (pid1 > 0) {
int status1;
wait_stat(&status1, &perf2);
print_perf(&perf2);
} else {
for (int a = 0; a < 100; ++a) {
int pid;
struct perf perf1;
pid = fork();
// the child pid is pid
if (pid > 0) {
int status;
sleep(5);
wait_stat(&status, &perf1);
} else {
if (npriority)
priority(*npriority);
int sum = 0;
for (int i = 0; i < 100000000; ++i) {
for (int j = 0; j < 100000000; ++j) {
++sum;
}
}
sleep(5);
exit(0);
}
}
exit(0);
}
return true;
}
boolean test_starvation_helper(int npolicy, int npriority) {
boolean result = true;
policy(npolicy);
int nProcs = 10;
int pid_arr[nProcs];
int pid;
memset(&pid_arr, 0, nProcs * sizeof(int));
for (int i = 0; i < nProcs; ++i) {
pid = fork();
if (pid < 0) {
break;
} else if (pid == 0) {
sleep(5);
priority(npriority);
for (;;) {};
} else {
pid_arr[i] = pid;
}
}
sleep(100);
for (int j = 0; j < nProcs; ++j) {
if (pid_arr[j] != 0) {
result = result && assert_equals(0, kill(pid_arr[j]), "failed to kill child (yes it does sound horrible)");
wait(null);
}
}
policy(ROUND_ROBIN);
return result;
}
/**
* test the growth of accumulator
*/
boolean test_accumulator() {
return test_starvation_helper(PRIORITY, 2);
}
/** I hope this does test the case of
starvation in extended priority
(where the priority is 0)
*/
boolean test_starvation() {
return test_starvation_helper(EXTENED_PRIORITY, 0);
}
boolean test_performance_round_robin() {
return test_performance_helper(null);
}
boolean test_performance_priority() {
policy(PRIORITY);
int npriority = 2;
boolean result = test_performance_helper(&npriority);
policy(ROUND_ROBIN);
return result;
}
boolean test_performance_extended_priority() {
policy(EXTENED_PRIORITY);
int npriority = 0;
boolean result = test_performance_helper(&npriority);
policy(ROUND_ROBIN);
return result;
}
int main(void) {
run_test(&test_exit_wait, "exit&wait");
run_test(&test_detach, "detach");
run_test(&test_round_robin_policy, "round robin policy");
run_test(&test_priority_policy, "priority policy");
run_test(&test_extended_priority_policy, "extended priority policy");
run_test(&test_accumulator, "accumulator");
run_test(&test_starvation, "starvation");
run_test(&test_performance_round_robin, "performance round robin");
run_test(&test_performance_priority, "performance priority");
run_test(&test_performance_extended_priority, "performance extended priority");
exit(0);
}