IML TD:
[TD_IML.git] / TD2 / exercise3 / exercise.c
diff --git a/TD2/exercise3/exercise.c b/TD2/exercise3/exercise.c
new file mode 100644 (file)
index 0000000..c4e9e41
--- /dev/null
@@ -0,0 +1,41 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/wait.h>
+
+int main() {
+    int pipefd[2];
+    char tmpbuf;
+
+    if (pipe(pipefd) == -1) {
+        perror("Pipe creation failure\n");
+        exit(EXIT_FAILURE);
+    };
+
+    pid_t cpid = fork();
+
+    if (cpid == -1) {
+        perror("fork failure\n");
+        exit(EXIT_FAILURE);
+    } else if (cpid == 0) {
+        close(pipefd[0]);
+        dup2(pipefd[1], STDOUT_FILENO);
+        printf("[%d] Hey Jude\n", getpid());
+        //execl("/bin/ls", "ls", (char*)NULL);
+        close(pipefd[1]);
+        exit(EXIT_SUCCESS);
+    } else {
+        close(pipefd[1]);
+        printf("[%d] REDIRECTION: \n", getpid());
+        while (read(pipefd[0], &tmpbuf, 1) > 0) {
+            write(STDOUT_FILENO, &tmpbuf, 1);
+        }
+        close(pipefd[0]);
+        wait(NULL);
+    }
+
+    return EXIT_SUCCESS;
+}