Skip to content
Snippets Groups Projects
Commit 2b0e85a8 authored by ismail.abloua's avatar ismail.abloua
Browse files

error fix: ALWAYS update semaphore increase when update bike (sem_places if...

error fix: ALWAYS update semaphore increase when update bike (sem_places if bike taken, sem_bikes if slot taken) with a sem_post
parent 0e5dddea
Branches
No related tags found
No related merge requests found
......@@ -25,9 +25,10 @@ Site sites[MAX_SITES];
int S, H, M, N;
int depot = 0;
int camion = 2;
//TODO tester avec et sans volatile
volatile int habitants_termines = 0;
pthread_mutex_t mutex_depot = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_camion = PTHREAD_MUTEX_INITIALIZER;
volatile int habitants_termines = 0;
pthread_mutex_t mutex_habitants_termines = PTHREAD_MUTEX_INITIALIZER;
// Habitant behavior
......@@ -39,7 +40,12 @@ void* thread_habitant(void* arg) {
// Get a bike
sem_wait(&sites[site].sem_velos);
pthread_mutex_lock(&sites[site].mutex);
// one less bike, one more free slot
sites[site].nb_velos--;
if (sem_post(&sites[site].sem_places) != 0) {
perror("sem_post");
exit(EXIT_FAILURE);
}
printf("(GET) person %d starts from terminal %d (%d bykes)\n", id, site, sites[site].nb_velos);
pthread_mutex_unlock(&sites[site].mutex);
......@@ -53,7 +59,14 @@ void* thread_habitant(void* arg) {
// Wait for a free slot at destination
sem_wait(&sites[dest].sem_places);
pthread_mutex_lock(&sites[dest].mutex);
printf("#debug, site %d, dest %d, depot %d, camion %d, available bike %d\n", site, dest, depot, camion, sites[dest].nb_velos);
// one less spot, one more bike
sites[dest].nb_velos++;
if (sem_post(&sites[site].sem_velos) != 0) {
perror("sem_post");
exit(EXIT_FAILURE);
}
printf("#debug, site %d, dest %d, depot %d, camion %d, available bike %d\n", site, dest, depot, camion, sites[dest].nb_velos);
printf("(PUT) person %d arrives at terminal %d (%d bykes)\n", id, dest, sites[dest].nb_velos);
pthread_mutex_unlock(&sites[dest].mutex);
......@@ -71,7 +84,7 @@ void* thread_habitant(void* arg) {
}
// Maintenance team behavior
void* thread_maintenance(void* arg) {
void* thread_maintenance() {
while (1) {
pthread_mutex_lock(&mutex_habitants_termines);
if (habitants_termines == H) {
......@@ -83,13 +96,14 @@ void* thread_maintenance(void* arg) {
for (int i = 0; i < S; i++) {
pthread_mutex_lock(&sites[i].mutex);
// Remove bikes if too many
while (sites[i].nb_velos > sites[i].nb_bornes - 2 && camion < MAX_CAMION) {
while (sites[i].nb_velos > (N - 2) && camion < MAX_CAMION) {
sites[i].nb_velos--;
camion++;
sem_post(&sites[i].sem_places);
printf("Truck removes a bike at terminal %d (bikes: %d)\n", i, sites[i].nb_velos);
}
// Add bikes if too few
// printf("sites[i].nb_velos = %d | camion = %d\n", sites[i].nb_velos, camion);
while (sites[i].nb_velos < 2 && camion > 0) {
sites[i].nb_velos++;
camion--;
......@@ -101,6 +115,7 @@ void* thread_maintenance(void* arg) {
// Manage depot to balance truck load
pthread_mutex_lock(&mutex_depot);
// printf("Depot: %d bikes | Truck: %d bikes\n", depot, camion);
if (camion > 2) {
depot += camion - 2;
camion = 2;
......@@ -135,11 +150,11 @@ int main(int argc, char* argv[]) {
return EXIT_FAILURE;
}
// random init
srand(time(NULL));
// Initialize sites
for (int i = 0; i < S; i++) {
sites[i].nb_bornes = N;
sites[i].nb_velos = N - 2;
pthread_mutex_init(&sites[i].mutex, NULL);
sem_init(&sites[i].sem_velos, 0, sites[i].nb_velos);
......@@ -147,12 +162,12 @@ int main(int argc, char* argv[]) {
}
pthread_t habitants[H], maintenance;
int ids[H];
// Start maintenance thread
pthread_create(&maintenance, NULL, thread_maintenance, NULL);
// Start habitants
int ids[H];
for (int i = 0; i < H; i++) {
ids[i] = i;
pthread_create(&habitants[i], NULL, thread_habitant, &ids[i]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment