Implement question 6
[TD_IML.git] / TD1 / part_one / exo.c
diff --git a/TD1/part_one/exo.c b/TD1/part_one/exo.c
new file mode 100644 (file)
index 0000000..3eeb4c4
--- /dev/null
@@ -0,0 +1,47 @@
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+void show_proc_pids() {
+    pid_t current_pid = getpid();
+    pid_t parent_pid = getppid();
+
+    printf("[%d] Mon PID: %d\n", current_pid, current_pid);
+    printf("[%d] PID du parent: %d\n", current_pid, parent_pid);
+}
+
+int main() {
+    int status = 0;
+    printf("Courant:\n");
+    show_proc_pids();
+
+    pid_t rtf = fork();
+
+    if (rtf == -1) {
+        printf("Erreur de clonage\n");
+        exit(EXIT_FAILURE);
+    } else if (rtf == 0) {
+        printf("Fils:\n");
+        show_proc_pids();
+        exit(EXIT_FAILURE);
+    } else {
+        printf("Parent:\n");
+        show_proc_pids();
+        pid_t rtw = wait(&status);
+        system("ps aux");
+        if (WIFEXITED(status)) {
+            printf("Fils termine, status=%d\n", WEXITSTATUS(status));
+        } else if (WIFSIGNALED(status)) {
+            printf("Fils tue par un signal %d\n", WTERMSIG(status));
+        } else if (WIFSTOPPED(status)) {
+            printf("Fils stoppe par un signal %d\n", WSTOPSIG(status));
+        } else if (WIFCONTINUED(status)) {
+            printf("Fils continue\n");
+        }
+    }
+    
+    return EXIT_SUCCESS;
+}