-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_3.c
87 lines (72 loc) · 1.36 KB
/
test_3.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
#include "types.h"
#include "stat.h"
#include "param.h"
#include "memlayout.h"
#include "mmu.h"
#include "x86.h"
#include "spinlock.h"
#include "proc.h"
#include "user.h"
#include "synch.h"
#define N 8
void *stack[NTHREAD];
int tid[NTHREAD];
void *retval[NTHREAD];
struct mutex_t m;
struct cond_t c;
int buffer[10];
int idx=0;
int condition = 0;
void *thread(void *arg){
// printf(1, "Holding thread: %d\n", m.current);
mutex_lock(&m);
while(condition == 0){
cond_wait(&c, &m);
// printf(1, "cond wait successed\n");
}
condition--;
buffer[idx++] = (int)arg;
mutex_unlock(&m);
thread_exit(0);
}
int
main(int argc, char **argv)
{
int i;
printf(1, "TEST: ");
for(i=0;i<NTHREAD;i++)
stack[i] = malloc(4096);
printf(1, "MUTEX: %d\nCV: %d\n", &m, &c);
mutex_init(&m);
cond_init(&c);
for(i=0;i<NTHREAD-1;i++){
tid[i] = thread_create(thread, (void *)i, stack[i]);
printf(1, "thread %d\n", i);
if(tid[i] == -1){
printf(1, "thread create failed\n");
exit();
}
}
for(i=0;i<NTHREAD-1;i++){
mutex_lock(&m);
condition++;
cond_signal(&c);
mutex_unlock(&m);
}
for(i=0;i<NTHREAD-1;i++){
if(thread_join(tid[i], &retval[i]) == -1){
printf(1, "thread join failed\n");
exit();
}
}
for(i=0;i<NTHREAD-1;i++){
if(buffer[i] != i){
printf(1, "WRONG\n");
exit();
}
}
for(i=0;i<NTHREAD;i++)
free(stack[i]);
printf(1, "OK\n");
exit();
}