-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrowd_monkey.c
83 lines (68 loc) · 1.54 KB
/
crowd_monkey.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
#include "types.h"
#include "stat.h"
#include "user.h"
#define TEST_NAME "crowd_monkey"
#include "318_test-tapish.h"
/* Authors Sam and Bailey */
/* A basic monkey test, performs a lot of operations at random based on the
* output of a random number generator. The random number generator (which
* is kinda crap) was borrowed from usertests */
// Potential improvement - just include a global variable from /dev/random
// and use that to generate random numbers
void monkey(void *);
void thread(void *);
int numthr;
unsigned long randstate = 1;
int counter = 0;
unsigned int rand()
{
if(++counter > 100) {
TEST_FINI();
exit();
}
randstate = randstate * 1664525 + 1013904223;
return randstate;
}
void do_operation() {
unsigned int rand40;
void *stack;
int tid = gettid();
rand40 = rand()%40;
if( rand40 < 10 ) {
numthr--;
texit();
} else if( rand40 < 30 ){
stack = malloc(2048);
if(stack != 0) {
numthr++;
tspawn(stack, monkey, 0);
}
} else if(rand40 < 50) {
TEST_EXIT_IF(tid != gettid(), "TID inaccurate");
}
}
void monkey(void *garbage) {
while(1) {
do_operation();
}
}
int main() {
void * stack;
int forks = 0;
TEST_STRT(100);
while(forks < 100) {
forks++;
printf(1, "Fork #%d\n", forks);
if(fork() == 0) {
numthr = 0;
while(numthr < 1) {
stack = malloc(2048);
TEST_EXIT_IF(stack == 0, "malloc fail");
TEST_EXIT_IF(tspawn(stack, monkey, 0) <= 0, "tspawn failed");
}
} else {
wait();
}
}
exit();
}