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


char *getChunkData(int mapperID) {
	//Message
	struct msgBuffer message;
	//Queue ID, not sure what it actually does
	int mid;
	//Queue Key, because globals bad
	key_t Qkey = ftok("4061 Project 2 SS", 'S');
	mid = msgget(Qkey, 0666 | IPC_CREAT);
	if (mid < 0) {
		perror("Cannot open queue.\n");
		return NULL;
	}
	msgrcv(mid, &message, sizeof(message.msgText), mapperID, 0);
	// if (strcmp("END", message.msgText)) {
	// 	struct msgBuffer ACK = {mapperID, "ACK"};
	// 	msgsnd(mid, &ACK, MSGSIZE, 0);
	// }
	return message.msgText;
}

// sends chunks of size 1024 to the mappers in RR fashion
void sendChunkData(char *inputFile, int nMappers) {
	struct msgBuffer message;
	int msgid;
	//Hopefully this works
	//TODO: Make sure this value matches the other Qkey
	key_t Qkey = ftok("4061 Project 2 SS", 'S');
	// open message queue
	msgid = msgget(Qkey, 0666 | IPC_CREAT);
	if (msgid < 0) {
		perror("Cannot open queue.\n");
		exit(-1);
	}
	// message.msgText = 1;
	FILE *fptr = fopen(inputFile, "r");

	// construct chunks of 1024 bytes
	while(fgets(&message, chunkSize, fptr) != EOF) {

		/*  Go to the end of the chunk, check if final character 
		    is a space if character is a space, do nothing
		    else cut off before that word and put back file      */
		// TODO! help 

		msgsnd(msgid, &message, 11, 0);
	}

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

		// TODO! does this need to be in another loop or is blocking good enough?
		msgrcv(msgid, &message, MSGSIZE, i, 0);
	}

}

// 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) {
}

void shuffle(int nMappers, int nReducers) {
}

// 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();
}