aboutsummaryrefslogtreecommitdiffstats
path: root/P2/lib
diff options
context:
space:
mode:
authorRossTheRoss <mstrapp@protonmail.com>2020-10-22 07:50:12 -0500
committerRossTheRoss <mstrapp@protonmail.com>2020-10-22 07:50:12 -0500
commit6ff784158f61a082fe6d4332c743a066ef007674 (patch)
tree9148a10ce43358d697d8a8f80bd7cdef7b91e06d /P2/lib
parentget rid of & (diff)
downloadcsci4061-6ff784158f61a082fe6d4332c743a066ef007674.tar
csci4061-6ff784158f61a082fe6d4332c743a066ef007674.tar.gz
csci4061-6ff784158f61a082fe6d4332c743a066ef007674.tar.bz2
csci4061-6ff784158f61a082fe6d4332c743a066ef007674.tar.lz
csci4061-6ff784158f61a082fe6d4332c743a066ef007674.tar.xz
csci4061-6ff784158f61a082fe6d4332c743a066ef007674.tar.zst
csci4061-6ff784158f61a082fe6d4332c743a066ef007674.zip
Add Project 2 Files
Diffstat (limited to '')
-rw-r--r--P2/lib/utils.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/P2/lib/utils.c b/P2/lib/utils.c
new file mode 100644
index 0000000..79b309e
--- /dev/null
+++ b/P2/lib/utils.c
@@ -0,0 +1,92 @@
+#include "utils.h"
+
+char *getChunkData(int mapperID) {
+}
+
+// sends chunks of size 1024 to the mappers in RR fashion
+void sendChunkData(char *inputFile, int nMappers) {
+}
+
+// hash function to divide the list of word.txt files across reducers
+//http://www.cse.yorku.ca/~oz/hash.html
+int hashFunction(char* key, int reducers){
+ unsigned long hash = 0;
+ int c;
+
+ while ((c = *key++)!='\0')
+ hash = c + (hash << 6) + (hash << 16) - hash;
+
+ return (hash % reducers);
+}
+
+int getInterData(char *key, 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();
+} \ No newline at end of file