X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TD1%2Fpart_one%2Fexo.c;fp=TD1%2Fpart_one%2Fexo.c;h=3eeb4c4bfd73d62519f672161db66df9463b8fc0;hb=f81c8f7c7117d38a0d47535e07c30744cec867f4;hp=0000000000000000000000000000000000000000;hpb=9a6251d3c0f454f46a1444e4bab375ab2210b986;p=TD_IML.git diff --git a/TD1/part_one/exo.c b/TD1/part_one/exo.c new file mode 100644 index 0000000..3eeb4c4 --- /dev/null +++ b/TD1/part_one/exo.c @@ -0,0 +1,47 @@ +#include +#include + +#include +#include +#include + +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; +}