aboutsummaryrefslogtreecommitdiffstats
path: root/P2/lib/utils.c
blob: f2dc4580e1284e3be82cd99427a7a1f91be0a5fd (plain) (blame)
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
#include "utils.h"

int openQueue(char* path) {
	return msgget(ftok(path, 253), 0666 | IPC_CREAT);
}
int closeQueue(int id) {
	return msgctl(id, IPC_RMID, NULL);
}

struct msgBuffer makeMessage() {
	struct msgBuffer temp;
	memset(temp.msgText, '\0', MSGSIZE);
	temp.msgType = 0;
	return temp;
}

char *getChunkData(int mapperID) {
	//Message
	struct msgBuffer message = makeMessage();
	//Queue ID
	int mid = openQueue("map");
	printf("%d\n", mapperID);
	msgrcv(mid, &message, sizeof(message.msgText), mapperID, 0);
	// printf("\n%s\n", message.msgText);
	// printf("%d\n", strncmp("END", message.msgText, 3));
	if (!strncmp("END", message.msgText, 3))
	{
		printf("END\n");
		return NULL;
	}
	char* value = message.msgText;
	printf("%s\n", message.msgText);
	return value;
	//return &(message.msgText);
}

// sends chunks of size 1024 to the mappers in RR fashion
void sendChunkData(char *inputFile, int nMappers) {
	printf("SENDING CHUNK DATA\n");
	struct msgBuffer message = makeMessage();
	// open message queue
	int msgid = openQueue("map");
	int map = 1;
	FILE* file = fopen(inputFile, "r");
	// construct chunks of 1024 bytes
	while(fgets(message.msgText, chunkSize + 1, file) != NULL) {

		int i = 1023;
		while((validChar(message.msgText[i]) )) {
			message.msgText[i] = '\0';
			i--;
		}
		// DEBUG!

		fseek(file, (i - 1023), SEEK_CUR);
		//The first mapper sent to is 2 instead of 1.
		//Is this a problem? Probably not.
		message.msgType = (map++ % nMappers) + 1;
		msgsnd(msgid, &message, map, 0);
	}

	for (int i = 1; i <= nMappers; i++) {
		struct msgBuffer END = {i, "END"};
		msgsnd(msgid, &END, MSGSIZE, 0);
	}
	fclose(file);
}

// hash function to divide the list of word.txt files across reducers
//http://www.cse.yorku.ca/~oz/hash.html
int hashFunction(char* Qkey, int reducers){
	unsigned long hash = 0;
    int c;

    while ((c = *Qkey++)!='\0')
        hash = c + (hash << 6) + (hash << 16) - hash;

    return (hash % reducers);
}

int getInterData(char *Qkey, int reducerID) {
	struct msgBuffer message= makeMessage();
	//DEBUG! make sure it work.
	// How do we traverse the directory if we're not given it as an arg?
	int id = openQueue("reduce");
	msgrcv(id, &message, chunkSize, reducerID, 0);
	*Qkey = *message.msgText;
	printf("%s\n", Qkey);
	return abs(strncmp("END", message.msgText, 3));
	// if (strncmp("END", message.msgText, 3))
	// {
	// 	return 0;
	// } else {
	// 	return 1;
	// }
}

void shuffle(int nMappers, int nReducers) {
	struct msgBuffer message = makeMessage();
	//Once again, MAKE SURE THIS WORKS PROPERLY!
	int id = openQueue("reduce");
	for (int i = 1; i <= nMappers; i++) {
		//Extra for loop traversing directory
		//TODO: Actually traverse directory
			//message.msgType = hashFunction(/* SOMETHING ,*/ nReducers);
			msgsnd(id, &message, chunkSize, 0);
	}

}

// check if the character is valid for a word
int validChar(char c){
	return (tolower(c) >= 'a' && tolower(c) <='z') ||
					(c >= '0' && c <= '9');
}

char *getWord(char *chunk, int *i){
	char *buffer = (char *)malloc(sizeof(char) * chunkSize);
	memset(buffer, '\0', chunkSize);
	int j = 0;
	while((*i) < strlen(chunk)) {
		// read a single word at a time from chunk
		// printf("%d\n", i);
		if (chunk[(*i)] == '\n' || chunk[(*i)] == ' ' || !validChar(chunk[(*i)]) || chunk[(*i)] == 0x0) {
			buffer[j] = '\0';
			if(strlen(buffer) > 0){
				(*i)++;
				return buffer;
			}
			j = 0;
			(*i)++;
			continue;
		}
		buffer[j] = chunk[(*i)];
		j++;
		(*i)++;
	}
	if(strlen(buffer) > 0)
		return buffer;
	return NULL;
}

void createOutputDir(){
	mkdir("output", ACCESSPERMS);
	mkdir("output/MapOut", ACCESSPERMS);
	mkdir("output/ReduceOut", ACCESSPERMS);
}

char *createMapDir(int mapperID){
	char *dirName = (char *) malloc(sizeof(char) * 100);
	memset(dirName, '\0', 100);
	sprintf(dirName, "output/MapOut/Map_%d", mapperID);
	mkdir(dirName, ACCESSPERMS);
	return dirName;
}

void removeOutputDir(){
	pid_t pid = fork();
	if(pid == 0){
		char *argv[] = {"rm", "-rf", "output", NULL};
		if (execvp(*argv, argv) < 0) {
			printf("ERROR: exec failed\n");
			exit(1);
		}
		exit(0);
	} else{
		wait(NULL);
	}
}

void bookeepingCode(){
	removeOutputDir();
	sleep(1);
	createOutputDir();
}