diff --git a/TP6/Exo2/max.c b/TP6/Exo2/max.c new file mode 100644 index 0000000..246ca6c --- /dev/null +++ b/TP6/Exo2/max.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include + +struct numbers +{ + int n1; + int n2; + int max; +}; + +typedef struct numbers numbers; + +void *max(void *n) +{ + numbers *comp = (numbers *)n; + if (comp->n1 > comp->n2) + comp->max = comp->n1; + else + comp->max = comp->n2; +} + +int main(int argc, char **argv) +{ + if (argc != 9) + { + printf("Usage : %s \n", argv[0]); + return EXIT_FAILURE; + } + + pthread_t threads[4]; + numbers duels[4]; + + for (int i = 0; i < 4; i++) + { + duels[i].n1 = (int)strtod(argv[2 * i + 1], NULL); + duels[i].n2 = (int)strtod(argv[2 * (i + 1)], NULL); + duels[i].max = 0; + pthread_create(&threads[i], NULL, max, (void *)(duels + i)); + } + + for (int i = 0; i < 4; i++) + { + pthread_join(threads[i], NULL); + } + + numbers demi_f[2]; + + for (int i = 0; i < 2; i++) + { + demi_f[i].n1 = duels[i * 2].max; + demi_f[i].n2 = duels[i * 2 + 1].max; + demi_f[i].max = 0; + pthread_create(&threads[i], NULL, max, (void *)(demi_f + i)); + } + + for (int i = 0; i < 2; i++) + { + pthread_join(threads[i], NULL); + } + + numbers finale = {}; + finale.n1 = demi_f[0].max; + finale.n2 = demi_f[1].max; + + pthread_create(&threads[0], NULL, max, (void *)(&finale)); + pthread_join(threads[0], NULL); + + printf("Le plus grand des huits nombres est : %d\n", finale.max); + + return EXIT_SUCCESS; +} \ No newline at end of file