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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
#include "errno.h"
#include "globals.h"
#include "test/proctest.h"
#include "test/usertest.h"
#include "util/debug.h"
#include "util/printf.h"
#include "util/string.h"
#include "proc/kthread.h"
#include "proc/proc.h"
#include "proc/sched.h"
/*
* Set up a testing function for the process to execute.
*/
void *test_func(long arg1, void *arg2)
{
proc_t *proc_as_arg = (proc_t *)arg2;
test_assert(arg1 == proc_as_arg->p_pid, "Arguments are not set up correctly");
test_assert(proc_as_arg->p_state == PROC_RUNNING, "Process state is not running");
test_assert(list_empty(&proc_as_arg->p_children), "There should be no child processes");
dbg(DBG_TEST, "Process %s is running\n", proc_as_arg->p_name);
return NULL;
}
void test_termination()
{
int num_procs_created = 0;
proc_t *new_proc1 = proc_create("proc test 1");
kthread_t *new_kthread1 = kthread_create(new_proc1, test_func, 2, new_proc1);
num_procs_created++;
sched_make_runnable(new_kthread1);
int count = 0;
int status;
while (do_waitpid(-1, &status, 0) != -ECHILD)
{
dbg(DBG_TEST, "Waiting for child process to terminate\n");
test_assert(status == 0, "Returned status not set correctly");
count++;
}
test_assert(count == num_procs_created,
"Expected: %d, Actual: %d number of processes have been cleaned up\n", num_procs_created, count);
}
/*
Tests the edge cases of do_waitpid.
1. No child processes to wait for
2. Waiting on a specific process id
3. Waiting any child process (-1)
*/
void test_do_waitpid()
{
int status;
int ret = do_waitpid(-1, &status, 0);
test_assert(ret == -ECHILD, "No child processes to wait for");
proc_t *new_proc1 = proc_create("proc test 1");
kthread_t *new_kthread1 = kthread_create(new_proc1, test_func, new_proc1->p_pid, new_proc1);
sched_make_runnable(new_kthread1);
proc_t *new_proc2 = proc_create("proc test 2");
kthread_t *new_kthread2 = kthread_create(new_proc2, test_func, new_proc2->p_pid, new_proc2);
sched_make_runnable(new_kthread2);
proc_t *new_proc3 = proc_create("proc test 3");
kthread_t *new_kthread3 = kthread_create(new_proc3, test_func, new_proc3->p_pid, new_proc3);
sched_make_runnable(new_kthread3);
// wait for a specific process id
ret = do_waitpid(new_proc2->p_pid, &status, 0);
test_assert(ret == new_proc2->p_pid, "Waiting for a specific process id");
dbg(DBG_TEST, "Successfully removed proc 2 with pid: %d\n", new_proc2->p_pid);
// wait for any child process
ret = do_waitpid(-1, &status, 0);
test_assert(ret == new_proc1->p_pid, "Waiting for any child process");
dbg(DBG_TEST, "Successfully removed proc 1 with pid: %d\n", new_proc1->p_pid);
ret = do_waitpid(-1, &status, 0);
test_assert(ret == new_proc3->p_pid, "Waiting for any child process");
dbg(DBG_TEST, "Successfully removed proc 3 with pid: %d\n", new_proc3->p_pid);
ret = do_waitpid(-1, &status, 0);
test_assert(ret == -ECHILD, "No child processes to wait for");
}
/*
Tests proc_kill_all on a single process.
*/
void *test_func_kill(long arg1, void *arg2)
{
// function to be called by the process
proc_t *proc_as_arg = (proc_t *)arg2;
test_assert(arg1 == proc_as_arg->p_pid, "Arguments are not set up correctly");
test_assert(proc_as_arg->p_state == PROC_RUNNING, "Process state is not running");
test_assert(list_empty(&proc_as_arg->p_children), "There should be no child processes");
dbg(DBG_TEST, "Process with pid=%d is running and calling proc_kill_all\n", proc_as_arg->p_pid);
proc_kill_all();
return NULL;
}
void test_proc_kill_all()
{
proc_t *new_proc_kill = proc_create("proc test kill");
kthread_t *new_kthread_kill = kthread_create(new_proc_kill, test_func_kill, new_proc_kill->p_pid, new_proc_kill);
sched_make_runnable(new_kthread_kill);
proc_t *new_proc1 = proc_create("proc test 1");
kthread_t *new_kthread1 = kthread_create(new_proc1, test_func, new_proc1->p_pid, new_proc1);
sched_make_runnable(new_kthread1);
proc_t *new_proc2 = proc_create("proc test 2");
kthread_t *new_kthread2 = kthread_create(new_proc2, test_func, new_proc2->p_pid, new_proc2);
sched_make_runnable(new_kthread2);
// wait on the process that calls proc_kill_all
int status;
int ret = do_waitpid(new_proc_kill->p_pid, &status, 0);
test_assert(ret == new_proc_kill->p_pid, "Waiting for a specific process id");
dbg(DBG_TEST, "Successfully removed proc_kill with pid: %d\n", new_proc_kill->p_pid);
// wait for all the threads to finish
int count = 0;
while (do_waitpid(-1, &status, 0) != -ECHILD)
{
dbg(DBG_TEST, "Waiting for child process to terminate\n");
test_assert(status == 0, "Returned status not set correctly");
count++;
}
test_assert(count == 2,
"Expected: %d, Actual: %d number of processes have been cleaned up\n", 2, count);
}
/*
Test to run several threads and processes concurrently.
*/
void test_multiple()
{
int num_procs_created = 0;
proc_t *new_proc1 = proc_create("proc test 1");
kthread_t *new_kthread1 = kthread_create(new_proc1, test_func, new_proc1->p_pid, new_proc1);
num_procs_created++;
sched_make_runnable(new_kthread1);
proc_t *new_proc2 = proc_create("proc test 2");
kthread_t *new_kthread2 = kthread_create(new_proc2, test_func, new_proc2->p_pid, new_proc2);
num_procs_created++;
sched_make_runnable(new_kthread2);
proc_t *new_proc3 = proc_create("proc test 3");
kthread_t *new_kthread3 = kthread_create(new_proc3, test_func, new_proc3->p_pid, new_proc3);
num_procs_created++;
sched_make_runnable(new_kthread3);
// print all of the threads in the system
list_iterate(&curproc->p_threads, thread, kthread_t, kt_plink)
{
dbg(DBG_THR, "Thread: %s\n", thread->kt_proc->p_name);
}
// wait for all the threads to finish
int count = 0;
int status;
while (do_waitpid(-1, &status, 0) != -ECHILD)
{
dbg(DBG_TEST, "Waiting for child process to terminate\n");
test_assert(status == 0, "Returned status not set correctly");
count++;
}
test_assert(count == num_procs_created,
"Expected: %d, Actual: %d number of processes have been cleaned up\n", num_procs_created, count);
}
/*
Test that creates several child processes and forces them to terminate out of order.
Then it checks to see if the processes are cleaned up correctly.
*/
void *func_forever_yielding(long arg1, void *arg2)
{
// function that always just yields
// goal is that it is cleaned up by a different process
while (1)
{
// have a way to stop the thread
int *to_stop = (int *)arg2;
if (*to_stop)
{
dbg(DBG_TEST, "Thread with pid=%d is stopping yield loop\n", curproc->p_pid);
do_exit(1); // return different status code
break;
}
// wait a bit
for (int i = 0; i < 1000000; i++)
{
;
}
dbg(DBG_TEST, "Thread with pid=%d is yielding on it's parent\n", curproc->p_pid);
sched_yield();
}
return NULL;
}
void test_out_of_order_termination()
{
// create a yielding proc that will last for a long time
proc_t *new_proc1 = proc_create("yieling proc 1");
int stop_func_1 = 0;
kthread_t *new_kthread1 = kthread_create(new_proc1, func_forever_yielding, new_proc1->p_pid, &stop_func_1);
sched_make_runnable(new_kthread1);
proc_t *new_proc2 = proc_create("yielding proc 2");
int stop_func_2 = 0;
kthread_t *new_kthread2 = kthread_create(new_proc2, func_forever_yielding, new_proc2->p_pid, &stop_func_2);
sched_make_runnable(new_kthread2);
proc_t *new_proc3 = proc_create("proc test 3");
kthread_t *new_kthread3 = kthread_create(new_proc3, test_func, new_proc3->p_pid, new_proc3);
sched_make_runnable(new_kthread3);
proc_t *new_proc4 = proc_create("proc test 4");
kthread_t *new_kthread4 = kthread_create(new_proc4, test_func, new_proc4->p_pid, new_proc4);
sched_make_runnable(new_kthread4);
// let to first and second procs go and yield
sched_yield();
// the first and second proc should yield to the third proc and fourth proc
// which will return here to the init proc
test_assert(curproc->p_pid == PID_INIT, "Should have returned to init proc");
test_assert(new_proc3->p_state == PROC_DEAD, "Third proc should be dead");
test_assert(new_proc4->p_state == PROC_DEAD, "Fourth proc should be dead");
// destroy procs 3 and 4
// terminate the second proc
stop_func_2 = 1;
sched_yield();
// we should return back to the init proc
test_assert(curproc->p_pid == PID_INIT, "Should have returned to init proc");
test_assert(new_proc2->p_state == PROC_DEAD, "Second proc should be dead");
// terminate the first proc
stop_func_1 = 1;
sched_yield();
// we should return back to the init proc
test_assert(curproc->p_pid == PID_INIT, "Should have returned to init proc");
test_assert(new_proc1->p_state == PROC_DEAD, "First proc should be dead");
// clean up proc 2 using do_waitpid, with specific status 1
int status;
int ret = do_waitpid(new_proc2->p_pid, &status, 0);
test_assert(ret == new_proc2->p_pid, "wrong specific process id");
test_assert(status == 1, "Returned status not set correctly");
test_assert(new_proc2->p_status == 1, "Status not set correctly");
// clean up the rest of the dead procs using do_waitpid
int count = 0;
while ((ret = do_waitpid(-1, &status, 0)) != -ECHILD)
{
dbg(DBG_TEST, "found child with pid=%d that needs to be cleaned up\n", ret);
if (ret == new_proc1->p_pid) {
test_assert(status == 1, "Returned status not set correctly for proc 1 with pid=%d", new_proc1->p_pid);
} else {
test_assert(status == 0, "Returned status not set correctly");
}
count++;
}
test_assert(count == 3,
"Expected: %d, Actual: %d number of processes have been cleaned up\n", 3, count);
}
/*
Test threads' cancellation fields.
*/
void *did_run_func(long arg1, void *arg2)
{
// arg2 is a boolean flag that indicated this ran
*(int *)arg2 = 1;
return NULL;
}
void test_cancellation()
{
proc_t *new_proc1 = proc_create("proc test 1");
int did_run = 0;
kthread_t *new_kthread1 = kthread_create(new_proc1, did_run_func, new_proc1->p_pid, (void *)&did_run);
test_assert(new_kthread1->kt_cancelled == 0, "Thread should not be cancelled");
sched_make_runnable(new_kthread1);
proc_kill(new_proc1, 1);
test_assert(new_kthread1->kt_cancelled == 1, "Thread should be cancelled");
// wait for the thread to finish
int status;
int ret = do_waitpid(new_proc1->p_pid, &status, 0);
test_assert(ret != -ECHILD, "Should have found the process");
test_assert(ret == new_proc1->p_pid, "Should have found the correct process");
// THESE NO LONGER PASS AFTER RUNNING VM, BUT THIS IS EXPECTED
// test_assert(status == 1, "Returned status not set correctly");
// test_assert(did_run == 0, "Thread should not have run if it was cancelled");
}
long proctest_main(long arg1, void *arg2)
{
dbg(DBG_TEST, "\nStarting Procs tests\n");
test_init();
test_termination();
// Add more tests here!
// We highly recommend looking at section 3.8 on the handout for help!
dbg(DBG_TEST, "\nStarting test_multiple\n");
test_multiple();
dbg(DBG_TEST, "\nStarting test_do_waitpid\n");
test_do_waitpid();
dbg(DBG_TEST, "\nStarting test_proc_kill_all\n");
test_proc_kill_all();
dbg(DBG_TEST, "\nStarting test_out_of_order_termination\n");
test_out_of_order_termination();
dbg(DBG_TEST, "\nStarting test_cancellation\n");
test_cancellation();
test_fini();
return 0;
}
|