-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtest.c
More file actions
150 lines (125 loc) · 3.26 KB
/
htest.c
File metadata and controls
150 lines (125 loc) · 3.26 KB
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
/*
* htest.c
*
* Created on: Apr 26, 2015
* Author: hodai
*/
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fs.h"
#define STACK_SIZE 1000
void* execThread(){
char* cat[] ={"cat", 0};
printf(1, "exec thread, tid:%d\n", kthread_id());
exec("cat", cat);
printf(2, "error - return from exec\n");
exit();
}
void* exitThread(){
printf(1, "exit thread, tid:%d\n", kthread_id());
exit();
}
void* normalThread(){
printf(1, "normal thread, tid:%d\n", kthread_id());
kthread_exit();
return 0;
}
void* loopThread(){
printf(1, "loop thread, tid:%d\n", kthread_id());
for(;;){};
kthread_exit();
return 0;
}
void* sleepThread(){
printf(1, "sleep thread, tid:%d\n", kthread_id());
sleep(1000);
printf(1, "tid:%d done\n", kthread_id());
kthread_exit();
return 0;
}
void* theThread(){
int i;
for(i = 0 ;i < 10; i++){
printf(1, "thread:%d,i=%d\n", kthread_id(), i);
}
kthread_exit();
return 0;
}
void stressTest1(int count){
int tid[count];
int i,ans;
char* stack;
int fail = 0;
for (i = 0 ; i < count; i++){
stack = malloc(STACK_SIZE);
tid[i] = kthread_create(theThread, stack+STACK_SIZE, STACK_SIZE);
if(tid[i] <= 0){
printf(2, "error: kthread_create return with: %d, for index:%d", tid[i], i);
fail = 1;
}
}
for (i = 0 ; i < count; i++){
ans = kthread_join(tid[i]);
if(ans != 0){
printf(2, "error: kthread_join(%d) return with: %d", tid[i], ans);
fail = 1;
}
}
if(fail){
printf(1, "stressTest1 fail\n");
} else {
printf(1, "stressTest1 success\n");
}
}
int main(){
char buf[100];
void* stack;
int t;
printf(1, "chose operation\n");
stack = malloc(STACK_SIZE);
while(read(0, buf, 100) >= 0){
stack = malloc(STACK_SIZE);
if(strcmp("sleep\n", buf) == 0){
t = kthread_create(sleepThread, stack+STACK_SIZE, STACK_SIZE);
printf(1, "creating sleep thread tid:%d\n", t);
} else if(strcmp(buf, "loop\n") == 0){
t = kthread_create(loopThread, stack+STACK_SIZE, STACK_SIZE);
printf(1, "creating loop thread tid:%d\n", t);
} else if(strcmp(buf, "norm\n") == 0){
t = kthread_create(normalThread, stack+STACK_SIZE, STACK_SIZE);
printf(1, "creating normal thread tid:%d\n", t);
} else if(strcmp(buf, "join\n") == 0){
printf(1,"chose tid: ");
if(read(0, buf, 100) > 0){
t = atoi(buf);
printf(1, "joining thread tid:%d\n", t);
t = kthread_join(t);
printf(1, "join result:%d \n", t);
} else{
printf(2,"fail\n");
}
} else if(strcmp(buf, "et\n") == 0){
t = kthread_create(exitThread, stack+STACK_SIZE, STACK_SIZE);
printf(1, "creating exit thread tid:%d\n", t);
} else if(strcmp(buf, "exec\n") == 0){
t = kthread_create(execThread, stack+STACK_SIZE, STACK_SIZE);
printf(1, "creating exec thread tid:%d\n", t);
} else if(strcmp(buf, "exit\n") == 0){
printf(1, "exiting program\n");
exit();
} else if(strcmp(buf, "stress\n") == 0){
printf(1,"chose num of threads: ");
if(read(0, buf, 100) > 0){
t = atoi(buf);
stressTest1(t);
} else{
printf(2,"fail\n");
}
} else {
printf(2, "error: %s\n", buf);
}
memset(buf, 0, 100);
}
exit();
}