aboutsummaryrefslogtreecommitdiffstats
path: root/P2/lib
diff options
context:
space:
mode:
Diffstat (limited to 'P2/lib')
-rw-r--r--P2/lib/utils.c42
1 files changed, 31 insertions, 11 deletions
diff --git a/P2/lib/utils.c b/P2/lib/utils.c
index b8922d4..3ec7526 100644
--- a/P2/lib/utils.c
+++ b/P2/lib/utils.c
@@ -2,7 +2,8 @@
int openQueue() {
char cwd [50];
- getcwd(cwd, 50);
+ if(getcwd(cwd, 50) == NULL)
+ return NULL;
return msgget(ftok(cwd, 4061), 0666 | IPC_CREAT);
}
int closeQueue(int id) {
@@ -21,7 +22,8 @@ char *getChunkData(int mapperID) {
struct msgBuffer message = makeMessage();
//Queue ID
int mid = openQueue();
- msgrcv(mid, &message, MSGSIZE, mapperID, 0);
+ if (msgrcv(mid, &message, MSGSIZE, mapperID, 0) == -1)
+ exit(-1);
if (strncmp("END", message.msgText, 3) == 0)
return NULL;
char* value = malloc(1024); // chunkSize or MSGSIZE?
@@ -33,11 +35,18 @@ char *getChunkData(int mapperID) {
void sendChunkData(char *inputFile, int nMappers) {
struct msgBuffer message = makeMessage();
// open message queue
- int msgid = openQueue();
- closeQueue(msgid);
+ int msgid;
+ if (msgid == NULL)
+ exit(-1);
+ if (closeQueue(msgid) == -1)
+ exit(-1);
msgid = openQueue();
+ if (msgid == NULL)
+ exit(-1);
int map = 0;
FILE* file = fopen(inputFile, "r");
+ if (file == NULL)
+ exit(-1);
// construct chunks of 1024 bytes
while(fgets(message.msgText, chunkSize + 1, file) != NULL) {
@@ -45,15 +54,19 @@ void sendChunkData(char *inputFile, int nMappers) {
while(validChar(message.msgText[i])) {
message.msgText[i--] = '\0';
}
- fseek(file, (i - 1023), SEEK_CUR);
+ if (fseek(file, (i - 1023), SEEK_CUR) == -1) {
+ break;
+ }
message.msgType = (map++ % nMappers) + 1;
- msgsnd(msgid, &message, MSGSIZE, 0);
+ if (msgsnd(msgid, &message, MSGSIZE, 0) == -1)
+ exit(-1);
}
for (int i = 1; i <= nMappers; i++) {
struct msgBuffer END = {i, "END"};
- msgsnd(msgid, &END, MSGSIZE, 0);
- }
+ if (msgsnd(msgid, &END, MSGSIZE, 0) == -1)
+ exit(-1);
+ }
fclose(file);
}
@@ -73,7 +86,10 @@ int getInterData(char *Qkey, int reducerID) {
struct msgBuffer message= makeMessage();
//DEBUG! make sure it work.
int id = openQueue();
- msgrcv(id, &message, MSGSIZE, reducerID, 0);
+ if (id == NULL)
+ exit(-1);
+ if (msgrcv(id, &message, MSGSIZE, reducerID, 0) == -1)
+ exit(-1);
strcpy(Qkey, message.msgText);
return (strncmp("END", message.msgText, 3) != 0);
}
@@ -87,19 +103,23 @@ void shuffle(int nMappers, int nReducers) {
char newpath[100];
sprintf(newpath, "output/MapOut/Map_%d", i); // Removed /, add to current dir
DIR *dir = opendir(newpath);
+ if (dir == NULL)
+ break;
struct dirent* entry;
while ((entry = readdir(dir)) != NULL) {
if (!strcmp(".", entry->d_name) || !strcmp("..", entry->d_name))
continue;
sprintf(message.msgText, "%s/%s", newpath, entry -> d_name);
message.msgType = (hashFunction(entry -> d_name, nReducers)+1);
- msgsnd(id, &message, MSGSIZE, 0);
+ if (msgsnd(id, &message, MSGSIZE, 0) == NULL)
+ exit(-1);
}
closedir(dir);
}
for (int i = 1; i <= nReducers; i++) {
struct msgBuffer END = {i, "END"};
- msgsnd(id, &END, MSGSIZE, 0);
+ if (msgsnd(id, &END, MSGSIZE, 0))
+ break;
}
}