t2 tp test
This commit is contained in:
parent
7a6f332f72
commit
8bcf9bc887
11 changed files with 343 additions and 10 deletions
168
T2/tp/exercice-1/acquisitionManagerAtomic.c
Executable file
168
T2/tp/exercice-1/acquisitionManagerAtomic.c
Executable file
|
@ -0,0 +1,168 @@
|
||||||
|
#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"
|
||||||
|
#include "stdatomic.h"
|
||||||
|
|
||||||
|
|
||||||
|
//producer count storage
|
||||||
|
_Atomic int produceCount = 0;
|
||||||
|
|
||||||
|
pthread_t producers[4];
|
||||||
|
|
||||||
|
static void *produce(void *params);
|
||||||
|
|
||||||
|
MSG_BLOCK Buffer[BUFFER_SIZE];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Semaphores and Mutex
|
||||||
|
*/
|
||||||
|
//TODO
|
||||||
|
sem_t semaphore_libre;
|
||||||
|
sem_t semaphore_occupe;
|
||||||
|
|
||||||
|
// pthread_mutex_t m_write = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Usefull variables
|
||||||
|
*/
|
||||||
|
|
||||||
|
_Atomic int index_libre = 0;
|
||||||
|
_Atomic int thread_count = 0;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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)
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
//TODO
|
||||||
|
produceCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int getProducedCount(void)
|
||||||
|
{
|
||||||
|
unsigned int p = 0;
|
||||||
|
//TODO
|
||||||
|
p = produceCount;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
MSG_BLOCK getMessage(void){
|
||||||
|
//TODO
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
//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");
|
||||||
|
|
||||||
|
for (i = 0; i < PRODUCER_COUNT; i++)
|
||||||
|
{
|
||||||
|
//TODO
|
||||||
|
pthread_create(&producers[i], NULL, produce, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ERROR_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void acquisitionManagerJoin(void)
|
||||||
|
{
|
||||||
|
unsigned int 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
void *produce(void* params)
|
||||||
|
{
|
||||||
|
D(printf("[acquisitionManager]Producer created with id %d\n", gettid()));
|
||||||
|
unsigned int i = 0;
|
||||||
|
int index_lock;
|
||||||
|
int thread_num;
|
||||||
|
MSG_BLOCK message;
|
||||||
|
|
||||||
|
//Counting producers
|
||||||
|
thread_num = thread_count;
|
||||||
|
thread_count++;
|
||||||
|
|
||||||
|
while (i < PRODUCER_LOOP_LIMIT)
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
sleep(PRODUCER_SLEEP_TIME+(rand() % 5));
|
||||||
|
//TODO
|
||||||
|
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;
|
||||||
|
incrementProducedCount();
|
||||||
|
|
||||||
|
sem_post(&semaphore_occupe);
|
||||||
|
}
|
||||||
|
printf("[acquisitionManager] %d termination\n", gettid());
|
||||||
|
//TODO
|
||||||
|
pthread_exit(NULL);
|
||||||
|
|
||||||
|
}
|
BIN
T2/tp/exercice-1/acquisitionManagerAtomic.o
Normal file
BIN
T2/tp/exercice-1/acquisitionManagerAtomic.o
Normal file
Binary file not shown.
|
@ -93,7 +93,6 @@ MSG_BLOCK getMessage(void){
|
||||||
sem_wait(&semaphore_occupe);
|
sem_wait(&semaphore_occupe);
|
||||||
res = Buffer[index_count];
|
res = Buffer[index_count];
|
||||||
index_count = (index_count+1)%BUFFER_SIZE;
|
index_count = (index_count+1)%BUFFER_SIZE;
|
||||||
incrementProducedCount();
|
|
||||||
sem_post(&semaphore_libre);
|
sem_post(&semaphore_libre);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
|
@ -138,7 +137,7 @@ void acquisitionManagerJoin(void)
|
||||||
|
|
||||||
void *produce(void* params)
|
void *produce(void* params)
|
||||||
{
|
{
|
||||||
D(printf("[acquisitionManager]Producer created with id %d\n", getpid()));
|
D(printf("[acquisitionManager]Producer created with id %d\n", gettid()));
|
||||||
unsigned int i = 0;
|
unsigned int i = 0;
|
||||||
int index_lock;
|
int index_lock;
|
||||||
int thread_num;
|
int thread_num;
|
||||||
|
@ -164,10 +163,11 @@ void *produce(void* params)
|
||||||
|
|
||||||
getInput(thread_num, &message);
|
getInput(thread_num, &message);
|
||||||
Buffer[index_lock] = message;
|
Buffer[index_lock] = message;
|
||||||
|
incrementProducedCount();
|
||||||
|
|
||||||
sem_post(&semaphore_occupe);
|
sem_post(&semaphore_occupe);
|
||||||
}
|
}
|
||||||
printf("[acquisitionManager] %d termination\n", getpid());
|
printf("[acquisitionManager] %d termination\n", gettid());
|
||||||
//TODO DONE
|
//TODO DONE
|
||||||
pthread_exit(NULL);
|
pthread_exit(NULL);
|
||||||
}
|
}
|
Binary file not shown.
165
T2/tp/exercice-1/acquisitionManagerTestAndSet.c
Executable file
165
T2/tp/exercice-1/acquisitionManagerTestAndSet.c
Executable file
|
@ -0,0 +1,165 @@
|
||||||
|
#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
|
||||||
|
_Atomic int produceCount = 0;
|
||||||
|
|
||||||
|
volatile unsigned int producedCount = 0;
|
||||||
|
|
||||||
|
pthread_t producers[4];
|
||||||
|
|
||||||
|
static void *produce(void *params);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Semaphores and Mutex
|
||||||
|
*/
|
||||||
|
//TODO
|
||||||
|
sem_t semaphore_libre;
|
||||||
|
sem_t semaphore_occupe;
|
||||||
|
|
||||||
|
// pthread_mutex_t m_write = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Usefull variables
|
||||||
|
*/
|
||||||
|
|
||||||
|
_Atomic int index_libre = 0;
|
||||||
|
_Atomic int thread_count = 0;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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)
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
//TODO
|
||||||
|
produceCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int getProducedCount(void)
|
||||||
|
{
|
||||||
|
unsigned int p = 0;
|
||||||
|
//TODO
|
||||||
|
p = produceCount;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
MSG_BLOCK getMessage(void){
|
||||||
|
//TODO
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
//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");
|
||||||
|
|
||||||
|
for (i = 0; i < PRODUCER_COUNT; i++)
|
||||||
|
{
|
||||||
|
//TODO
|
||||||
|
pthread_create(&producers[i], NULL, produce, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ERROR_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void acquisitionManagerJoin(void)
|
||||||
|
{
|
||||||
|
unsigned int 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
void *produce(void* params)
|
||||||
|
{
|
||||||
|
D(printf("[acquisitionManager]Producer created with id %d\n", gettid()));
|
||||||
|
unsigned int i = 0;
|
||||||
|
int index_lock;
|
||||||
|
int thread_num;
|
||||||
|
MSG_BLOCK message;
|
||||||
|
|
||||||
|
//Counting producers
|
||||||
|
thread_num = thread_count;
|
||||||
|
thread_count++;
|
||||||
|
|
||||||
|
while (i < PRODUCER_LOOP_LIMIT)
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
sleep(PRODUCER_SLEEP_TIME+(rand() % 5));
|
||||||
|
//TODO
|
||||||
|
sem_wait(&semaphore_libre);
|
||||||
|
|
||||||
|
index_lock = index_libre;
|
||||||
|
index_libre = (index_libre + 1)%BUFFER_SIZE;
|
||||||
|
|
||||||
|
getInput(thread_num, &message);
|
||||||
|
Buffer[index_lock] = message;
|
||||||
|
incrementProducedCount();
|
||||||
|
|
||||||
|
sem_post(&semaphore_occupe);
|
||||||
|
}
|
||||||
|
printf("[acquisitionManager] %d termination\n", gettid());
|
||||||
|
//TODO
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
|
@ -30,7 +30,7 @@ void displayManagerJoin(void){
|
||||||
|
|
||||||
static void *display( void *parameters )
|
static void *display( void *parameters )
|
||||||
{
|
{
|
||||||
D(printf("[displayManager]Thread created for display with id %d\n", getpid()));
|
D(printf("[displayManager]Thread created for display with id %d\n", gettid()));
|
||||||
unsigned int diffCount = 0;
|
unsigned int diffCount = 0;
|
||||||
MSG_BLOCK mBlock;
|
MSG_BLOCK mBlock;
|
||||||
while(diffCount < DISPLAY_LOOP_LIMIT){
|
while(diffCount < DISPLAY_LOOP_LIMIT){
|
||||||
|
@ -41,7 +41,7 @@ static void *display( void *parameters )
|
||||||
messageDisplay(&mBlock);
|
messageDisplay(&mBlock);
|
||||||
print(getProducedCount(),getConsumedCount());
|
print(getProducedCount(),getConsumedCount());
|
||||||
}
|
}
|
||||||
printf("[displayManager] %d termination\n", getpid());
|
printf("[displayManager] %d termination\n", gettid());
|
||||||
//TODO DONE
|
//TODO DONE
|
||||||
pthread_exit(NULL);
|
pthread_exit(NULL);
|
||||||
}
|
}
|
||||||
|
|
Binary file not shown.
|
@ -34,7 +34,7 @@ MSG_BLOCK getCurrentSum(){
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int getConsumedCount(){
|
unsigned int getConsumedCount(){
|
||||||
//TODO
|
//TODO DONE
|
||||||
return consumeCount;
|
return consumeCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,13 +62,13 @@ static void incrementConsumeCount(void){
|
||||||
|
|
||||||
static void *sum( void *parameters )
|
static void *sum( void *parameters )
|
||||||
{
|
{
|
||||||
D(printf("[messageAdder]Thread created for sum with id %d\n", getpid()));
|
D(printf("[messageAdder]Thread created for sum with id %d\n", gettid()));
|
||||||
unsigned int i = 0;
|
unsigned int i = 0;
|
||||||
MSG_BLOCK message;
|
MSG_BLOCK message;
|
||||||
while(i<ADDER_LOOP_LIMIT){
|
while(i<ADDER_LOOP_LIMIT){
|
||||||
i++;
|
i++;
|
||||||
sleep(ADDER_SLEEP_TIME);
|
sleep(ADDER_SLEEP_TIME);
|
||||||
//TODO
|
//TODO DONE
|
||||||
message = getMessage();
|
message = getMessage();
|
||||||
if(messageCheck(&message)){
|
if(messageCheck(&message)){
|
||||||
messageAdd(&out,&message);
|
messageAdd(&out,&message);
|
||||||
|
@ -76,8 +76,8 @@ static void *sum( void *parameters )
|
||||||
}
|
}
|
||||||
// getCurrentSum();
|
// getCurrentSum();
|
||||||
}
|
}
|
||||||
printf("[messageAdder] %d termination\n", getpid());
|
printf("[messageAdder] %d termination\n", gettid());
|
||||||
//TODO
|
//TODO DONE
|
||||||
pthread_exit(NULL);
|
pthread_exit(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Binary file not shown.
BIN
T2/tp/exercice-1/multitaskingAccumulatorAtomic
Executable file
BIN
T2/tp/exercice-1/multitaskingAccumulatorAtomic
Executable file
Binary file not shown.
Binary file not shown.
Loading…
Reference in a new issue