From 13c0155c7498f6ac21b12a9094a226bce015a682 Mon Sep 17 00:00:00 2001 From: Tom Moguljak Date: Thu, 28 Sep 2023 22:32:32 +0200 Subject: [PATCH] Ajout de l'exo 7 TP3 --- TP3/Exo7/exo7.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 TP3/Exo7/exo7.c diff --git a/TP3/Exo7/exo7.c b/TP3/Exo7/exo7.c new file mode 100644 index 0000000..9ce2339 --- /dev/null +++ b/TP3/Exo7/exo7.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void fils(void) +{ + int n; + srand(getpid()); + exit(rand() % 128); +} + +int main(int argc, char *argv[]) +{ + int n, max, status; + assert(argc == 2); + n = strtol(argv[1], NULL, 0); + + for (int i = 0; i <= n; i++) + { + pid_t p = fork(); + assert(p != -1); + + if (p == 0) + { + fils(); + } + } + + wait(&status); + + if (WIFEXITED(status)) + max = WEXITSTATUS(status); + + for (int i = 0; i <= n; i++) + { + wait(&status); + if (WIFEXITED(status)) + { + if (WEXITSTATUS(status) > max) + max = WEXITSTATUS(status); + } + } + + printf("Le maximum est %d\n", max); + exit(0); +} \ No newline at end of file