-
Notifications
You must be signed in to change notification settings - Fork 0
/
clone.c
168 lines (148 loc) · 5.29 KB
/
clone.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
#include"types.h"
#include"stat.h"
#include"user.h"
#include"fs.h"
#include"threads.h"
int global = 0;
int arr1[6][6] = {{1,2,3,2,1,7}, {5,6,8,4,5,3}, {7,4,2,1,5,1}, {9,8,4,5,4,5}, {7,8,4,2,3,7}, {5,4,6,9,8,6}};
int arr2[6][6] = {{5,4,6,9,8,6}, {7,1,2,3,5,1}, {2,8,6,4,1,7}, {8,6,5,3,7,3}, {4,6,5,1,3,8}, {7,4,2,1,5,1}};
int arr3[6][6];
int step = 0;
void matmul(void *arg1, void *arg2){
// printf(1, "%s\n", (char*)arg1);
int div = step++;
if(div != 2){
for(int i = div*(6/3); i < (div + 1)*(6/3); i++){
for(int j = 0; j < 6; j++){
int sum = 0;
for(int k = 0; k < 6; k++){
sum += arr1[i][k]*arr2[k][j];
}
arr3[i][j] = sum;
}
}
}
else{
for(int i = div*(6/3); i < 6; i++){
for(int j = 0; j < 6; j++){
int sum = 0;
for(int k = 0; k < 6; k++){
sum += arr1[i][k]*arr2[k][j];
}
arr3[i][j] = sum;
}
}
}
exit();
}
void F5(void *arg1, void *arg2){
char *exec_arr[] = {"cat", "README"};
int pid = fork();
if(pid == 0){
exec("cat", exec_arr);
printf(1, "Inside child process\n");
}
else{
printf(1, "Inside parent process\n");
wait();
}
wait();
exit();
}
void F4(void *arg1, void *arg2){
printf(1, "%s\n", (char*)arg1);
int ret = thread_kill(thread_gettid(), thread_gettgid(), 5);
printf(1, "%d\n", ret);
printf(1, "%d\n", (int)arg2);
exit();
}
void F3(void *arg1, void *arg2){
printf(1, "%s %d\n", (char*)arg1, (int)arg2);
exit();
}
void F2(void *arg1, void *arg2){
printf(1, "%s %d\n", (char*)arg1, (int)arg2);
char a[100] = "This is F3 clone function called inside F2 clone function and my arg 2 is";
int b = 5;
int flag_count = 4;
int *flags;
flags = malloc(flag_count*sizeof(int));
*(flags + CLONE_THREAD) = 0;
*(flags + CLONE_PARENT) = 0;
*(flags + CLONE_VM) = 0;
*(flags + CLONE_FS) = 0;
int pid = thread_create(&F3, (void*)a, (void*)b, (void*)flag_count, (void*)flags);
thread_join(pid);
exit();
}
void F1(void *arg1, void *arg2){
printf(1, "%s %d\n", (char*)arg1, (int)arg2);
exit();
}
int main(int argc, char *argv[]){
char a[50] = "This is F1 clone function and my arg 2 is";
char c[50] = "This is F2 clone function and my arg 2 is";
char d[100] = "This is F4 clone function and I would not be able to print arg 2 as I will be killed";
char e[100] = "This is F5 clone function and we are doing matrix multiplication here";
char f[100] = "This is F1 clone function and we have set VM flag to 1 and my arg2 is";
char g[100] = "This is F1 clone function and we have set THREAD flag to 1 and my arg2 is";
char h[100] = "This is F1 clone function and we have set VM flag and THREAD flag to 1 and my arg2 is";
int b = 10;
int flag_count = 4;
int *flags;
flags = malloc(flag_count*sizeof(int));
*(flags + CLONE_THREAD) = 0;
*(flags + CLONE_PARENT) = 0;
*(flags + CLONE_VM) = 0;
*(flags + CLONE_FILES) = 0;
printf(1, "\n\n_________CLONE JOIN TESTING_________\n\n");
int pid = thread_create(&F1, (void*)a, (void*)b, (void*)flag_count, (void*)flags);
thread_join(pid);
printf(1, "\n\n_________CLONE INSIDE CLONE WITH JOIN TESTING_________\n\n");
pid = thread_create(&F2, (void*)c, (void*)b, (void*)flag_count, (void*)flags);
thread_join(pid);
printf(1, "\n\n_________CLONE KILL TESTING_________\n\n");
pid = thread_create(&F4, (void*)d, (void*)b, (void*)flag_count, (void*)flags);
thread_join(pid);
printf(1, "\n\n_________FORK-EXEC INSIDE CLONE TESTING_________\n\n");
pid = thread_create(&F5, (void*)d, (void*)b, (void*)flag_count, (void*)flags);
thread_join(pid);
printf(1, "\n\n_________CLONE_VM FLAG TESTING_________\n\n");
*(flags + CLONE_VM) = 1;
pid = thread_create(&F1, (void*)f, (void*)b, (void*)flag_count, (void*)flags);
thread_join(pid);
printf(1, "\n\n_________CLONE_THREAD FLAG TESTING_________\n\n");
*(flags + CLONE_VM) = 0;
*(flags + CLONE_THREAD) = 1;
pid = thread_create(&F1, (void*)g, (void*)b, (void*)flag_count, (void*)flags);
thread_join(pid);
printf(1, "\n\n_________CLONE_THREAD and CLONE_VM FLAG TESTING_________\n\n");
*(flags + CLONE_VM) = 1;
*(flags + CLONE_THREAD) = 1;
pid = thread_create(&F1, (void*)h, (void*)b, (void*)flag_count, (void*)flags);
thread_join(pid);
printf(1, "\n\n_________MATRIX MULTIPLICATION USING CLONE TESTING_________\n\n");
int pid_matmul[3];
*(flags + CLONE_VM) = 1;
for (int i = 0; i < 3; i++){
pid_matmul[i] = thread_create(&matmul, (void*)e, (void*)b, (void*)flag_count, (void*)flags);
}
for (int i = 0; i < 3; i++){
thread_join(pid_matmul[i]);
}
for(int i = 0; i < 6; i++){
for(int j = 0; j < 6; j++){
printf(1, "%d ", arr3[i][j]);
}
printf(1, "\n");
}
printf(1, "\n\n_________MULTIPLE CLONES IN FOR LOOP TESTING_________\n\n");
int pids[100];
for(int i = 0; i < 60; i++){
pids[i] = thread_create(&F1, (void*)a, (void*)b, (void*)flag_count, (void*)flags);
}
for(int i = 0; i < 60; i++){
join(pids[i]);
}
exit();
}