-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_monkey.c
71 lines (61 loc) · 1.51 KB
/
test_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
#include "types.h"
#include "user.h"
#include "semaphore.c"
struct semaphore *tree;
int dominantMonkey = 0;
void monkey(void* m) {
int num = (int)m;
while(dominantMonkey != 0){}
sema_acquire(tree);
//printf(1,"Monkey acq Count: %d\n", tree->count);
printf(1,"Monkey #%d UP\n", num);
int i = 0;
int coconutTimer = random(99999) + 500000;
for(;i < coconutTimer; i++){}
printf(1,"Monkey $%d DOWN\n",num);
sema_signal(tree);
//printf(1,"Monkey sig Count: %d\n", tree->count);
texit();
}
void dmonkey(void* m) {
dominantMonkey++;
sema_acquire(tree);
//printf(1,"Monkey acq Count: %d\n", tree->count);
printf(1,"Dominant Monkey UP\n");
int i = 0;
int coconutTimer = random(99999) + 500000;
for(;i < coconutTimer; i++){}
printf(1,"Dominant Monkey DOWN\n");
sema_signal(tree);
dominantMonkey--;
//printf(1,"Monkey sig Count: %d\n", tree->count);
texit();
}
void part_a() {
int i = 0;
for(; i < 10; i++) {
thread_create(monkey,(void*)i);
}
while(wait() >= 0);
}
void part_c() {
int i = 0;
for(; i < 5; i++) {
thread_create(monkey,(void*)i);
}
thread_create(dmonkey,(void*)1);
for(; i < 10; i++) {
thread_create(monkey,(void*)i);
}
while(wait() >= 0);
}
int main(int argc, char *argv[]){
sema_init(tree);
tree->count = 3;
printf(1,"\n*****Part a*****\n\n");
part_a();
printf(1,"\n*****Part c*****\n\n");
part_c();
printf(1,"\n");
exit();
}