-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapred.c
More file actions
366 lines (313 loc) · 8.72 KB
/
Copy pathmapred.c
File metadata and controls
366 lines (313 loc) · 8.72 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
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
/*
* @author: Hua Yang
* @RUID: 128-00-2637
* @author: Erik Kamp
* @RUID: 132-00-4838
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <unistd.h>
#include <ctype.h>
#include <math.h>
#include <pthread.h>
#include "uthash.h"
#include "mapred.h"
#define wordBufferSize 4096
/*
* @param word : the desired word that one wishes to know the value of it is the ket and this will get the value
*/
wordDictionaryPtr global_wdptr = NULL;
void
addWordReduce(char* word, wordDictionaryPtr* wdptr,int count)
{
wordDictionaryPtr tempNode = NULL;
HASH_FIND_STR(*wdptr, word, tempNode);
if (tempNode) {
tempNode->value+= count;
} else {
wordDictionaryPtr addWord;
addWord = malloc(sizeof(struct wordDictionary));
addWord->key = (char*) calloc(strlen(word), sizeof(char));
strcpy(addWord->key, word);
addWord->value = 1;
HASH_ADD_STR(*wdptr, key, addWord);
}
}
/*
* Create a new hash struct with the given word,
* then insert into the main hash.
* @param word : word to be added to the hash table
*/
void
addWord(char* word, wordDictionaryPtr* wdptr)
{
wordDictionaryPtr tempNode = NULL;
HASH_FIND_STR(*wdptr, word, tempNode);
if (tempNode) {
tempNode->value++;
} else {
wordDictionaryPtr addWord;
addWord = malloc(sizeof(struct wordDictionary));
addWord->key = (char*) calloc(strlen(word), sizeof(char));
strcpy(addWord->key, word);
addWord->value = 1;
HASH_ADD_STR(*wdptr, key, addWord);
}
}
int
sortWord(wordDictionaryPtr ptr1, wordDictionaryPtr ptr2)
{
return strcmp(ptr1->key, ptr2->key);
}
/*
* @param fileName : the name of the file to be split into new files containing each 1000 orso to be read faster by the mappers
* @param numberOfPieces : the number of pieces the file will be split into. This will coorespond to the number of mappers so that one mapper gets one piece
*/
void
deleteGeneratedFiles(char* file)
{
int len = strlen(file) + 5;
char* cmd = (char*) calloc(len, sizeof(char));
strcat(cmd, "rm ");
strcat(cmd, file);
strcat(cmd, ".*");
cmd[len] = '\0';
system(cmd);
}
void
splitFile(char* fileName, char* numberOfPieces)
{
pid_t childLabor, lazyParent;
int status = 0;
childLabor = fork();
if (childLabor == 0) {
char* splitFile[] = {"./split.sh", fileName, numberOfPieces, '\0'};
if (execvp("./split.sh",splitFile) < 0) {
perror("./split.sh");
}
} else if (childLabor < 0) {
perror("child");
} else {
//int returnStatus;
//waitpid(childLabor, &returnStatus, 0);
}
while ((lazyParent = wait(&status)) > 0) {
// wait for child to finish
}
}
void
free_uthash(wordDictionaryPtr wdptr)
{
wordDictionaryPtr ptr;
for(ptr = wdptr; ptr != NULL; ptr = ptr->hh.next) {
HASH_DEL(wdptr, ptr);
free(ptr);
}
}
void
print_words(wordDictionaryPtr* wdptr)
{
wordDictionaryPtr s;
for(s = *wdptr; s != NULL; s = s->hh.next) {
printf("Key %s: Value %d\n", s->key, s->value);
}
}
/*
* This funciton reads in a file and prints it to the console taking in the name of the file as input.
* @param name : String that contains the name of the input file
*/
void
readFile(char* name, wordDictionaryPtr* wdptr)
{
int c;
FILE* fp;
struct dirent *data;
char *nextName,
*relPath,
*token,
*saveptr;
char word[wordBufferSize];
wordDictionaryPtr tempWDPtr;
//printf("\nThe name of the file is |%s| \n",name);
//Check to make sure the name is legal
if(name == NULL || name[strlen(name)-1] == '.') {
return;
}
//Make sure the file exists
if ((fp = fopen(name, "r")) != NULL) {
//Loop over the characters of the file in order to print them to the console
while (fgets(word, wordBufferSize, fp) != NULL) {
token = strtok_r(word, " .,:;!?&$@|[]{}/\'\"*#-_<>()~\r\n",&saveptr);
while (token) {
addWord(token, wdptr);
token = strtok_r(NULL, " .,:;!?&$@|[]{}/\'\"*#-_<>()~\r\n",&saveptr);
}
}
fclose(fp);
} else {
fprintf(stderr , "ERROR: %s is not a file or directory.\n", name);
}
}
void
mapFile(char* infile, int fileNum, wordDictionaryPtr *holder)
{
int len;
char *file, *buffer;
if (fileNum == 0)
len = 1;
else
len = floor(log10(abs(fileNum))) + 1;
file = (char*) calloc(strlen(infile) + len + 2, sizeof(char));
buffer = (char*) calloc(len, sizeof(char));
sprintf(buffer, "%d", fileNum);
strcat(file, infile);
strcat(file, ".");
strcat(file, buffer);
strcat(file, "\0");
readFile(file, holder);
printf("%s.%d have %d words\n", infile, fileNum, HASH_COUNT(*holder));
}
void*
mapperController(void *arg)
{
_thread_id *p = (_thread_id *)arg;
mapFile(p->file, p->id, &(p->wdptr) );
return (NULL);
}
void
masterReduce(wordDictionaryPtr *master,wordDictionaryPtr *wdptr)
{
wordDictionaryPtr s;
for(s = *wdptr; s != NULL; s = s->hh.next) {
addWordReduce(s->key, master, s->value);
}
}
/*
* This function will launch all the mappers, and the mappers as specified by the user each one getting a certain setion of the input file
* @param numberOfMappers : number of mappers to be run at the same time
* @param baseFileName : the base name of the file that was split via the split command
* @param numberOfReducers : the number of reduces to be run at one time
*/
void
runTheMappers(int numberOfMappers, char* baseFileName, int numberOfReducers)
{
int i, r;
pthread_t *threads;
pthread_attr_t pthread_custom_attr;
_thread_id *p;
threads = (pthread_t *) malloc(numberOfMappers*sizeof(*threads));
pthread_attr_init(&pthread_custom_attr);
p = (_thread_id *) malloc(sizeof(_thread_id)*numberOfMappers);
/* Start up thread */
for (i = 0; i < numberOfMappers; i++) {
p[i].id = i;
p[i].file = (char*) calloc(strlen(baseFileName), sizeof(char));
strcpy(p[i].file, baseFileName);
p[i].wdptr = NULL;
pthread_create(&threads[i], &pthread_custom_attr, mapperController, (void *)(p+i));
}
/* Synchronize the completion of each thread. */
for (i = 0; i < numberOfMappers; i++) {
pthread_join(threads[i],NULL);
}
for (i = 0; i < numberOfMappers; i++) {
masterReduce(&global_wdptr,&(p[i].wdptr));
}
for (i = 0; i < numberOfMappers; i++) {
free_uthash(p[i].wdptr);
free(p[i].file);
}
}
void
writeWordCount(char* file, wordDictionaryPtr* wdptr)
{
FILE* fp;
wordDictionaryPtr ptr;
if ((fp = fopen(file, "w+")) != NULL) {
for(ptr = *wdptr; ptr != NULL; ptr = ptr->hh.next)
fprintf(fp, "%s : %d\n", ptr->key, ptr->value);
}
fclose(fp);
}
void
writeWordSort(char* file, wordDictionaryPtr* wdptr)
{
FILE* fp;
wordDictionaryPtr ptr;
if ((fp = fopen(file, "w+")) != NULL) {
HASH_SORT(*wdptr, sortWord);
for(ptr = *wdptr; ptr != NULL; ptr = ptr->hh.next)
fprintf(fp, "%s\n", ptr->key);
}
fclose(fp);
}
int
main(int argc, char** argv)
{
FILE *input,
*output;
char safety,
tooMany;
char *typeOfRun,
*threadOrProc,
*infile,
*outfile,
*numberOfMappers;
int numberOfReducers;
// assign the variables from the input taken from the command line args
typeOfRun = argv[2];
threadOrProc = argv[4];
numberOfMappers = argv[6];
numberOfReducers = atoi(argv[8]);
infile = argv[9];
outfile = argv[10];
// if incorrect number of arguments
if (argc != 11) {
fprintf(stderr, "ERROR: Incorrect number of arguments\n");
return 0;
}
if (strcmp(threadOrProc, "threads") != 0) {
fprintf(stderr, "The program will only run threads!\nEnding program.\n");
return 0;
}
// disallow overwiting our source files in the output
if (strcmp(outfile,"mapred.o") == 0 ||
strcmp(outfile,"Makefile") == 0 ||
strcmp(outfile,"readme.pdf") == 0) {
fprintf(stderr, "Please don't try to overwrite our sourcefiles with the output\n");
return 0;
}
if ((input = fopen(infile, "r")) == NULL) {
fclose(input);
fprintf(stderr, "The input file, %s, does not exist!\nEnding program.\n", infile);
return 0;
} else {
fclose(input);
}
if ((output = fopen(outfile, "r")) != NULL) {
fclose(output);
fprintf(stdout, "The file designated for output already exists, are you sure you wish to overwite it? (y/n): ");
if(fscanf(stdin, "%c%c", &safety, &tooMany)!=2||safety!='y'||tooMany!='\n') {
fprintf(stdout, "Ending program.\n");
return 0;
}
}
// set the out file as the first argument from the commandline
output = fopen(outfile,"w");
// before reading in the file make sure to split, the file in 25 or less parts, in his example he uses 25 soooo im just going to use it for now
splitFile(infile,numberOfMappers);
// run the mappers
runTheMappers(atoi(numberOfMappers), infile, numberOfReducers);
// check if output in word count format or sort format
if (strcmp(typeOfRun, "sort") == 0) {
writeWordSort(outfile, &global_wdptr);
} else {
writeWordCount(outfile, &global_wdptr);
}
fclose(output);
deleteGeneratedFiles(infile);
return 0;
}