SCR/SCR3.1/TP6/max.c

58 lines
1.3 KiB
C
Raw Normal View History

2023-10-12 16:07:28 +02:00
#include <unistd.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
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 <n1> <n2> <n3> <n4> <n5> <n6> <n7> <n8>\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;
2023-10-13 10:03:20 +02:00
pthread_create(&threads[i], NULL, max, (void*) (demi_f + i));
2023-10-12 16:07:28 +02:00
}
for (int i = 0; i < 2;i++){
pthread_join(threads[i], NULL);
}
2023-10-13 10:03:20 +02:00
numbers finale = {};
2023-10-12 16:07:28 +02:00
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);
2023-10-13 10:03:20 +02:00
printf("Le plus grand des huits nombres est : %d\n", finale.max);
2023-10-12 16:07:28 +02:00
return EXIT_SUCCESS;
}