forked from pelhamnicholas/xv6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frisbee.c
49 lines (41 loc) · 884 Bytes
/
frisbee.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
#include "param.h"
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fs.h"
#include "fcntl.h"
#include "syscall.h"
#include "traps.h"
#include "memlayout.h"
#include "semaphore.h"
#define NUMTHREADS 20
semaphore frisbee_sem;
int thrower;
void player(void *arg_ptr);
int main(int argc, char *argv[]) {
int i;
sem_init(&frisbee_sem, 1);
for (i = 0; i < NUMTHREADS; i++) {
thread_create(player, (void*)&i);
sleep(10);
}
while(wait(0)>=0) ;
exit(0);
}
void player(void *arg_ptr) {
int i, self;
int *num = (int*) arg_ptr;
self = *num;
for (i = 0; i < 10; i++) {
if (thrower != self) {
sem_wait(&frisbee_sem);
printf(1, "%d caught frisbee from %d\n", self, thrower);
thrower = self;
printf(1, " throwing frisbee\n", self);
sleep(20);
sem_signal(&frisbee_sem);
}
sleep(20);
}
exit(0);
}