IML TD(take2):
[TD_IML.git] / TD1 / part_two / exo.c
index 0194d04e47ec22387c9aaa95ce3c5a13079de811..2f5e419062f7be8cdbbb37bc8ab14e23916da617 100644 (file)
@@ -1,11 +1,11 @@
-#include <sys/types.h>
-#include <sys/wait.h>
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
+#include <sys/types.h>
+#include <sys/wait.h>
+
 void show_proc_pids() {
     pid_t current_pid = getpid();
     pid_t parent_pid = getppid();
@@ -33,26 +33,22 @@ int main() {
     } else if (cpid == 0) {
         printf("Fils:\n");
         show_proc_pids();
-        const char* msg = "Coucou papa !";
+        const char* str_fmt = "[%d] Coucou papa !\n";
         close(pipefd[0]);
-        write(pipefd[1], msg, strlen(msg));
+        write(pipefd[1], str_fmt, strlen(str_fmt));
         close(pipefd[1]);
         exit(EXIT_SUCCESS);
     } else {
         printf("Parent:\n");
         show_proc_pids();
         close(pipefd[1]);
-        
-        while (read(pipefd[0], &buf, 1) > 0) {
-            write(STDOUT_FILENO, &buf, 1);
-        }
-                       
-        write(STDOUT_FILENO, "\n", 1);
+        read(pipefd[0], &buf, 20*sizeof(buf));
+        printf(&buf, getpid());
         close(pipefd[0]);
         wait(NULL);
         exit(EXIT_SUCCESS);
     }
-    
+
     /* unreachable code */
     return EXIT_SUCCESS;
 }