Code cleanups.
[TD_IML.git] / TD1 / part_one / exo.c
1 #include <sys/types.h>
2 #include <sys/wait.h>
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7
8 void show_proc_pids() {
9 pid_t current_pid = getpid();
10 pid_t parent_pid = getppid();
11
12 printf("[%d] Mon PID: %d\n", current_pid, current_pid);
13 printf("[%d] PID du parent: %d\n", current_pid, parent_pid);
14 }
15
16 int main() {
17 int status = 0;
18 printf("Courant:\n");
19 show_proc_pids();
20
21 pid_t cpid = fork();
22
23 if (cpid == -1) {
24 printf("Erreur de clonage\n");
25 exit(EXIT_FAILURE);
26 } else if (cpid == 0) {
27 printf("Fils:\n");
28 show_proc_pids();
29 exit(EXIT_FAILURE);
30 } else {
31 printf("Parent:\n");
32 show_proc_pids();
33 pid_t wpid = wait(&status);
34 printf("Fin du PID: %d\n", wpid);
35 system("ps aux");
36 if (WIFEXITED(status)) {
37 printf("Fils termine, status=%d\n", WEXITSTATUS(status));
38 } else if (WIFSIGNALED(status)) {
39 printf("Fils tue par un signal %d\n", WTERMSIG(status));
40 } else if (WIFSTOPPED(status)) {
41 printf("Fils stoppe par un signal %d\n", WSTOPSIG(status));
42 } else if (WIFCONTINUED(status)) {
43 printf("Fils continue\n");
44 }
45 }
46
47 return EXIT_SUCCESS;
48 }