-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_clone_join.c
106 lines (69 loc) · 1.52 KB
/
test_clone_join.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
#include "types.h"
#include "user.h"
#include "spinlock.h"
int mypthread_create(void (*start_routine) (void *), void *arg)
{
int pid;
void *stack = malloc(4096);
pid = clone(start_routine, arg, stack);
if(!pid)
(*start_routine)(arg);
free(stack);
return pid;
}
int mypthread_join()
{
void **stack = malloc(4096);
return join(stack);
}
/*
void mypthread_yield()
{
sleep();
return ;
}
*/
/*
void clone_test()
{
int x = 5, *p = &x;
p += 2;
printf(1, "\nInside clone_test()");
//printf(2, "\nReturn address [from clone_test()]: %x\n", p);
//printf(2, "\n%d\n", getpid());
exit();
}
int main(void)
{
int num = 8, ret = 10;
char *stack = malloc(4096);
char *stack3 = malloc(4096);
char *stack2 = malloc(4096);
char **ptr_stack2 = &stack2;
ret = clone(clone_test, (void *) num, (void *) stack);
ret = clone(clone_test, (void *) num, (void *) stack3);
printf(2, "\nWaiting for clone_test() to exit\nClone returned %d\n", ret);
printf(2, "\nJoin returned %d\n", join((void **) ptr_stack2));
printf(2, "\nJoin returned %d\n", join((void **) ptr_stack2));
//listprocesses();
free(stack);
exit();
}
*/
void WTF(void *p) // work time fun
{
printf(2, "Thread %d, in WTF\n", getpid());
printf(2, "Thread %d exiting\n", getpid());
exit();
}
int main(void)
{
int a, b;
printf(2, "Thread %d, in main\n", getpid());
a = mypthread_create(WTF, &a);
mypthread_join();
b = mypthread_create(WTF, &b);
mypthread_join();
printf(2, "Thread %d exiting\n", getpid());
exit();
}