M2_SETI/T2/tp/exercice-1/acquisitionManagerPOSIX.c

173 lines
3.5 KiB
C
Raw Permalink Normal View History

2022-11-18 15:07:43 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include "acquisitionManager.h"
#include "msg.h"
#include "iSensor.h"
#include "multitaskingAccumulator.h"
#include "iAcquisitionManager.h"
#include "debug.h"
//producer count storage
volatile unsigned int produceCount = 0;
pthread_t producers[4];
static void *produce(void *params);
2022-11-19 16:11:18 +01:00
MSG_BLOCK Buffer[BUFFER_SIZE];
2022-11-18 20:10:14 +01:00
2022-11-18 15:07:43 +01:00
/**
* Semaphores and Mutex
*/
2022-11-19 16:11:18 +01:00
//TODO
sem_t semaphore_libre;
sem_t semaphore_occupe;
2022-11-18 20:10:14 +01:00
2022-11-19 16:11:18 +01:00
pthread_mutex_t m_count = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t m_write = PTHREAD_MUTEX_INITIALIZER;
2022-11-18 15:07:43 +01:00
2022-11-19 16:11:18 +01:00
/**
* Usefull variables
*/
int index_libre = 0;
int thread_count = 0;
2022-11-18 15:07:43 +01:00
/*
* Creates the synchronization elements.
* @return ERROR_SUCCESS if the init is ok, ERROR_INIT otherwise
*/
static unsigned int createSynchronizationObjects(void);
/*
* Increments the produce count.
*/
static void incrementProducedCount(void);
static unsigned int createSynchronizationObjects(void)
{
2022-11-19 16:11:18 +01:00
int error;
2022-11-18 15:07:43 +01:00
2022-11-19 16:11:18 +01:00
//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;
}
2022-11-18 15:07:43 +01:00
printf("[acquisitionManager]Semaphore created\n");
return ERROR_SUCCESS;
}
static void incrementProducedCount(void)
{
2022-11-19 16:11:18 +01:00
//TODO DONE
pthread_mutex_lock(&m_count);
produceCount++;
pthread_mutex_unlock(&m_count);
2022-11-18 15:07:43 +01:00
}
unsigned int getProducedCount(void)
{
2022-11-19 16:11:18 +01:00
//TODO DONE
2022-11-18 15:07:43 +01:00
unsigned int p = 0;
2022-11-19 16:11:18 +01:00
pthread_mutex_lock(&m_count);
p = produceCount;
pthread_mutex_unlock(&m_count);
2022-11-18 15:07:43 +01:00
return p;
}
MSG_BLOCK getMessage(void){
//TODO
2022-11-19 16:11:18 +01:00
MSG_BLOCK res;
static int index_count = 0;
sem_wait(&semaphore_occupe);
res = Buffer[index_count];
index_count = (index_count+1)%BUFFER_SIZE;
sem_post(&semaphore_libre);
return res;
2022-11-18 15:07:43 +01:00
}
//TODO create accessors to limit semaphore and mutex usage outside of this C module.
unsigned int acquisitionManagerInit(void)
{
unsigned int i;
printf("[acquisitionManager]Synchronization initialization in progress...\n");
fflush( stdout );
if (createSynchronizationObjects() == ERROR_INIT)
return ERROR_INIT;
printf("[acquisitionManager]Synchronization initialization done.\n");
2022-11-19 16:11:18 +01:00
2022-11-18 15:07:43 +01:00
for (i = 0; i < PRODUCER_COUNT; i++)
{
2022-11-19 16:11:18 +01:00
//TODO DONE
pthread_create(&producers[i], NULL, produce, NULL);
2022-11-18 15:07:43 +01:00
}
return ERROR_SUCCESS;
}
void acquisitionManagerJoin(void)
{
unsigned int i;
for (i = 0; i < PRODUCER_COUNT; i++)
{
2022-11-19 16:11:18 +01:00
//TODO DONE
2022-11-18 20:10:14 +01:00
pthread_join(producers[i], NULL);
2022-11-18 15:07:43 +01:00
}
2022-11-19 16:11:18 +01:00
//TODO DONE
sem_destroy(&semaphore_libre);
sem_destroy(&semaphore_occupe);
2022-11-18 15:07:43 +01:00
printf("[acquisitionManager]Semaphore cleaned\n");
}
void *produce(void* params)
{
2022-11-22 14:39:55 +01:00
D(printf("[acquisitionManager]Producer created with id %d\n", gettid()));
2022-11-18 15:07:43 +01:00
unsigned int i = 0;
2022-11-19 16:11:18 +01:00
int index_lock;
int thread_num;
MSG_BLOCK message;
//Counting producers
pthread_mutex_lock(&m_count);
thread_num = thread_count;
thread_count++;
pthread_mutex_unlock(&m_count);
2022-11-18 15:07:43 +01:00
while (i < PRODUCER_LOOP_LIMIT)
{
i++;
sleep(PRODUCER_SLEEP_TIME+(rand() % 5));
2022-11-19 16:11:18 +01:00
//TODO DONE
sem_wait(&semaphore_libre);
pthread_mutex_lock(&m_write);
index_lock = index_libre;
index_libre = (index_libre + 1)%BUFFER_SIZE;
pthread_mutex_unlock(&m_write);
getInput(thread_num, &message);
Buffer[index_lock] = message;
2022-11-22 14:39:55 +01:00
incrementProducedCount();
2022-11-19 16:11:18 +01:00
sem_post(&semaphore_occupe);
2022-11-18 15:07:43 +01:00
}
2022-11-22 14:39:55 +01:00
printf("[acquisitionManager] %d termination\n", gettid());
2022-11-19 16:11:18 +01:00
//TODO DONE
pthread_exit(NULL);
}