From 33f71e89f68d8a3acfe5d5ac2182f037f034dedf Mon Sep 17 00:00:00 2001 From: Etienne Date: Tue, 22 Nov 2022 15:06:56 +0100 Subject: [PATCH 1/2] Test and Set --- .../exercice-1/acquisitionManagerTestAndSet.c | 85 +++++++++++++------ 1 file changed, 57 insertions(+), 28 deletions(-) diff --git a/T2/tp/exercice-1/acquisitionManagerTestAndSet.c b/T2/tp/exercice-1/acquisitionManagerTestAndSet.c index 17c40f9..7922ff6 100755 --- a/T2/tp/exercice-1/acquisitionManagerTestAndSet.c +++ b/T2/tp/exercice-1/acquisitionManagerTestAndSet.c @@ -10,12 +10,10 @@ #include "multitaskingAccumulator.h" #include "iAcquisitionManager.h" #include "debug.h" +#include - -//producer count storage -_Atomic int produceCount = 0; - -volatile unsigned int producedCount = 0; +#include +#define gettid() syscall(SYS_gettid) pthread_t producers[4]; @@ -34,9 +32,18 @@ sem_t semaphore_occupe; * Usefull variables */ +MSG_BLOCK Buffer[BUFFER_SIZE]; _Atomic int index_libre = 0; _Atomic int thread_count = 0; + +//producer count storage +_Atomic int verrou = 0; +volatile unsigned int produceCount = 0; + + + + /* * Creates the synchronization elements. * @return ERROR_SUCCESS if the init is ok, ERROR_INIT otherwise @@ -48,37 +55,59 @@ static unsigned int createSynchronizationObjects(void); */ static void incrementProducedCount(void); +// Méthode pour verrouiller un accès +static void pCountLockTake(){ + int expected = 0; + + //Tant que verrou ne vaut pas 0, on attend et expected vaut 0. Si verrou vaut bien 0, on le met à 1 + while(!atomic_compare_exchange_weak(&verrou, &expected, 1)) + expected = 0; + + //La fonction se termine.. +} + +// Méthode pour libérer un accès +static void pCountLockRelease(){ + verrou = 1; +} + static unsigned int createSynchronizationObjects(void) { - int error; - - //TODO DONE - if((error = sem_init(&semaphore_libre, 0, BUFFER_SIZE)) < 0){ - printf("[acquisitionManager]Semaphore L Error No. %d\n",error); - return error; - } - - if((error = sem_init(&semaphore_occupe, 0, 0)) < 0){ - printf("[acquisitionManager]Semaphore O Error No. %d\n",error); - return error; - } - - printf("[acquisitionManager]Semaphore created\n"); - return ERROR_SUCCESS; + //Initialisation des sémaphores + semaphore_libre = sem_open("/acquisitionManager_semLibre", O_CREAT, 0644, BUFFER_SIZE); + if (semaphore_libre == SEM_FAILED) + { + perror("[sem_open"); + return ERROR_INIT; + } + + semaphore_occupe = sem_open("/acquisitionManager_semLibre", O_CREAT, 0644, 0); + if (semaphore_occupe == SEM_FAILED) + { + perror("[sem_open"); + return ERROR_INIT; + } + //----Fin de l'initialisation sémaphore ----- + printf("[acquisitionManager]Semaphore created\n"); + return ERROR_SUCCESS; } static void incrementProducedCount(void) { - //TODO - produceCount++; + //On incrément le nombre de threads producteurs + pCountLockTake(); + produceCount++; + pCountLockRelease(); } unsigned int getProducedCount(void) { - unsigned int p = 0; - //TODO - p = produceCount; - return p; + unsigned int p = 0; + //On crée une section actomique + pCountLockTake(); + p = produceCount; + pCountLockRelease(); + return p; } @@ -159,7 +188,7 @@ void *produce(void* params) sem_post(&semaphore_occupe); } - printf("[acquisitionManager] %d termination\n", gettid()); + printf("[acquisitionManager] %d termination\n", syscall(gettid())); //TODO pthread_exit(NULL); -} \ No newline at end of file +} From ea7a71272886d037f449825eb863099d95551571 Mon Sep 17 00:00:00 2001 From: Etienne Date: Tue, 22 Nov 2022 15:13:53 +0100 Subject: [PATCH 2/2] Test and Set 2 --- .../exercice-1/acquisitionManagerTestAndSet.c | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/T2/tp/exercice-1/acquisitionManagerTestAndSet.c b/T2/tp/exercice-1/acquisitionManagerTestAndSet.c index 7922ff6..8d019eb 100755 --- a/T2/tp/exercice-1/acquisitionManagerTestAndSet.c +++ b/T2/tp/exercice-1/acquisitionManagerTestAndSet.c @@ -11,7 +11,6 @@ #include "iAcquisitionManager.h" #include "debug.h" #include - #include #define gettid() syscall(SYS_gettid) @@ -73,23 +72,21 @@ static void pCountLockRelease(){ static unsigned int createSynchronizationObjects(void) { - //Initialisation des sémaphores - semaphore_libre = sem_open("/acquisitionManager_semLibre", O_CREAT, 0644, BUFFER_SIZE); - if (semaphore_libre == SEM_FAILED) - { - perror("[sem_open"); - return ERROR_INIT; - } - - semaphore_occupe = sem_open("/acquisitionManager_semLibre", O_CREAT, 0644, 0); - if (semaphore_occupe == SEM_FAILED) - { - perror("[sem_open"); - return ERROR_INIT; - } - //----Fin de l'initialisation sémaphore ----- - printf("[acquisitionManager]Semaphore created\n"); - return ERROR_SUCCESS; + int error; + + //TODO DONE + if((error = sem_init(&semaphore_libre, 0, BUFFER_SIZE)) < 0){ + printf("[acquisitionManager]Semaphore L Error No. %d\n",error); + return error; + } + + if((error = sem_init(&semaphore_occupe, 0, 0)) < 0){ + printf("[acquisitionManager]Semaphore O Error No. %d\n",error); + return error; + } + + printf("[acquisitionManager]Semaphore created\n"); + return ERROR_SUCCESS; } static void incrementProducedCount(void)