generated from acm-projects/pocket-museum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtree (1).c
318 lines (236 loc) · 9.14 KB
/
htree (1).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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <errno.h> // for EINTR
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <pthread.h>
#include <sys/syscall.h>
#include <string.h>
#include "common_threads.h"
#include "common.h"
// Print out the usage of the program and exit
void Usage(char*);
//Declare Hash function
uint32_t jenkins_one_at_a_time_hash(const uint8_t* , uint64_t );
//Declare tree thread function
void *tree(void *arg);
//Global var nblocks to hold the number of blocks
uint32_t nblocks;
//Global eachBlock to hold the mmaped version of the file
uint8_t *eachBlock;
// block size
#define BSIZE 4096
//struct threadVars for passing variables in the tree subroutine
struct threadVars {
//int threads to hold the number of threads
int threads;
//int index to hold the node index
int index;
//char *hash
char *hash;
};
int
main(int argc, char** argv)
{
//int32_t fd to get file
int32_t fd;
// input checking
if (argc != 3)
Usage(argv[0]);
// open input file
fd = open(argv[1], O_RDWR);
if (fd == -1) {
perror("open failed");
exit(EXIT_FAILURE);
}
// use fstat to get file size
struct stat buf;
fstat(fd,&buf);
size_t size = buf.st_size;
//int threadNum to hold the value of the number of wanted threads
int threadNum = atoi(argv[2]);
//Prints the number of threads wanted
printf("num Threads = %d \n", threadNum);
// calculate nblocks
//nblocks for the number of blocks in total
nblocks = size / BSIZE;
//int nblocksThread to hold number of blocks in one thread
int nblocksThread = nblocks/threadNum;
//Prints the number of number of blocks per thread
printf("Blocks per Thread= %u \n", nblocksThread);
//Print for proper formatting
printf("hash value = ");
//eachblock to hold an mmaped array of the file
eachBlock = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
double start = GetTime();
// calculate hash value of the input file
//thread to create instance for tree class
pthread_t thread;
//Creates struct threadVars vars to format values to send to tree class
struct threadVars vars;
vars.threads = threadNum;
vars.index = 0;
vars.hash = malloc(sizeof(char) * 2000);
//Creates thread to execute tree class
pthread_create(&thread, NULL, tree, (void*)&vars);
//Waits for thread to finish
pthread_join(thread, (void**)&vars);
//Prints hash value of input file
printf("%s \n", vars.hash);
printf("\n");
double end = GetTime();
printf("time taken = %f \n", (end - start));
//Closes file and end programs
close(fd);
return EXIT_SUCCESS;
}
uint32_t
jenkins_one_at_a_time_hash(const uint8_t* key, uint64_t length)
{
uint64_t i = 0;
uint32_t hash = 0;
while (i != length) {
hash += key[i++];
hash += hash << 10;
hash ^= hash >> 6;
}
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
return hash;
}
void
Usage(char* s)
{
fprintf(stderr, "Usage: %s filename num_threads \n", s);
exit(EXIT_FAILURE);
}
//void *tree to execute tree threading of file
void *tree(void* arg){
//Stores argument in struct threadVars vars
struct threadVars* vars = (struct threadVars*) arg;
//Stores thread argument in threadNum
int threadNum = vars->threads;
//Stores index argument in currentIndex
int currentIndex = vars->index;
//Stores hash value in hash
char* hash = vars->hash;
//char tempHolder to temporarily hold the parent node hash value
char tempHolder[100];
//char catHolder to temporarily hold the combined parent/child's node hash values
char catHolder[300];
//blockOffset to hold the offsetted value of the pointer so pointer points to proper index in array
uint8_t *blockOffset = eachBlock + (BSIZE * (currentIndex * (nblocks /threadNum)));
//blockLength to hold length of the block for hashing
uint64_t blockLength = BSIZE * (int) (nblocks / threadNum);
//hasNum to hold the hash value of block by calling jenkins_one_at_a_time_hash function
uint32_t hashNum = jenkins_one_at_a_time_hash(blockOffset, blockLength);
//Converts hashNum into string and saves it in tempHolder
sprintf(tempHolder, "%u", hashNum);
//If statement to check how many child processes the parent node needs to make. If there is 2 child nodes then the following is executed.
if ( (((2 * currentIndex) + 2) <= (threadNum-1)) && (((2 * currentIndex) + 1) < (threadNum-1))){
//leftThread and rightThread to make two child threads
pthread_t leftThread, rightThread;
//char* to hold the hash values of the left child node and right child node
char* hashSectLeft;
char* hashSectRight;
//indexLeft and indexRight to to find index of left and right thread
int indexLeft = (2 * currentIndex) + 1;
int indexRight = (2 * currentIndex) + 2;
//struct to hold arguments to pass arguments to left and right thread
struct threadVars leftVar;
struct threadVars rightVar;
//stores the argument in the struct to pass to left child node
leftVar.threads = threadNum;
leftVar.index = indexLeft;
leftVar.hash = malloc(sizeof(char) * 1000);
//stores the argument in the struct to pass to right child node
rightVar.threads = threadNum;
rightVar.index = indexRight;
rightVar.hash = malloc(sizeof(char) * 1000);
int leftResult;
int rightResult;
leftResult = pthread_create(&leftThread, NULL, tree, (void*)&leftVar);
if (leftResult != 0) {
printf("Error creating thread. Error code: %d\n", leftResult);
}
rightResult = pthread_create(&rightThread, NULL, tree, (void*)&rightVar);
if (rightResult != 0) {
printf("Error creating thread. Error code: %d\n", rightResult);
}
leftResult = pthread_join(leftThread, (void**)&leftVar);
if (leftResult != 0) {
printf("Error joining thread. Error code: %d\n", leftResult);
}
rightResult = pthread_join(rightThread, (void**)&rightVar);
if (rightResult != 0) {
printf("Error joining thread. Error code: %d\n", rightResult);
}
//Stores the returned hash values within respective char holders
hashSectLeft = leftVar.hash;
hashSectRight = rightVar.hash;
//temp to hold concatendated hash codes
char temp[300];
//Combines the different hash codes for child and parent and stores them in catHolder
strcat(catHolder, tempHolder);
strcat(catHolder, hashSectLeft);
strcat(catHolder, hashSectRight);
//catLength to find length of catHolder for hashing
uint64_t catLength = (uint64_t)strlen(catHolder);
//Finds combined hash code of child and parent threads through calling jenkins_one_at_a_time_hash functiom
uint32_t catHashNum = jenkins_one_at_a_time_hash((uint8_t*)catHolder, catLength);
sprintf(temp, "%u", catHashNum);
strcat(hash, temp);
//If there is only one child thread needed then the following is executed
} else if ( (((2 * currentIndex) + 1) <=(threadNum-1)) && !(((2 * currentIndex) + 2) <=(threadNum-1)) ){
//leftThread and rightThread to make two child threads
pthread_t leftThread;
//char* to hold the hash values of the left child node
char* hashSectLeft;
//indexLeft and indexRight to to find index of left thread
int indexLeft = (2 * currentIndex) + 1;
//struct to hold arguments to pass arguments to left thread
struct threadVars leftVar;
//stores the argument in the struct to pass to left child node
leftVar.threads = threadNum;
leftVar.index = indexLeft;
leftVar.hash = malloc(sizeof(char) * 1000);
//temp to hold concatendated hash codes
char temp[300];
int leftResult;
leftResult = pthread_create(&leftThread, NULL, tree, (void*)&leftVar);
if (leftResult != 0) {
printf("Error creating thread. Error code: %d\n", leftResult);
}
leftResult = pthread_join(leftThread, (void**)&leftVar);
if (leftResult != 0) {
printf("Error joining thread. Error code: %d\n", leftResult);
}
//Stores the returned hash values within hashSectLeft
hashSectLeft = leftVar.hash;
//Combines the different hash codes for child and parent and stores them in catHolder
strcat(catHolder, tempHolder);
strcat(catHolder, hashSectLeft);
//catLength to find length of catHolder for hashing
uint64_t catLength = (uint64_t)strlen(catHolder);
//Finds combined hash code of child and parent threads through calling jenkins_one_at_a_time_hash functiom
uint32_t catHashNum = jenkins_one_at_a_time_hash((uint8_t*)catHolder, catLength);
sprintf(temp, "%u", catHashNum);
strcat(hash, temp);
//If there is no child node, then current thread node is a leaf node, simply adds hash code on and returns
} else {
strcat(hash, tempHolder);
}
//Stores the newly changed arg values back into original vars arg struct
vars->threads = threadNum;
vars->index = currentIndex;
vars->hash = hash;
//Exits current struct
pthread_exit(vars);
return NULL;
}