Implement question 6
[TD_IML.git] / TD1 / part_one / exo.c
CommitLineData
9a6251d3
JB
1#include <sys/types.h>
2#include <sys/wait.h>
3
4#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7
8void 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
16int 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");
f81c8f7c
JB
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 }
9a6251d3
JB
44 }
45
46 return EXIT_SUCCESS;
47}