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