-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.c
260 lines (221 loc) · 3.55 KB
/
tests.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "kernel.h"
#include "mi86/debug.h"
#define SERVER 1
struct semaphore a, b, c, full, empty, mutex;
extern uint_8 stack_b[], stack_c[] ;
int f, r, pro;
void test_producer1(void);
void test_producer2(void);
void test_customer1(void);
void test_customer2(void);
void pro_cus(void){
f = 0;
r = 0;
pro = 0;
sem_init(&full, 0, FALSE);
sem_init(&empty, 100, FALSE);
sem_init(&mutex, 1, FALSE);
cli();
kthread_create(test_producer1, &stack_b[254], 3);
kthread_create(test_producer2, &stack_c[254], 3);
sti();
}
void test_producer1(void){
while (1){
P(&empty);
P(&mutex);
pro++;
r = (r + 1) % 100;
V(&full);
V(&mutex);
}
}
void test_producer2(void){
while (1){
P(&empty);
P(&mutex);
pro++;
r = (r + 1) % 100;
V(&full);
V(&mutex);
}
}
void test_customer1(void){
while (1){
P(&full);
P(&mutex);
pro--;
f = (f + 1) % 100;
V(&empty);
V(&mutex);
}
}
void test_customer2(void){
while (1){
P(&full);
P(&mutex);
pro--;
f = (f + 1) % 100;
V(&empty);
V(&mutex);
}
}
void test_server(void){
struct message ms;
while (1){
receive(&ms);
printk("SERVER receive message (%d,%d,%d) from PID:%d",
ms.p1, ms.p2, ms.p3, ms.source);
send(2, &ms);
idle_cpu();
}
}
void test_sender(void){
struct message ms;
ms.p1 = 1;
ms.p2 = 2;
ms.p3 = 3;
while (1){
printk("SENDER send message (%d,%d,%d) to SERVER\n",
ms.p1, ms.p2, ms.p3);
invoke(SERVER, &ms);
ms.p3++;
idle_cpu();
}
}
void sem_a(void);
void sem_b(void);
void sem_c(void);
void semtest(void){
int i;
printk("this should be a test for semaphore\n");
printk("wish you good luck\n");
sem_init(&a, 1, FALSE);
sem_init(&b, 0, FALSE);
sem_init(&c, 0, FALSE);
cli();
kthread_create(sem_a, &stack_b[254], 2);
kthread_create(sem_b, &stack_c[254], 2);
sti();
}
void sem_a(void){
int i;
while (1){
P(&a);
printk("A");
for (i = 0; i < 40; i++);
V(&b);
}
}
void sem_b(void){
int i;
while (1){
P(&b);
printk("B");
for (i = 0; i < 40; i++);
V(&c);
}
}
void sem_c(void){
int i;
while (1){
P(&c);
printk("C");
for (i = 0; i < 40; i++);
V(&a);
}
}
void test1_a(void){
int i = 10;
char c = 'a';
while (i--) {
printk("why %c ", c);
c++;
if (c > 'z')
c = 'a';
yield();
printk("back from yield\n");
}
while (1) printk("this is A\n");
}
void test1_b(void){
int i = 10;
char c = 'A';
while (i--) {
printk("%c ", c);
c++;
if (c > 'Z')
c = 'A';
yield();
}
while(1) printk("this is B\n");
}
void test1_c(void){
int i = 0;
while (i < 1000){
i++;
printk("%d ", i);
yield();
}
while (1) printk("this is C\n");
}
int cnt = 0;
void thread_a(void){
int tmp;
while (1){
tmp = cnt+1;
printk("\ndata%d \n", tmp);
cnt = tmp;
for (tmp = 0; tmp < 20; tmp++);
idle_cpu();
}
}
void thread_b(void){
int tmp;
while(1){
tmp = cnt+1;
printk("\ndata%d \n", tmp);
cnt = tmp;
for (tmp = 0; tmp < 20; tmp++);
idle_cpu();
}
}
static uint_16 count = 0;
void test_thread_a(void) {
int i, j, k, l, tmp;
for (i = 0; i < 512; i++)
for (j = 0; j < 512; j++)
for (k = 0; k < 512; k++){
/* dis_int();
*/
tmp = count + 1;
count = tmp;
if (count == 0){
printk("a");
printk("\n");
}
/* en_int();
*/
}
cli();
printk("A reported: total = %d\n", count);
sti();
}
void test_thread_b(void){
int i, j, k, l, tmp;
for (i = 0; i < 512; i++)
for (j = 0; j < 512; j++)
for (k = 0; k < 512; k++){
/* dis_int();
*/
tmp = count + 1;
count = tmp;
/* printk("%d ", count);
*/
/* en_int();
*/
}
cli();
printk("B reported: total = %d\n", count);
sti();
}