Give more explicit name to PID variables
[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
eb2b442a 21 pid_t cpid = fork();
9a6251d3 22
eb2b442a 23 if (cpid == -1) {
9a6251d3
JB
24 printf("Erreur de clonage\n");
25 exit(EXIT_FAILURE);
eb2b442a 26 } else if (cpid == 0) {
9a6251d3
JB
27 printf("Fils:\n");
28 show_proc_pids();
29 exit(EXIT_FAILURE);
30 } else {
31 printf("Parent:\n");
32 show_proc_pids();
eb2b442a
JB
33 pid_t wpid = wait(&status);
34 printf("Fin du PID: %d\n", wpid);
9a6251d3 35 system("ps aux");
f81c8f7c
JB
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 }
9a6251d3
JB
45 }
46
47 return EXIT_SUCCESS;
48}