From e17200de88db73dbad4263a3de6d2f8128e9fdc3 Mon Sep 17 00:00:00 2001
From: Santiago Lo Coco <slococo@itba.edu.ar>
Date: Sat, 4 Sep 2021 12:41:14 -0300
Subject: [PATCH] Change the use of pipes in master

Co-authored-by: Ezequiel Bellver <ebellver@itba.edu.ar>
Co-authored-by: Juan Barmasch <jbarmasch@itba.edu.ar>
---
 Makefile | 14 +++++++-------
 master.c | 25 +++++++++++--------------
 slave.c  | 14 +++++++-------
 3 files changed, 25 insertions(+), 28 deletions(-)

diff --git a/Makefile b/Makefile
index 2efc877..fc17205 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
 CC = gcc
-CCFLAGS = -Wall -std=c99 -pedantic # -g (si queremos debuggear)
+CCFLAGS = -Wall -std=c99 -pedantic -g # (si queremos debuggear)
 
 SOURCES = $(wildcard *.c)
 OBJECTS = $(SOURCES:.c=.o)
@@ -8,14 +8,14 @@ OBJECTS = $(SOURCES:.c=.o)
 
 #%.o : %.c
 #	$(CC) $(CFLAGS) $^ -o $@
-all: master.o error.o slave.o
-	$(CC) $(CFLAGS) -o master master.o error.o
-master.o: master.c error.h
-	$(CC) $(CFLAGS) -c master.c
+all: masterV2.o error.o slave.o
+	$(CC) $(CCFLAGS) -o master masterV2.o error.o
+masterV2.o: masterV2.c error.h
+	$(CC) $(CCFLAGS) -c masterV2.c
 error.o: error.c error.h
-	$(CC) $(CFLAGS) -c error.c
+	$(CC) $(CCFLAGS) -c error.c
 slave.o: slave.c error.o
-	$(CC) $(CFLAGS) slave.c -o slave.o error.o
+	$(CC) $(CCFLAGS) slave.c -o slave.o error.o
 
 clean:
 	rm -rf $(OBJECTS)
diff --git a/master.c b/master.c
index 9ed823d..fec400c 100644
--- a/master.c
+++ b/master.c
@@ -10,7 +10,7 @@
 #include <fcntl.h>
 #include "error.h"
 
-#define SLAVE_NUM 1
+#define SLAVE_NUM 3
 #define MAX_SIZE 200
 
 void create(int ans[2], int pipes[SLAVE_NUM][2]);
@@ -29,7 +29,6 @@ int main(int argc, char *argv[]) {
     fd_set ansSet;
     FD_ZERO(&ansSet);
     FD_SET(ans[0], &ansSet);
-    //printf("%d", ansSet);
 
     fd_set fdSet;
     FD_ZERO(&fdSet);
@@ -55,9 +54,6 @@ int main(int argc, char *argv[]) {
 
             if (write(pipes[j++][1], argv[i], strlen(argv[i]) + 1) < 0) {
             //if (write(pipes[j++][1], "prueba.cnf\n", strlen(argv[i]) + 1) < 0) {
-                printf("quesesto %d\n", fcntl(pipes[j-1][1], F_GETFD));
-                printf("%s\n", argv[i]);
-                printf("fd write: %d\n", pipes[j-1][0]);
                 printSystemError("write() -- ERROR");
             }
             //close(pipes[j-1][1]);
@@ -82,16 +78,17 @@ int main(int argc, char *argv[]) {
 
     int fd;
     char buf[MAX_SIZE];
-    if ((fd = select(4, &ansSet, NULL, NULL, NULL)) < 0)
+    if ((fd = select(ans[0] + 1, &ansSet, NULL, NULL, NULL)) < 0)
         printSystemError("select() -- ERROR");
-    int readBytes = 0;
-    do {
-        if ((readBytes += read(ans[0], buf + readBytes, MAX_SIZE)) < 0)
-            printSystemError("read() -- ERROR");
-    } while (buf[readBytes - 1] != '\n');
-    buf[readBytes] = 0;
-    printf("%s\n", buf);
-    //write(pipes[j++][1], "prueba.cnf\n", strlen(argv[i]) + 1);
+    while (fd > 0) {
+        int readBytes = 0;
+        do {
+            if ((readBytes += read(ans[0], buf + readBytes, MAX_SIZE)) < 0)
+                printSystemError("read() -- ERROR");
+        } while (buf[readBytes - 1] != '\n');
+        buf[readBytes] = 0;
+        printf("%s\n", buf);
+    }
         /*
         readBytes = 0;
         do {
diff --git a/slave.c b/slave.c
index 5a3c17d..90aaacd 100644
--- a/slave.c
+++ b/slave.c
@@ -51,6 +51,10 @@ int main(int argc, char *argv[]) {
             inputLine = strtok(NULL, "\n");
         }
     }
+
+
+    // int saved_flags = fcntl(fd, F_GETFL);
+    // fcntl(fd, F_SETFL, saved_flags & ~O_NONBLOCK);
 }
 
 void runCommand(char * inputLine, char * output) {
@@ -73,11 +77,7 @@ void runCommand(char * inputLine, char * output) {
 
 void printOutput(char * output, char * fileName) {
     printf("Nombre del archivo: %s - %s - Id del esclavo: %d\n", fileName, output, getpid());
-}
 
-/*
-void printSystemError(char * string) {
-    perror(string);
-    exit(EXIT_FAILURE);
-}
-*/
\ No newline at end of file
+    // int saved_flags = fcntl(fd, F_GETFL);
+    // fcntl(fd, F_SETFL, saved_flags & O_NONBLOCK);
+}
\ No newline at end of file