-
Notifications
You must be signed in to change notification settings - Fork 0
/
threadtests.c
406 lines (358 loc) · 9.45 KB
/
threadtests.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "param.h"
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fs.h"
#include "fcntl.h"
#include "syscall.h"
void testfunc(void *arg1, void *arg2){
printf(1, "Printing by threads\n");
exit();
}
// test for one thread creation
int cloneTest(void){
printf(1, "CloneTest starting\n");
int clone_tid, join_tid;
void *stack = malloc(4096);
int arg1 = 10;
char *arg2 = "string arg";
clone_tid = clone(testfunc, &arg1, arg2, stack, CLONE_THREAD | CLONE_VM | CLONE_FILES | CLONE_FS | CLONE_PARENT);
join_tid = join(stack);
free(stack);
if(clone_tid == join_tid){
printf(1, "CloneTest passed\n\n");
} else{
printf(1, "CloneTest failed\n\n");
}
return 0;
}
// test for creating thread in fork
int cloneInForkTest(void){
printf(1, "CloneInForkTest starting\n");
int tgid, clone_tid, join_tid;
int arg1 = 10;
char *arg2 = "string arg";
void *stack;
tgid = fork();
if (!tgid){
// child
stack = malloc(4096);
clone_tid = clone(testfunc, &arg1, arg2, stack, CLONE_THREAD | CLONE_VM | CLONE_FILES | CLONE_FS | CLONE_PARENT);
join_tid = join(stack);
free(stack);
if(clone_tid == join_tid){
printf(1, "CloneInForkTest passed\n\n");
} else{
printf(1, "CloneInForkTest failed\n\n");
}
exit();
}
wait();
return 0;
}
// test for creating multiple threads
int mutlipleCloneTest(void){
printf(1, "MutlipleCloneTest starting\n");
void *stack1 = malloc(4096);
void *stack2 = malloc(4096);
int arg1 = 10;
char *arg2 = "string arg";
clone(testfunc, &arg1, arg2, stack1, CLONE_THREAD | CLONE_VM | CLONE_FILES | CLONE_FS | CLONE_PARENT);
join(stack1);
clone(testfunc, &arg1, arg2, stack2, CLONE_THREAD | CLONE_VM | CLONE_FILES | CLONE_FS | CLONE_PARENT);
join(stack2);
free(stack1);
free(stack2);
printf(1, "MutlipleCloneTest passed\n\n");
return 0;
}
void clonetestfunc(void *arg1, void *arg2){
int tgid;
tgid = fork();
if (!tgid){
printf(1,"In Forked Child\n");
exit();
}
wait();
printf(1,"In Forked Parent\n");
exit();
}
// test for using clone in fork
int forkInCloneTest(){
printf(1, "ForkInCloneTest starting\n");
int one1, two1;
void *stack1;
one1 = 1;
two1 = 1;
stack1 = malloc(4096);
clone(clonetestfunc, &one1, &two1, stack1, CLONE_VM | CLONE_PARENT);
join(stack1);
free(stack1);
printf(1, "ForkInCloneTest passed\n\n");
return 0;
}
// test for userland thread library
int userlandFuncTest(void){
printf(1, "UserlandFuncTest starting\n");
int i = 0;
int create_pid, join_pid;
create_pid = thread_create(testfunc, &i, &i);
join_pid = thread_join();
if(create_pid == join_pid){
printf(1, "UserlandFuncTest passed\n\n");
} else{
printf(1, "UserlandFuncTest failed\n\n");
}
return 0;
}
// Matrix multiplication code
#define MAX_SIZE 100000
int count = 0, i, j, k;
long int *matrix1[MAX_SIZE];
long int *matrix2[MAX_SIZE];
long int *result[MAX_SIZE];
void calculate(){
int divider = count++;
for(int a = divider * i/3; a < (divider+1) * i/3; a++){
for (int b = 0; b < k; b++)
for (int c = 0; c < j; c++)
result[a][b] += matrix1[a][c] * matrix2[c][b];
}
exit();
}
int matmulTest(){
printf(1, "MatrixMultplicationTest starting\n");
int row, col;
i = 3;
j = 2;
int value = 0;
for(row = 0; row < i; row++){
matrix1[row] = (long int *) malloc(j * sizeof(long int));
}
for(row = 0; row < i; row++){
for(col = 0; col < j; col++){
matrix1[row][col] = value++;
}
}
int isMultiplicable;
isMultiplicable = 2;
k = 1;
if(isMultiplicable != j){
printf(1, "Check Dimensions for multiplication\n");
return 1;
}
for(row = 0; row < j; row++){
matrix2[row] = (long int *) malloc(k * sizeof(long int));
}
for(row = 0; row < j; row++){
for(col = 0; col < k; col++){
matrix2[row][col] = value++;
}
}
for(row = 0; row < i; row++){
result[row] = (long int *) malloc(k * sizeof(long int));
}
int thread_id = 1;
for(int thread_num = 0; thread_num < 3; thread_num++){
thread_create(calculate, &thread_id, &thread_id);
}
for(int thread_num = 0; thread_num < 3; thread_num++){
thread_join();
}
printf(1, "Output: \n");
for(row = 0; row < i; row++){
for(col = 0; col < k; col++){
printf(1, "%d", result[row][col]);
}
printf(1, "\n");
}
printf(1, "MatrixMultplicationTest passed\n\n");
return 0;
}
void exectestfunc(void *arg1, void *arg2) {
char *echoargv[] = { "echo", "From echo:", "EchoinCloneTest","passed", 0 };
exec("echo", echoargv);
exit();
}
int execInCloneTest(void){
printf(1, "ExecInCloneTest starting\n");
int one1, two1;
void *stack1;
one1 = 1;
two1 = 1;
stack1 = malloc(4096);
clone(exectestfunc, &one1, &two1, stack1, CLONE_VM | CLONE_PARENT | CLONE_THREAD );
join(stack1);
free(stack1);
printf(1, "ExecInCloneTest passed\n\n");
return 0;
}
void func(void *arg1, void *arg2)
{
printf(1, "Child1 pid = %d\n", getpid());
printf(1, "Child1 tid = %d\n", gettid());
if(gettid() > 2 && getpid() > 2){
printf(1, "getTidTest passed\n\n");
} else{
printf(1, "getTidTest failed\n\n");
}
exit();
}
int getTidTest(void){
printf(1, "getTidTest passed\n");
void *stack = malloc(4096);
int arg1 = 0;
char *arg2 = "Hello";
printf(1, "Parent tid = %d\n", gettid());
printf(1, "Parent pid = %d\n", getpid());
clone(&func, &arg1, arg2, stack, CLONE_THREAD);
join(stack);
return 0;
}
tlock mylock;
int global = 0;
void lockfunc1(void *arg1, void *arg2) {
for (int i = 0; i < 10; i++) {
lock_acquire(&mylock);
global++;
lock_release(&mylock);
}
exit();
}
void lockfunc2(void *arg1, void *arg2) {
for (int i = 1; i < 10; i++) {
lock_acquire(&mylock);
global--;
lock_release(&mylock);
}
exit();
}
int locksTest(void){
printf(1, "LocksTest starting\n");
lock_init(&mylock);
int i = 0;
thread_create(lockfunc1, &i, &i);
thread_create(lockfunc2, &i, &i);
thread_join();
thread_join();
if(global == 1){
printf(1, "LocksTest passed\n\n");
} else{
printf(1, "LocksTest failed\n\n");
}
return 0;
}
int tgkillTest(void){
printf(1, "tgKillTest started\n");
void *stack = malloc(4096);
int arg1 = 0;
char *arg2 = "Hello";
int cloned_tid = clone(&testfunc, &arg1, arg2, stack, CLONE_THREAD);
int killed = tgkill(getpid(), cloned_tid);
if(killed == 0){
printf(1, "tgKillTest passed\n\n");
} else{
printf(1, "tgKillTest failed\n\n");
}
return 0;
}
void ffunc(void *arg1, void *arg2) {
(*(int*) arg1)++;
exit();
}
int maxThreadsTest(void){
printf(1, "MaximumThreadTest starting\n");
void *stack[100];
int arg1 = 0, total = 1;
int n = 100;
for(int i = 0; i < n ; i++){
stack[i] = malloc(4096);
if(clone(&ffunc, &arg1, &arg1, stack[i], CLONE_THREAD | CLONE_VM) < 0){
break;
} else{
total++;
}
}
for(int i = 0; i < total; i++){
join(stack[i]);
free(stack[i]);
}
printf(1, "Maximum Thread Limit reached %d\n\n", total);
return 0;
}
int limitedThreadsTest(void){
printf(1, "LimitedThreadsTest starting\n");
void *stack[50];
int arg1 = 0, total = 1;
int n = 50;
for(int i = 0; i < n ; i++){
stack[i] = malloc(4096);
if(clone(&ffunc, &arg1, &arg1, stack[i], CLONE_THREAD | CLONE_VM) < 0){
printf(1, "Limited Threads Test Failed\n\n");
} else{
total++;
}
}
for(int i = 0; i < n; i++){
join(stack[i]);
free(stack[i]);
}
printf(1, "Limited Threads Test Passed %d\n\n", total);
return 0;
}
void fstestfunc(void *a, void *b){
mkdir("test");
chdir("test");
exit();
}
int cloneFSTest(void)
{
printf(1, "clone fs starting\n");
void *stack = malloc(4096);
int arg1 = 0;
clone(fstestfunc, &arg1, &arg1, stack, CLONE_THREAD | CLONE_FS);
join(stack);
if (chdir("../") < 0){
printf(1, "clone fs test FAILED\n");
return 0;
}
if (unlink("test") < 0){
printf(1, "unlink failed\n");
return 0;
}
free(stack);
printf(1, "CloneFsTest passed\n");
return 0;
}
void filesfunc(void *arg1, void *arg2) {
write(*(int*)arg1, "Hello\n", strlen("Hello\n"));
exit();
}
int cloneFilesTest(void){
printf(1, "CloneFilesTest started\n");
void *stack = malloc(4096);
int fd = open("flags.txt", O_RDONLY|O_WRONLY|O_CREATE);
clone(filesfunc, &fd, &fd, stack, CLONE_THREAD | CLONE_FILES);
join(stack);
write(fd, "in MAIN\n", strlen("in MAIN\n"));
free(stack);
printf(1, "CloneFilesTest Passed\n\n");
return 0;
}
int main(int argc, char *argv[]){
cloneTest();
cloneInForkTest();
cloneFSTest();
cloneFilesTest();
mutlipleCloneTest();
forkInCloneTest();
userlandFuncTest();
getTidTest();
locksTest();
tgkillTest();
maxThreadsTest();
limitedThreadsTest();
matmulTest();
printf(1, "All THREADTESTS COMPLETED\n");
exit();
}