/
test.c
113 lines (100 loc) · 2.22 KB
/
test.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
#include "types.h"
#include "stat.h"
#include "fcntl.h"
#include "user.h"
#include "x86.h"
#include "uthread.h"
#include "signal.h"
void foo(void* i);
void bar(void* j);
//-----------------------------------PART 3
int main(){
uthread_init();
struct binary_semaphore semaphore;
binary_semaphore_init(&semaphore, 1);
uthread_create(&foo, &semaphore);
uthread_create(&foo, &semaphore);
// uthread_join(2);
uthread_exit();
exit();
}
void foo(void* sem) {
int k;
binary_semaphore_down((struct binary_semaphore*)sem);
for (k=0; k<20; k++) {
printf (1, "foo: tid: %d\n", uthread_self());
}
binary_semaphore_up((struct binary_semaphore*)sem);
}
// -------------------- PART 2
// int main(){
// uthread_init();
//
//
// struct binary_semaphore semaphore;
// binary_semaphore_init(&semaphore, 1);
// binary_semaphore_down(&semaphore);
// binary_semaphore_up(&semaphore);
//
// // int j=7, i=3;
// //
//
// //
// // uthread_create(&bar, &j);
// //
// // uthread_create(&foo, &i);
// // uthread_join(1);
// // uthread_join(2);
// // int k,l,m;
// // for (k=0; k<1; k++) {
// // for (l=0; l<1; l++) {
// // for (m=0; m<1; m++) {
// // printf (1, "main\n");
// // }
// // }
// // }
// // printf (1, "main\n");
//
// uthread_exit();
// exit();
// }
//
// void foo(void* i) {
// int k,l,m;
// for (k=0; k<2; k++) {
// for (l=0; l<2; l++) {
// for (m=0; m<2; m++) {
// printf (1, "foo: %d\n", *(int *)i);
// }
// }
// }
// printf (1, "foo: %d\n", *(int *)i);
// }
//
// void bar(void* j) {
// int k,l,m;
// for (k=0; k<2; k++) {
// for (l=0; l<2; l++) {
// for (m=0; m<2; m++) {
// printf (1, "bar: %d\n", *(int *)j);
// }
// }
// }
// printf (1, "bar: %d\n", *(int *)j);
// }
//----------------------- eran -----------------------
/*
void test(void *t){
while (1){
printf(1,"thread child %p\n", t);
}
}
int main(int argc,char** argv){
uthread_init();
uthread_create(test, (void *) 5);
while (1){
printf(1,"thread father\n");
}
exit();
return 0;
}*/