Respect the asked string format for stdout message passing between
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 30 May 2017 21:52:10 +0000 (23:52 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 30 May 2017 21:52:10 +0000 (23:52 +0200)
child and parent processes.

Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
TD1/part_two/exo.c

index 0194d04e47ec22387c9aaa95ce3c5a13079de811..bb6f3a5e06356a96bb4f316851183edaa404fa6b 100644 (file)
@@ -25,6 +25,7 @@ int main() {
         exit(EXIT_FAILURE);
     };
 
+    const char* str_fmt = "[%d] Coucou papa !\n";
     pid_t cpid = fork();
 
     if (cpid == -1) {
@@ -33,21 +34,16 @@ int main() {
     } else if (cpid == 0) {
         printf("Fils:\n");
         show_proc_pids();
-        const char* msg = "Coucou papa !";
         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, strlen(str_fmt));
+        printf(&buf, getpid());
         close(pipefd[0]);
         wait(NULL);
         exit(EXIT_SUCCESS);