From 8a80a14798e8a5041f9fdf04224d3d93564e1a5c Mon Sep 17 00:00:00 2001
From: "remi.greub" <remi.greub@hes-so.ch>
Date: Thu, 10 Apr 2025 21:49:04 +0200
Subject: [PATCH] =?UTF-8?q?affichage=20termin=C3=A9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 G17_bykes/main.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/G17_bykes/main.c b/G17_bykes/main.c
index 7660798..57fde56 100644
--- a/G17_bykes/main.c
+++ b/G17_bykes/main.c
@@ -19,6 +19,7 @@ pthread_mutex_t *mutex;
 pthread_barrier_t b;
 
 bool end = false;
+int personsFinished=0;
 
 struct habitant{
     int num_thread;
@@ -35,6 +36,7 @@ struct camionette{
     int i_site;
     int nbSites;
     int nbornes;
+    int nbHabs;
 };
 
 struct site{
@@ -59,6 +61,7 @@ int main(int argc, char **argv){
     int nbHabs = atoi(argv[2]);     //H 100
     int nbTrajets = atoi(argv[3]);  //M 10
     int nbBorne = atoi(argv[4]);    //N 10
+    nbSites += 1;
     srand(0);
 
     
@@ -72,14 +75,14 @@ int main(int argc, char **argv){
         sites[j].nb_bikesParked = nbBorne-2;
         sites[j].nb_P_waiting = 0;
     }
-    sites[0].nb_bikesParked = 0; //depot avec 0 velos au depart
+    sites[DEPOT].nb_bikesParked = 0; //depot avec 0 velos au depart
 
     camion->i_site = 0;
     camion->n_Velo = CAMION_CAPACITE_TYP;
     camion->nbornes = nbBorne;
     camion->num_thread = nbHabs;
     camion->nbSites = nbSites;
-
+    camion->nbHabs = nbHabs;
 
     sem_BikeFree = malloc(sizeof(sem_t)*nbSites);
     sem_BorneFree = malloc(sizeof(sem_t)*nbSites);
@@ -139,11 +142,13 @@ int main(int argc, char **argv){
     free(sem_BikeFree);
     free(sem_BorneFree);
     free(mutex);
+    printf("***************** CYCLING TERMINATED ***************\n");
     int sum=0;
-    for(int i=0; i<nbSites; i++){
+    for(int i=1; i<nbSites; i++){
+        printf("Terminal %d contains %d bikes\n", i, sites[i].nb_bikesParked);
         sum+=sites[i].nb_bikesParked;
     }
-    printf("nb de velos en fin de programme %d\n", sum);
+    printf("total number of bikes in town: %d, in depot: %d, in truck: %d, total: %d\n", sum, sites[DEPOT].nb_bikesParked, camion->n_Velo, camion->n_Velo+sites[DEPOT].nb_bikesParked+sum);
 
     free(habs);
     free(camion);
@@ -233,14 +238,13 @@ void *habitants(void *arg){
         hab->i_site = j;
         hab->nb_trajet += 1;
     }
-    //printf("machin bidule finished : %d, tot_trajets = %d\n", hab->num_thread, hab->nb_trajet);
-
+    printf("person %d stops\n", hab->num_thread);
+    pthread_mutex_lock(&mutex[hab->i_site]);
+    personsFinished += 1;
+    pthread_mutex_unlock(&mutex[hab->i_site]);
     pthread_barrier_wait(&b);
     //printf("machin bidule a passé la barriere !!!! : %d, tot_trajets = %d\n", hab->num_thread, hab->nb_trajet);
 
-    pthread_mutex_lock(&mutex[hab->i_site]);
-    end = true;
-    pthread_mutex_unlock(&mutex[hab->i_site]);
     return NULL;
 }
 
@@ -248,7 +252,7 @@ void *Camion(void *arg){
     struct camionette *camion = (struct camionette*)arg;
     int tmp_nbBikes;
     
-    while(!end){
+    while(personsFinished < camion->nbHabs){
         //1 : for each sites from 1 to S (0 is the depot)
         for(int i=1; i<camion->nbSites; i++){
             //spreads the bikes evenly
@@ -314,8 +318,9 @@ void *Camion(void *arg){
         }
         //3: pause betweem 100us up to 199us
         usleep(rand_Dc());
+        printf("TRUCK new loop, person's threads finished: %d\n", personsFinished);
     }
-    printf("end camion\n");
+    printf("TRUCK end loop\n");
     return NULL;
 }
 
-- 
GitLab