From 82cf22952dfd204b0b3ca7671ea7eafbc7a3dbb3 Mon Sep 17 00:00:00 2001 From: ngwalang Date: Thu, 12 Oct 2023 16:07:28 +0200 Subject: [PATCH] TP6 --- SCR3.1/TP6/ex1.c | 30 +++++++++++++++++++++ SCR3.1/TP6/max.c | 57 +++++++++++++++++++++++++++++++++++++++ SCR3.1/TP6/stack/Makefile | 10 +++++++ SCR3.1/TP6/stack/main.c | 21 +++++++++++++++ SCR3.1/TP6/stack/stack.c | 32 ++++++++++++++++++++++ SCR3.1/TP6/stack/stack.h | 12 +++++++++ 6 files changed, 162 insertions(+) create mode 100644 SCR3.1/TP6/ex1.c create mode 100644 SCR3.1/TP6/max.c create mode 100644 SCR3.1/TP6/stack/Makefile create mode 100644 SCR3.1/TP6/stack/main.c create mode 100644 SCR3.1/TP6/stack/stack.c create mode 100644 SCR3.1/TP6/stack/stack.h diff --git a/SCR3.1/TP6/ex1.c b/SCR3.1/TP6/ex1.c new file mode 100644 index 0000000..c8b7525 --- /dev/null +++ b/SCR3.1/TP6/ex1.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +#define NUM_THREADS 16 + +void *thread(void *thread_id) +{ + int id = *((int*) thread_id); + printf("Hello from thread %d\n", id); + + return NULL; +} + +int main(int argc, char *argv[]) +{ + int th[NUM_THREADS]; + pthread_t threads[NUM_THREADS]; + for (int i = 0; i < NUM_THREADS; i++){ + th[i] = i; + assert( pthread_create(&threads[i], NULL, thread, &th[i]) == 0); + } + + for (int i = 0; i < NUM_THREADS; i++) + assert( pthread_join(threads[i], NULL) == 0); + + return EXIT_SUCCESS; +} + diff --git a/SCR3.1/TP6/max.c b/SCR3.1/TP6/max.c new file mode 100644 index 0000000..0ff555c --- /dev/null +++ b/SCR3.1/TP6/max.c @@ -0,0 +1,57 @@ +#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*) (duels + i)); + } + for (int i = 0; i < 2;i++){ + pthread_join(threads[i], NULL); + } + + numbers finale = {0}; + 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", finale.max); + return EXIT_SUCCESS; +} diff --git a/SCR3.1/TP6/stack/Makefile b/SCR3.1/TP6/stack/Makefile new file mode 100644 index 0000000..28447ff --- /dev/null +++ b/SCR3.1/TP6/stack/Makefile @@ -0,0 +1,10 @@ +stack.o : stack.c + gcc -c stack.c + +main.o : main.c stack.h + gcc -c main.c + +main : main.o stack.o + gcc -o main main.o stack.o + +all : main diff --git a/SCR3.1/TP6/stack/main.c b/SCR3.1/TP6/stack/main.c new file mode 100644 index 0000000..514a427 --- /dev/null +++ b/SCR3.1/TP6/stack/main.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include "stack.h" + + + +int main(int argc, char *argv[]) +{ + + int val; + stack_t * s=stack_create(20); + + for(int i=0;i<20; i++) + stack_push(s,i); + + for(int i=0;i<20; i++) + stack_pop(s,&val); + + return 0; +} diff --git a/SCR3.1/TP6/stack/stack.c b/SCR3.1/TP6/stack/stack.c new file mode 100644 index 0000000..d1cc620 --- /dev/null +++ b/SCR3.1/TP6/stack/stack.c @@ -0,0 +1,32 @@ +#include "stack.h" +#include +#include + +struct stack_t { + /* + votre implantation + + */ +}; + +stack_t * stack_create( int max_size) +{ + +} + + +int stack_destroy(stack_t * s) +{ + +} + +int stack_push(stack_t *s,int val) +{ + +} + +int stack_pop(stack_t *s,int * val) +{ + +} + diff --git a/SCR3.1/TP6/stack/stack.h b/SCR3.1/TP6/stack/stack.h new file mode 100644 index 0000000..24768ec --- /dev/null +++ b/SCR3.1/TP6/stack/stack.h @@ -0,0 +1,12 @@ +#ifndef _STACK_INT_H +#define _STACK_INT_H + +typedef struct stack_t stack_t; + +stack_t * stack_create(int max_size); +int stack_destroy(stack_t *s); +int stack_push(stack_t *s, int val); +int stack_pop(stack_t *s,int *val); + + +#endif