3eeb4c4bfd73d62519f672161db66df9463b8fc0
[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 rtf = fork();
22
23 if (rtf == -1) {
24 printf("Erreur de clonage\n");
25 exit(EXIT_FAILURE);
26 } else if (rtf == 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 rtw = wait(&status);
34 system("ps aux");
35 if (WIFEXITED(status)) {
36 printf("Fils termine, status=%d\n", WEXITSTATUS(status));
37 } else if (WIFSIGNALED(status)) {
38 printf("Fils tue par un signal %d\n", WTERMSIG(status));
39 } else if (WIFSTOPPED(status)) {
40 printf("Fils stoppe par un signal %d\n", WSTOPSIG(status));
41 } else if (WIFCONTINUED(status)) {
42 printf("Fils continue\n");
43 }
44 }
45
46 return EXIT_SUCCESS;
47 }