-
Notifications
You must be signed in to change notification settings - Fork 0
/
autograder_final.c
411 lines (294 loc) · 9.23 KB
/
autograder_final.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/**
* Sean Gillen April 2019
*
* This file will automatically grade your cs170 project 2.
* Please see the README for more info
*/
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <poll.h>
#include <signal.h>
#include <fcntl.h>
#define NUM_TESTS 15
#define PASS 1
#define FAIL 0
#define TEST_WAIT_MILI 4000 // how many miliseconds do we wait before assuming a test is hung
//if your code compiles you pass test 0 for free
//==============================================================================
static int test0(void){
return PASS;
}
//basic pthread create and exit tests
//==============================================================================
static void* _thread_dummy(void* arg){
pthread_t a = pthread_self();
pthread_exit(0);
}
static void* _thread_dummy_loop(void* arg){
pthread_t a = pthread_self();
for(int i = 0; i < 100000000; i++);
pthread_exit(0);
}
static int test1(void){
pthread_t tid1 = 0;
pthread_t tid2 = 0;
pthread_create(&tid1, NULL, &_thread_dummy, NULL);
pthread_create(&tid2, NULL, &_thread_dummy_loop, NULL);
if(tid1 != tid2){
return PASS;
}else{
return FAIL;
}
}
//basic pthread self test
//==============================================================================
static pthread_t global_tid1 = 1;
static void* _thread_self_test(void* arg){
global_tid1 = pthread_self();
pthread_exit(0);
}
static int test2(void){
pthread_t tid1 = 0;
pthread_create(&tid1, NULL, &_thread_self_test, NULL);
while((global_tid1 != tid1)); //failure occurs on a timeout
return PASS;
}
//scheduler test 1
//==============================================================================
static int a3 = 0;
static int b3 = 0;
static void* _thread_schedule_a(void* arg){
while(1){
a3 = 1;
}
}
static void* _thread_schedule_b(void* arg){
while(1){
if(a3){
b3 = 1;
}
}
}
static int test3(void){
pthread_t tid1; pthread_t tid2;
pthread_create(&tid1, NULL, &_thread_schedule_a, NULL);
pthread_create(&tid2, NULL, &_thread_schedule_b, NULL);
while(b3 != 1); //just wait, failure occurs if there is a timeout
return PASS;
}
//scheduler test 2
//==============================================================================
static int a4 = 0;
static void* _thread_inc4(void* arg){
for(int i = 0; i < 10; i++){
a4++;
}
pthread_exit(0);
}
static int test4(void){
pthread_t tid1; pthread_t tid2;
pthread_create(&tid1, NULL, &_thread_inc4, NULL);
pthread_create(&tid2, NULL, &_thread_inc4, NULL);
for(int i = 0; i < 10; i++){
a4++;
}
while(a4 != 30); //just wait, failure occurs if there is a timeout
return PASS;
}
//scheduler test 3
//==============================================================================
static int a5 = 0;
static void* _thread_inc1(void* arg){
a5++;
pthread_exit(0);
}
static int test5(void){
pthread_t tid1;
for(int i = 0; i < 128; i++){
pthread_create(&tid1, NULL, &_thread_inc1, NULL);
}
while(a5 != 128); //just wait, failure occurs if there is a timeout
return PASS;
}
//passing arguments
//==============================================================================
static int e = 0;
static void* _thread_arg(void* arg){
e = *(int*)arg;
pthread_exit(0);
}
static int test6(void){
pthread_t tid1; pthread_t tid2;
int arg = 244567;
pthread_create(&tid1, NULL, &_thread_arg, &arg);
while(e != arg); //just wait, failure occurs if there is a timeout
return PASS;
}
//does your stack work?
//==============================================================================
static int _thread_fib(int a){
if(a == 0) {
return 0;
}
if(a == 1){
return 1;
}
return _thread_fib(a-1) + _thread_fib(a-2);
}
static void* _thread_fcn(void* arg){
int fib = *(int*)arg;
*(int*)arg = _thread_fib(fib);
pthread_exit(0);
}
static int test7(void){
pthread_t tid1; pthread_t tid2;
int arg = 12;
pthread_create(&tid1, NULL, &_thread_fcn, &arg);
while(arg != 144); //just wait, failure occurs if there is a timeout
return PASS;
}
//Start of private tests
//basic pthread self test
//==============================================================================
static pthread_t global_tid2 = 57;
static void* _thread_self_test2(void* arg){
global_tid2 = pthread_self();
pthread_exit(0);
}
static int test8(void){
pthread_t tid1 = 0;
pthread_create(&tid1, NULL, &_thread_self_test2, NULL);
while((global_tid2 != tid1)); //failure occurs on a timeout
return PASS;
}
//do you actually resume execution? or do you call start routine over and over?
//==============================================================================
static int a9 = 0;
static int b9 = 0;
static int c9 = 0;
static void* _thread_schedule_c(void* arg){
if(b9 == 0){
while(b9 != 1){
a9 = 1;
}
c9 = 1;
}
}
static void* _thread_schedule_d(void* arg){
while(1){
if(a9){
b9 = 1;
}
}
}
static int test9(void){
pthread_t tid1; pthread_t tid2;
pthread_create(&tid1, NULL, &_thread_schedule_c, NULL);
pthread_create(&tid2, NULL, &_thread_schedule_d, NULL);
while(c9 != 1); //just wait, failure occurs if there is a timeout
return PASS;
}
//pthread create from a thread
//==============================================================================
static int a10;
static void* _thread_create_inc(void* arg){
a10 = 1;
}
static void* _thread_create(void* arg){
pthread_t tid1;
pthread_create(&tid1, NULL, &_thread_create_inc, NULL);
pthread_exit(0);
}
static int test10(void){
pthread_t tid1 = 0;
pthread_create(&tid1, NULL, &_thread_create, NULL);
while((a10 != 1)); //failure occurs on a timeout
return PASS;
}
//Let's combine some previous tests
//==============================================================================
static int test11(void){
return( test1() && test7());
}
//==============================================================================
static int test12(void){
return( test5() && test7());
}
//==============================================================================
static int test13(void){
return( test4() && test6());
}
//another scheduler test
//==============================================================================
static int a14 = 0;
static void* _thread_inc14(void* arg){
a14++;
pthread_exit(0);
}
static int test14(void){
pthread_t tid1;
for(int i = 0; i < 11; i++){
pthread_create(&tid1, NULL, &_thread_inc14, NULL);
}
while(a14 != 11); //just wait, failure occurs if there is a timeout
for(int i = 0; i < 13; i++){
pthread_create(&tid1, NULL, &_thread_inc14, NULL);
}
while(a14 != 11 + 13); //just wait, failure occurs if there is a timeout
return PASS;
}
/**
* Some implementation details: Main spawns a child process for each
* test, that way if test 2/20 segfaults, we can still run the remaining
* tests. It also hands the child a pipe to write the result of the test.
* the parent polls this pipe, and counts the test as a failure if there
* is a timeout (which would indicate the child is hung).
*/
//Yeah I don't exactly love this either
static int (*test_arr[NUM_TESTS])(void) = {&test0, &test1, &test2, &test3, &test4, &test5,
&test6, &test7, &test8, &test9, &test10,&test11,
&test12,&test13, &test14};
int main(void){
int status; pid_t pid;
int pipe_fd[2]; int timeout; struct pollfd poll_fds;
int score = 0; int total_score = 0;
int devnull_fd = open("/dev/null", O_WRONLY);
pipe(pipe_fd);
poll_fds.fd = pipe_fd[0]; // only going to poll the read end of our pipe
poll_fds.events = POLLRDNORM; //only care about normal read operations
for(int i = 0; i < NUM_TESTS; i++){
score = 0;
pid = fork();
//child, launches the test
if (pid == 0){
dup2(devnull_fd, STDOUT_FILENO); //begone debug messages
dup2(devnull_fd, STDERR_FILENO);
score = test_arr[i]();
write(pipe_fd[1], &score, sizeof(score));
exit(0);
}
//parent, polls on the pipe we gave the child, kills the child,
//keeps track of score
else{
if(poll(&poll_fds, 1, TEST_WAIT_MILI)){
read(pipe_fd[0], &score, sizeof(score));
}
total_score += score;
kill(pid, SIGKILL);
waitpid(pid,&status,0);
if(score){
printf("test %i : PASS\n", i);
}
else{
printf("test %i : FAIL\n", i);
}
}
}
printf("total score was %i / %i\n", total_score, NUM_TESTS);
return 0;
}