-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
145 lines (117 loc) · 3.02 KB
/
main.c
File metadata and controls
145 lines (117 loc) · 3.02 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "mapreduce.h"
#include "usr_functions.h"
int str_is_decimal_num(char * str)
{
int ret = 1;
char * p = NULL;
if (NULL == str)
{
printf("error: null input\n");
return 0;
}
p = str;
while (*p)
{
if (*p < '0' || *p > '9')
{
ret = 0;
break;
}
p += 1;
}
return ret;
}
int is_regular_file(char * file_path)
{
struct stat file_stat;
if (-1 == stat(file_path, &file_stat))
{
return 0;
}
if (S_ISREG(file_stat.st_mode))
{
return 1;
}
return 0;
}
void print_usage(char * cmd_name)
{
printf("Usage: %s \"counter\"|\"finder\" file_path split_num [word_to_find]\n", cmd_name);
}
int main(int argc, char * argv[])
{
int i = 0, is_letter_counter = 0;
MAPREDUCE_SPEC spec;
MAPREDUCE_RESULT result;
setbuf(stdout, NULL);
if (argc < 4)
{
print_usage(argv[0]);
exit(1);
}
if (!strcmp(argv[1], "counter"))
{
is_letter_counter = 1;
}
else if (!strcmp(argv[1], "finder"))
{
is_letter_counter = 0;
if (argc < 5) // there must be a argv[4], which is the word to find
{
print_usage(argv[0]);
exit(1);
}
}
else
{
print_usage(argv[0]);
exit(1);
}
// argv[2] is the input data file
if (!is_regular_file(argv[2]))
{
printf("Regular file %s does not exist.\n", argv[2]);
exit(0);
}
// argv[3] is the number of the splits
if (!str_is_decimal_num(argv[3]))
{
printf("%s is not a valide split size. It should be a decimal number. \n", argv[3]);
exit(0);
}
spec.input_data_filepath = argv[2]; // argv[2] is the input data file
spec.split_num = atoi(argv[3]); // argv[3] is the number of the splits
if (is_letter_counter)
{
spec.map_func = letter_counter_map;
spec.reduce_func = letter_counter_reduce;
spec.usr_data = NULL;
}
else
{
spec.map_func = word_finder_map;
spec.reduce_func = word_finder_reduce;
spec.usr_data = argv[4]; // argv[4] is the word to find
}
result.filepath = "output.rst"; // name of the output file (placed in the working directory)
result.map_worker_pid = malloc(spec.split_num * sizeof(*result.map_worker_pid));
if (NULL == result.map_worker_pid)
{
printf("Memory allocation failed!\n");
exit(2);
}
mapreduce(&spec, &result);
printf("\n---FINAL OUTPUT---\n");
printf("Output file saved at: %s\n", result.filepath);
printf("Map worker process IDs: \n");
for (i = 0; i < spec.split_num; i++) printf("%d \n", result.map_worker_pid[i]);
printf("\n");
printf("Reduce worker process ID:\n %d\n", result.reduce_worker_pid);
printf("Total processing time (microseconds):\n %d\n\n", result.processing_time);
free(result.map_worker_pid);
exit(0);
}