Le code pour le tp T2

This commit is contained in:
stersky 2022-11-18 20:10:14 +01:00
parent ad29a645b7
commit 34e7403df1
4 changed files with 90 additions and 19 deletions

View file

@ -11,4 +11,8 @@ unsigned int acquisitionManagerInit(void);
*/ */
void acquisitionManagerJoin(void); void acquisitionManagerJoin(void);
//La taille du tampon
#define BUFFER_SIZE 13
#endif #endif

View file

@ -19,10 +19,21 @@ pthread_t producers[4];
static void *produce(void *params); static void *produce(void *params);
//Le tampon pour stocker les messages
MSG_BLOCK buffer[BUFFER_SIZE];
int index_buffer_libre = 0;
int nbr_threads = 0;
/** /**
* Semaphores and Mutex * Semaphores and Mutex
*/ */
//TODO sem_t *semaphore_libre;
sem_t *semaphore_occupe;
pthread_mutex_t mutex_tab_indices = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_count = PTHREAD_MUTEX_INITIALIZER;
/* /*
* Creates the synchronization elements. * Creates the synchronization elements.
@ -38,25 +49,54 @@ static void incrementProducedCount(void);
static unsigned int createSynchronizationObjects(void) static unsigned int createSynchronizationObjects(void)
{ {
//TODO //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"); printf("[acquisitionManager]Semaphore created\n");
return ERROR_SUCCESS; return ERROR_SUCCESS;
} }
static void incrementProducedCount(void) static void incrementProducedCount(void)
{ {
//TODO pthread_mutex_lock(&mutex_count);
produceCount++;
pthread_mutex_unlock(&mutex_count);
} }
unsigned int getProducedCount(void) unsigned int getProducedCount(void)
{ {
unsigned int p = 0; unsigned int p = 0;
//TODO pthread_mutex_lock(&mutex_count);
p = produceCount;
pthread_mutex_unlock(&mutex_count);
return p; return p;
} }
MSG_BLOCK getMessage(void){ MSG_BLOCK getMessage(void){
//TODO //TODO
MSG_BLOCK message;
static int index_lecture = 0;
sem_wait(semaphore_occupe);
message = buffer[index_lecture];
index_lecture = (index_lecture+1)%BUFFER_SIZE;
sem_post(semaphore_libre);
return message;
} }
//TODO create accessors to limit semaphore and mutex usage outside of this C module. //TODO create accessors to limit semaphore and mutex usage outside of this C module.
@ -73,7 +113,7 @@ unsigned int acquisitionManagerInit(void)
for (i = 0; i < PRODUCER_COUNT; i++) for (i = 0; i < PRODUCER_COUNT; i++)
{ {
//TODO pthread_create(&producers[i], NULL, produce, NULL);
} }
return ERROR_SUCCESS; return ERROR_SUCCESS;
@ -84,23 +124,50 @@ void acquisitionManagerJoin(void)
unsigned int i; unsigned int i;
for (i = 0; i < PRODUCER_COUNT; i++) for (i = 0; i < PRODUCER_COUNT; i++)
{ {
//TODO pthread_join(producers[i], NULL);
} }
//TODO sem_destroy(semaphore_libre);
sem_destroy(semaphore_occupe);
printf("[acquisitionManager]Semaphore cleaned\n"); printf("[acquisitionManager]Semaphore cleaned\n");
} }
void *produce(void* params) void *produce(void* params)
{ {
D(printf("[acquisitionManager]Producer created with id %d\n", gettid())); //D(printf("[acquisitionManager]Producer created with id %d\n", gettid()));
unsigned int i = 0; unsigned int i = 0;
int index_loc;
MSG_BLOCK message;
//Récupérer le numéro du thread
int num_thread;
pthread_mutex_lock(&mutex_count);
nbr_threads++;
num_thread = nbr_threads;
pthread_mutex_unlock(&mutex_count);
while (i < PRODUCER_LOOP_LIMIT) while (i < PRODUCER_LOOP_LIMIT)
{ {
i++; i++;
sleep(PRODUCER_SLEEP_TIME+(rand() % 5)); sleep(PRODUCER_SLEEP_TIME+(rand() % 5));
//TODO
//----- L'ajout d'un nouveau message -----
//On prend un jeton : une case se remplit
sem_wait(semaphore_libre);
pthread_mutex_lock(&mutex_tab_indices);
index_loc = index_buffer_libre;
index_buffer_libre = (index_buffer_libre+1)%BUFFER_SIZE;
pthread_mutex_unlock(&mutex_tab_indices);
//Récupérer l'entrée
getInput(num_thread, &message);
buffer[index_loc] = message;
//On met un jeton dans le sémaphore "occupé"
sem_post(semaphore_occupe);
} }
printf("[acquisitionManager] %d termination\n", gettid()); //printf("[acquisitionManager] %d termination\n", get_id());
//TODO
pthread_exit(NULL);
} }

View file

@ -28,12 +28,12 @@ void displayManagerJoin(void){
static void *display( void *parameters ) static void *display( void *parameters )
{ {
D(printf("[displayManager]Thread created for display with id %d\n", gettid())); //D(printf("[displayManager]Thread created for display with id %d\n", gettid()));
unsigned int diffCount = 0; unsigned int diffCount = 0;
while(diffCount < DISPLAY_LOOP_LIMIT){ while(diffCount < DISPLAY_LOOP_LIMIT){
sleep(DISPLAY_SLEEP_TIME); sleep(DISPLAY_SLEEP_TIME);
//TODO //TODO
} }
printf("[displayManager] %d termination\n", gettid()); //printf("[displayManager] %d termination\n", gettid());
//TODO //TODO
} }

View file

@ -52,14 +52,14 @@ void messageAdderJoin(void){
static void *sum( void *parameters ) static void *sum( void *parameters )
{ {
D(printf("[messageAdder]Thread created for sum with id %d\n", gettid())); //D(printf("[messageAdder]Thread created for sum with id %d\n", gettid()));
unsigned int i = 0; unsigned int i = 0;
while(i<ADDER_LOOP_LIMIT){ while(i<ADDER_LOOP_LIMIT){
i++; i++;
sleep(ADDER_SLEEP_TIME); sleep(ADDER_SLEEP_TIME);
//TODO //TODO
} }
printf("[messageAdder] %d termination\n", gettid()); //printf("[messageAdder] %d termination\n", gettid());
//TODO //TODO
} }