diff --git a/Bykes_TP/civilian.h b/Bykes_TP/civilian.h new file mode 100644 index 0000000000000000000000000000000000000000..33c2a2342c56c0d512e188815ca5a0be657e0cbc --- /dev/null +++ b/Bykes_TP/civilian.h @@ -0,0 +1,24 @@ +#ifndef _CIVILIAN_H_ +#define _CIVILIAN_H_ + +#include "city.h" + +#define USLEEP 1 + +struct person{ + int num_thread; + int nb_travels; + int max_travels; + int i_terminals; + int nbTerminals; + int nslots; +}; + +void init_civilians(struct person *civil, struct city *city, int nbTrajets); +int rand_D(); +void *Persons(void *arg); + +//unused +void free_Person(struct person **pers); + +#endif \ No newline at end of file diff --git a/G17_bykes/.vscode/settings.json b/G17_bykes/.vscode/settings.json deleted file mode 100644 index 5a51a28efb30dd3f77571eb8dcfac4718a8b4d89..0000000000000000000000000000000000000000 --- a/G17_bykes/.vscode/settings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "files.associations": { - "threads.h": "c" - } -} \ No newline at end of file diff --git a/G17_bykes/Makefile b/G17_bykes/Makefile index a12c3ef709e7e6ae77a4b17a9350827397a2acdc..511a4d2076fe77d8ca0d972008e7d3db8e4996fd 100644 --- a/G17_bykes/Makefile +++ b/G17_bykes/Makefile @@ -4,9 +4,15 @@ LDFLAGS:=-lpthread -lm EXEC:=bykes ARGS:=10 100 10 10 -$(EXEC): main.c +$(EXEC): main.c city.o civilian.o Truck.o $(CC) $(CFLAGS) $^ -o $(EXEC) $(LDFLAGS) +city.o: city.c city.h + +Truck.o: Truck.c Truck.h + +civilian.o: civilian.c civilian.h + clean: rm -rf *.o $(EXEC) diff --git a/G17_bykes/Truck.c b/G17_bykes/Truck.c new file mode 100644 index 0000000000000000000000000000000000000000..cff6f13b76d12514829b128c83035ecc80faaf37 --- /dev/null +++ b/G17_bykes/Truck.c @@ -0,0 +1,101 @@ +#include "Truck.h" + +void init_truck(struct Truck *truck, struct city *city){ + //truck = malloc(sizeof(struct Truck)); + truck->city = city; + truck->i_terminals = 0; + truck->n_bike = TRUCK_CAPACITE_TYP; + truck->nslots = city->nSlots; + truck->num_thread = city->npersons; + truck->nbterminals = city->nTerminal; + truck->nbpersons = city->npersons; +} + +int rand_Dc(){ + return (rand()%99)+100; +} + +void *trucks(void *arg){ + struct Truck *truck = (struct Truck*)arg; + int tmp_nbBikes; + + while(truck->city->personsFinished < truck->nbpersons){ + //1 : for each terminals from 1 to S (0 is the depot) + for(int i=1; i<truck->nbterminals; i++){ + //spreads the bikes evenly + //too many bikes in this terminals + tmp_nbBikes = truck->n_bike; + if(truck->city->terminals[i].nb_bikesParked > (truck->nslots-2) && truck->n_bike < TRUCK_CAPACITE_MAX){ + while(truck->city->terminals[i].nb_bikesParked > (truck->nslots-2) && truck->n_bike < TRUCK_CAPACITE_MAX){ + sem_wait(truck->city->terminals[i].sem_BikeFree); + pthread_mutex_lock(truck->city->terminals[i].mutex); + truck->n_bike += 1; + truck->city->terminals[i].nb_bikesParked -= 1; + pthread_mutex_unlock(truck->city->terminals[i].mutex); + sem_post(truck->city->terminals[i].sem_FreeSlot); + } + + pthread_mutex_lock(truck->city->terminals[i].mutex); + printf("truck removes %d bikes at terminal %d (%d bikes, %d persons waiting, %d left in truck)\n", truck->n_bike-tmp_nbBikes, i, truck->city->terminals[i].nb_bikesParked, truck->city->terminals[i].nb_P_waiting, truck->n_bike); + pthread_mutex_unlock(truck->city->terminals[i].mutex); + } + // not enough bikes in this terminals + // until he gets at least 2 bikes (or the truck is empty) + else if(truck->city->terminals[i].nb_bikesParked < CITY_CAPACITY_MIN && truck->n_bike > 0){ + tmp_nbBikes = truck->n_bike; + while(truck->city->terminals[i].nb_bikesParked < CITY_CAPACITY_MIN && truck->n_bike > 0){ + sem_wait(truck->city->terminals[i].sem_FreeSlot); + pthread_mutex_lock(truck->city->terminals[i].mutex); + truck->n_bike -= 1; + truck->city->terminals[i].nb_bikesParked += 1; + pthread_mutex_unlock(truck->city->terminals[i].mutex); + sem_post(truck->city->terminals[i].sem_BikeFree); + } + + pthread_mutex_lock(truck->city->terminals[i].mutex); + printf("truck adds %d bikes at terminal %d (%d bikes, %d persons waiting, %d left in truck)\n", tmp_nbBikes-truck->n_bike, i, truck->city->terminals[i].nb_bikesParked, truck->city->terminals[i].nb_P_waiting, truck->n_bike); + pthread_mutex_unlock(truck->city->terminals[i].mutex); + } + } + //2: goes to the depot to rebalance the truck's content to 2 bycycles + tmp_nbBikes = truck->n_bike; + if(truck->n_bike < TRUCK_CAPACITE_TYP){ + if(truck->city->terminals[DEPOT].nb_bikesParked > 0){ + while(truck->n_bike < TRUCK_CAPACITE_TYP && truck->city->terminals[DEPOT].nb_bikesParked > 0){ + truck->n_bike += 1; + pthread_mutex_lock(truck->city->terminals[DEPOT].mutex); + truck->city->terminals[DEPOT].nb_bikesParked -= 1; + pthread_mutex_unlock(truck->city->terminals[DEPOT].mutex); + } + pthread_mutex_lock(truck->city->terminals[DEPOT].mutex); + printf("(DEPOT) truck gets %d bikes at terminal %d (%d bikes, %d persons waiting, %d left in truck)\n", truck->n_bike-tmp_nbBikes, DEPOT, truck->city->terminals[DEPOT].nb_bikesParked, truck->city->terminals[DEPOT].nb_P_waiting, truck->n_bike); + pthread_mutex_unlock(truck->city->terminals[DEPOT].mutex); + } + } + else if (truck->n_bike > TRUCK_CAPACITE_TYP){ + while(truck->n_bike > TRUCK_CAPACITE_TYP){ + truck->n_bike -= 1; + pthread_mutex_lock(truck->city->terminals[DEPOT].mutex); + truck->city->terminals[DEPOT].nb_bikesParked += 1; + pthread_mutex_unlock(truck->city->terminals[DEPOT].mutex); + } + pthread_mutex_lock(truck->city->terminals[DEPOT].mutex); + printf("(DEPOT) truck deposit %d bikes at terminal %d (%d bikes, %d persons waiting, %d left in truck)\n", tmp_nbBikes-truck->n_bike, DEPOT, truck->city->terminals[DEPOT].nb_bikesParked, truck->city->terminals[DEPOT].nb_P_waiting, truck->n_bike); + pthread_mutex_unlock(truck->city->terminals[DEPOT].mutex); + } + //3: pause betweem 100us up to 199us +#if USLEEP + usleep(rand_Dc()); +#endif + //printf("TRUCK new loop, person's threads finished: %d\n", personsFinished); + } + printf("TRUCK end loop\n"); + return NULL; +} + +//unused +/*void free_truck(struct Truck **truck){ + (*truck)->city = NULL; + free(*truck); + truck = NULL; +}*/ \ No newline at end of file diff --git a/G17_bykes/Truck.h b/G17_bykes/Truck.h new file mode 100644 index 0000000000000000000000000000000000000000..97bf70735b19307302e0bf769279ec78319ca755 --- /dev/null +++ b/G17_bykes/Truck.h @@ -0,0 +1,26 @@ +#ifndef _TRUCK_H_ +#define _TRUCK_H_ + +#include "city.h" + +#define TRUCK_CAPACITE_MAX 4 +#define TRUCK_CAPACITE_TYP 2 + +struct Truck{ + int num_thread; + int n_bike; + int i_terminals; + int nbterminals; + int nslots; + int nbpersons; + struct city *city; +}; + +void init_truck(struct Truck *truck, struct city *city); +int rand_Dc(); +void *trucks(void *arg); + +//unused +//void free_truck(struct Truck **truck); + +#endif \ No newline at end of file diff --git a/G17_bykes/bykes b/G17_bykes/bykes index 1fafd7cc7285dadb5a8a7f53b320f9d91b29224a..f4ccc94a889b389e17bc24927c01c6960adf02cf 100755 Binary files a/G17_bykes/bykes and b/G17_bykes/bykes differ diff --git a/G17_bykes/city.c b/G17_bykes/city.c new file mode 100644 index 0000000000000000000000000000000000000000..11253886f466e479bd035f0ed6adf1d1ee8ee37b --- /dev/null +++ b/G17_bykes/city.c @@ -0,0 +1,45 @@ +#include "city.h" + +void init_city(struct city* city, int nbTerminals, int npersons, int nbSlots){ + //city = malloc(sizeof(struct city)); + city->nTerminal = nbTerminals; + city->npersons = npersons; + city->nSlots = nbSlots; + city->personsFinished = 0; + + city->terminals = calloc(city->npersons, sizeof(struct city)); + + for(int j = 0; j<city->npersons; j++){ + city->terminals[j].nb_bikesParked = nbSlots-2; + city->terminals[j].nb_P_waiting = 0; + city->terminals[j].sem_BikeFree = malloc(sizeof(sem_t)); + city->terminals[j].sem_FreeSlot = malloc(sizeof(sem_t)); + city->terminals[j].mutex = malloc(sizeof(pthread_mutex_t)); + + sem_init(city->terminals[j].sem_BikeFree, 0, nbSlots-2); + sem_init(city->terminals[j].sem_FreeSlot, 0, nbSlots-(nbSlots-2)); + pthread_mutex_init(city->terminals[j].mutex, NULL); + } + city->terminals[DEPOT].nb_bikesParked = 0; + pthread_barrier_init(&city->b, NULL, city->npersons); +} + +//unused +void free_city(struct city *city){ + //free the memory + for(int i=0; i<city->nTerminal; i++){ + sem_destroy(city->terminals[i].sem_BikeFree); + sem_destroy(city->terminals[i].sem_FreeSlot); + pthread_mutex_destroy(city->terminals[i].mutex); + } + pthread_barrier_destroy(&city->b); + for(int i=0; i<city->nTerminal; i++){ + free(city->terminals[i].sem_BikeFree); + free(city->terminals[i].sem_FreeSlot); + free(city->terminals[i].mutex); + } + for(int i= 0; i<city->nTerminal; i++){ + free(&city->terminals[i]); + } + free(city); +} \ No newline at end of file diff --git a/G17_bykes/city.h b/G17_bykes/city.h new file mode 100644 index 0000000000000000000000000000000000000000..dc9b8a678989f596a83c764097f6bde5922f8d6f --- /dev/null +++ b/G17_bykes/city.h @@ -0,0 +1,40 @@ +#ifndef _CITY_H_ +#define _CITY_H_ + +#include <stdio.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdlib.h> +#include <unistd.h> +#include <pthread.h> +#include <time.h> +#include <semaphore.h> + + +#define CITY_CAPACITY_MIN 2 +#define DEPOT 0 +#define USLEEP 1 + +struct terminal{ + int nb_bikesParked; + int nb_P_waiting; + sem_t *sem_FreeSlot; + sem_t *sem_BikeFree; + pthread_mutex_t *mutex; +}; + +struct city{ + int npersons; + int nSlots; + int nTerminal; + int personsFinished; + pthread_barrier_t b; + struct terminal *terminals; +}; + +void init_city(struct city* city, int nbTerminals, int npersons, int nbSlots); + +//unused +void free_city(struct city *city); + +#endif \ No newline at end of file diff --git a/G17_bykes/civilian.c b/G17_bykes/civilian.c new file mode 100644 index 0000000000000000000000000000000000000000..0f1a6b60a4ce35bf1b023f12743b56f88ae14aca --- /dev/null +++ b/G17_bykes/civilian.c @@ -0,0 +1,117 @@ +#include "civilian.h" +struct city *city_; + +void init_civilians(struct person *civil, struct city *city, int nbTrajets){ + //civil = calloc(city->npersons, sizeof(struct person)); + for(int i=0; i<city->npersons; i++){ + civil[i].max_travels = nbTrajets; + civil[i].nslots = city->nSlots; + civil[i].nbTerminals = city->nTerminal; + civil[i].num_thread = i; + civil[i].nb_travels = 0; + civil[i].i_terminals = (rand()%(civil[i].nbTerminals - 1))+1; + } + city_ = city; +} + +int rand_D(){ + return (rand()%999)+1000; +} + +void *Persons(void *arg){ + struct person *pers = (struct person*)arg; + int j=0; + int val; + // inside a Mtravels loop for each persons + while(pers->nb_travels < pers->max_travels){ + //waits for a bike in terminals i + //if(pers->i_terminals==4){ + sem_getvalue(city_->terminals[pers->i_terminals].sem_FreeSlot,&val); + pthread_mutex_lock(city_->terminals[pers->i_terminals].mutex); + printf("(GET) person %d starts from terminal %d (%d bykes, %d persons waiting, semslotsFree_val : %d)\n", pers->num_thread, pers->i_terminals, city_->terminals[pers->i_terminals].nb_bikesParked, city_->terminals[pers->i_terminals].nb_P_waiting, val);//terminals[pers->i_terminals].nb_P_waiting); + pthread_mutex_unlock(city_->terminals[pers->i_terminals].mutex); + //} + + if(city_->terminals[pers->i_terminals].nb_bikesParked<=0){// && pers->i_terminals == 4){ + printf("person %d is STUCK at terminal %d: rack empty : %d bikes\n", pers->num_thread, pers->i_terminals, city_->terminals[pers->i_terminals].nb_bikesParked); + } + + pthread_mutex_lock(city_->terminals[pers->i_terminals].mutex); + city_->terminals[pers->i_terminals].nb_P_waiting += 1; + pthread_mutex_unlock(city_->terminals[pers->i_terminals].mutex); + + + sem_wait(city_->terminals[pers->i_terminals].sem_BikeFree); + pthread_mutex_lock(city_->terminals[pers->i_terminals].mutex); + city_->terminals[pers->i_terminals].nb_P_waiting -= 1; + city_->terminals[pers->i_terminals].nb_bikesParked -= 1; //critical section + //if(pers->i_terminals==4){ + //printf("person %d leaves terminal %d by bike\n", pers->num_thread, pers->i_terminals); + //} + pthread_mutex_unlock(city_->terminals[pers->i_terminals].mutex); + sem_post(city_->terminals[pers->i_terminals].sem_FreeSlot); + //if(pers->i_terminals==4){ + printf("person %d leaves terminal %d by bike\n", pers->num_thread, pers->i_terminals); + //} + + + //go to place j = rand()%nterminals and j != i + do{ + j = (rand()%(pers->nbTerminals - 1))+1; + }while(j==pers->i_terminals); +#if USLEEP + usleep(rand_D()); +#endif + + //waits for a parkinglot to be freed and deposit his bycycle + //if(j==4){ + sem_getvalue(city_->terminals[j].sem_BikeFree,&val); + pthread_mutex_lock(city_->terminals[j].mutex); + printf("(PUT) person %d arrives at terminal %d (%d bykes, %d persons waiting, semBikeFree_val : %d)\n", pers->num_thread, j, city_->terminals[j].nb_bikesParked, city_->terminals[j].nb_P_waiting, val);//terminals[j].nb_P_waiting); + pthread_mutex_unlock(city_->terminals[j].mutex); + //} + + + if(city_->terminals[j].nb_bikesParked>=pers->nslots){//} && j == 4){ + printf("person %d is STUCK at terminal %d: rack full : %d bikes\n", pers->num_thread, j, city_->terminals[j].nb_bikesParked); + } + + pthread_mutex_lock(city_->terminals[j].mutex); + city_->terminals[j].nb_P_waiting += 1; + pthread_mutex_unlock(city_->terminals[j].mutex); + + + sem_wait(city_->terminals[j].sem_FreeSlot); + pthread_mutex_lock(city_->terminals[j].mutex); + //deposit his bycycle + city_->terminals[j].nb_P_waiting -= 1; + city_->terminals[j].nb_bikesParked += 1; //critical section + pthread_mutex_unlock(city_->terminals[j].mutex); + sem_post(city_->terminals[j].sem_BikeFree); + //if(j==4){ + printf("person %d leaves terminal %d by foot\n", pers->num_thread, j); + //} + + //does whatever he has to do for a D delay time (waits for rand() 1000->1999us) +#if USLEEP + usleep(rand_D()); +#endif + //changes his position from terminals i = j (j is the new terminals) + pers->i_terminals = j; + pers->nb_travels += 1; + } + printf("person %d stops\n", pers->num_thread); + pthread_mutex_lock(city_->terminals[pers->i_terminals].mutex); + city_->personsFinished += 1; + pthread_mutex_unlock(city_->terminals[pers->i_terminals].mutex); + pthread_barrier_wait(&city_->b); + + return NULL; +} + +//unused +void free_Person(struct person **pers){ + city_ = NULL; + free((*pers)); + pers = NULL; +} \ No newline at end of file diff --git a/G17_bykes/civilian.h b/G17_bykes/civilian.h new file mode 100644 index 0000000000000000000000000000000000000000..33c2a2342c56c0d512e188815ca5a0be657e0cbc --- /dev/null +++ b/G17_bykes/civilian.h @@ -0,0 +1,24 @@ +#ifndef _CIVILIAN_H_ +#define _CIVILIAN_H_ + +#include "city.h" + +#define USLEEP 1 + +struct person{ + int num_thread; + int nb_travels; + int max_travels; + int i_terminals; + int nbTerminals; + int nslots; +}; + +void init_civilians(struct person *civil, struct city *city, int nbTrajets); +int rand_D(); +void *Persons(void *arg); + +//unused +void free_Person(struct person **pers); + +#endif \ No newline at end of file diff --git a/G17_bykes/main.c b/G17_bykes/main.c index 7428bcd9a898849ebbd6743ea37b9826b6e6210b..74519e546edec7f4b1c0a09097566e89cd101812 100644 --- a/G17_bykes/main.c +++ b/G17_bykes/main.c @@ -6,327 +6,70 @@ #include <pthread.h> #include <time.h> #include <semaphore.h> - -#define TRUCK_CAPACITE_MAX 4 -#define TRUCK_CAPACITE_TYP 2 -#define CITY_CAPACITY_MIN 2 - -#define DEPOT 0 - -#define USLEEP 1 - -sem_t *sem_FreeSlot; -sem_t *sem_BikeFree; -pthread_mutex_t *mutex; -pthread_barrier_t b; - -int personsFinished=0; - -struct civilian{ - int num_thread; - int nb_travels; - int max_trajets; - int i_cities; - int nbcities; - int nslotss; -}; - -struct Truck{ - int num_thread; - int n_bike; - int i_cities; - int nbcities; - int nslotss; - int nbCivilians; -}; - -struct city{ - int nb_bikesParked; - int nb_P_waiting; -}; - -int rand_Dc(); -int rand_D(); -//void civilians_PassiveMode(void *arg); -void *trucks(void *arg); -void *civilians(void *arg); -struct city *cities; //this table is the size of the number of cities, each cities has NParkinglot and each numbers represents a free parkinglot - +#include "civilian.h" +#include "Truck.h" +#include "city.h" int main(int argc, char **argv){ if (argc < 5 || argc > 5){ perror("erreur d'arguments d'entrée\n"); return EXIT_FAILURE; } - int nbcities = atoi(argv[1]); //S 10 - int nbCivilians = atoi(argv[2]); //H 100 - int nbTrajets = atoi(argv[3]); //M 10 + int nbterminals = atoi(argv[1]); //S 10 + int nbpersons = atoi(argv[2]); //H 100 + int nbTravels = atoi(argv[3]); //M 10 int nbslots = atoi(argv[4]); //N 10 - nbcities += 1; + nbterminals += 1; srand(0); - - pthread_t *threadCivilians = malloc(sizeof(pthread_t)*nbCivilians); + pthread_t *threadpersons = malloc(sizeof(pthread_t)*nbpersons); pthread_t *threadTruck = malloc(sizeof(pthread_t)); - cities = calloc(nbcities, sizeof(struct city)); - struct civilian *Civilians = calloc(nbCivilians, sizeof(struct civilian)); - struct Truck *truck = malloc(sizeof(struct Truck)); - - for(int j = 0; j<nbcities; j++){ - cities[j].nb_bikesParked = nbslots-2; - cities[j].nb_P_waiting = 0; - } - cities[DEPOT].nb_bikesParked = 0; - truck->i_cities = 0; - truck->n_bike = TRUCK_CAPACITE_TYP; - truck->nslotss = nbslots; - truck->num_thread = nbCivilians; - truck->nbcities = nbcities; - truck->nbCivilians = nbCivilians; + struct city city; + struct person *persons = malloc(sizeof(struct person)*nbpersons); + struct Truck truck; + init_city(&city, nbterminals, nbpersons, nbslots); + printf("COUOCU COUCOU COUCOU %d\n",city.npersons); + init_civilians(persons, &city, nbTravels); + init_truck(&truck, &city); + printf("COUOCU COUCOU COUCOU \n"); - sem_BikeFree = malloc(sizeof(sem_t)*nbcities); - sem_FreeSlot = malloc(sizeof(sem_t)*nbcities); - mutex = malloc(sizeof(pthread_mutex_t)*nbcities); - - for(int x =0; x<nbcities; x++){ - sem_init(&sem_BikeFree[x], 0, nbslots-2);//8); - sem_init(&sem_FreeSlot[x], 0, nbslots-(nbslots-2));//2); - pthread_mutex_init(&mutex[x], NULL); //mutex du truck - } - - pthread_barrier_init(&b, NULL, nbCivilians); - - if(pthread_create(threadTruck, NULL, trucks, (void*)truck)!=0){ + if(pthread_create(threadTruck, NULL, trucks, (void*)&truck)!=0){ perror("thread creation error"); return EXIT_FAILURE; } - for(int i=0; i<nbCivilians; i++){ - Civilians[i].max_trajets = nbTrajets; - Civilians[i].nslotss = nbslots; - Civilians[i].nbcities = nbcities; - Civilians[i].num_thread = i; - Civilians[i].nb_travels = 0; - Civilians[i].i_cities = (rand()%(Civilians[i].nbcities - 1))+1; - if(pthread_create(&threadCivilians[i], NULL, civilians, (void*)&Civilians[i])!=0){ + for(int i=0; i<nbpersons; i++){ + if(pthread_create(&threadpersons[i], NULL, Persons, (void*)&persons[i])!=0){ perror("thread creation error"); return EXIT_FAILURE; } } - //pthread_barrier_wait(&b); if(pthread_join(*threadTruck, NULL)){ perror("thread join error"); return EXIT_FAILURE; } - //thread join - for(int i=0; i<nbCivilians; i++){ - if(pthread_join(threadCivilians[i], NULL)){ + for(int i=0; i<nbpersons; i++){ + if(pthread_join(threadpersons[i], NULL)){ perror("thread join error"); return EXIT_FAILURE; } } - //free the memory - for(int i=0; i<nbcities; i++){ - sem_destroy(&sem_BikeFree[i]); - sem_destroy(&sem_FreeSlot[i]); - pthread_mutex_destroy(&mutex[i]); - } - pthread_barrier_destroy(&b); - free(threadCivilians); - free(threadTruck); - free(sem_BikeFree); - free(sem_FreeSlot); - free(mutex); + printf("***************** CYCLING TERMINATED ***************\n"); int sum=0; - for(int i=1; i<nbcities; i++){ - printf("Terminal %d contains %d bikes\n", i, cities[i].nb_bikesParked); - sum+=cities[i].nb_bikesParked; - } - printf("total number of bikes in town: %d, in depot: %d, in truck: %d, total: %d\n", sum, cities[DEPOT].nb_bikesParked, truck->n_bike, truck->n_bike+cities[DEPOT].nb_bikesParked+sum); - - free(Civilians); - free(truck); - free(cities); - return EXIT_SUCCESS; -} - -void *civilians(void *arg){ - struct civilian *hab = (struct civilian*)arg; - int j=0; - int val; - // inside a Mtrajets loop for each civilians - while(hab->nb_travels < hab->max_trajets){ - //waits for a bike in cities i - //if(hab->i_cities==4){ - sem_getvalue(&sem_FreeSlot[hab->i_cities],&val); - pthread_mutex_lock(&mutex[hab->i_cities]); - printf("(GET) person %d starts from terminal %d (%d bykes, %d persons waiting, semslotsFree_val : %d)\n", hab->num_thread, hab->i_cities, cities[hab->i_cities].nb_bikesParked, cities[hab->i_cities].nb_P_waiting, val);//cities[hab->i_cities].nb_P_waiting); - pthread_mutex_unlock(&mutex[hab->i_cities]); - //} - - if(cities[hab->i_cities].nb_bikesParked<=0){// && hab->i_cities == 4){ - printf("person %d is STUCK at terminal %d: rack empty : %d bikes\n", hab->num_thread, hab->i_cities, cities[hab->i_cities].nb_bikesParked); - } - - pthread_mutex_lock(&mutex[hab->i_cities]); - cities[hab->i_cities].nb_P_waiting += 1; - pthread_mutex_unlock(&mutex[hab->i_cities]); - - - sem_wait(&sem_BikeFree[hab->i_cities]); - pthread_mutex_lock(&mutex[hab->i_cities]); - cities[hab->i_cities].nb_P_waiting -= 1; - cities[hab->i_cities].nb_bikesParked -= 1; //critical section - //if(hab->i_cities==4){ - //printf("person %d leaves terminal %d by bike\n", hab->num_thread, hab->i_cities); - //} - pthread_mutex_unlock(&mutex[hab->i_cities]); - sem_post(&sem_FreeSlot[hab->i_cities]); - //if(hab->i_cities==4){ - printf("person %d leaves terminal %d by bike\n", hab->num_thread, hab->i_cities); - //} - - - //go to place j = rand()%ncities and j != i - do{ - j = (rand()%(hab->nbcities - 1))+1; - }while(j==hab->i_cities); -#if USLEEP - usleep(rand_D()); -#endif - - //waits for a parkinglot to be freed and deposit his bycycle - //if(j==4){ - sem_getvalue(&sem_BikeFree[j],&val); - pthread_mutex_lock(&mutex[j]); - printf("(PUT) person %d arrives at terminal %d (%d bykes, %d persons waiting, semBikeFree_val : %d)\n", hab->num_thread, j, cities[j].nb_bikesParked, cities[j].nb_P_waiting, val);//cities[j].nb_P_waiting); - pthread_mutex_unlock(&mutex[j]); - //} - - - if(cities[j].nb_bikesParked>=hab->nslotss){//} && j == 4){ - printf("person %d is STUCK at terminal %d: rack full : %d bikes\n", hab->num_thread, j, cities[j].nb_bikesParked); - } - - pthread_mutex_lock(&mutex[j]); - cities[j].nb_P_waiting += 1; - pthread_mutex_unlock(&mutex[j]); - - - sem_wait(&sem_FreeSlot[j]); - pthread_mutex_lock(&mutex[j]); - //deposit his bycycle - cities[j].nb_P_waiting -= 1; - cities[j].nb_bikesParked += 1; //critical section - pthread_mutex_unlock(&mutex[j]); - sem_post(&sem_BikeFree[j]); - //if(j==4){ - printf("person %d leaves terminal %d by foot\n", hab->num_thread, j); - //} - - //does whatever he has to do for a D delay time (waits for rand() 1000->1999us) -#if USLEEP - usleep(rand_D()); -#endif - //changes his position from cities i = j (j is the new cities) - hab->i_cities = j; - hab->nb_travels += 1; + for(int i=1; i<city.nTerminal; i++){ + printf("Terminal %d contains %d bikes\n", i, city.terminals[i].nb_bikesParked); + sum+=city.terminals[i].nb_bikesParked; } - printf("person %d stops\n", hab->num_thread); - pthread_mutex_lock(&mutex[hab->i_cities]); - personsFinished += 1; - pthread_mutex_unlock(&mutex[hab->i_cities]); - pthread_barrier_wait(&b); - //printf("machin bidule a passé la barriere !!!! : %d, tot_trajets = %d\n", hab->num_thread, hab->nb_travels); - - return NULL; -} + printf("total number of bikes in town: %d, in depot: %d, in truck: %d, total: %d\n", sum, city.terminals[DEPOT].nb_bikesParked, truck.n_bike, truck.n_bike+city.terminals[DEPOT].nb_bikesParked+sum); -void *trucks(void *arg){ - struct Truck *truck = (struct Truck*)arg; - int tmp_nbBikes; - - while(personsFinished < truck->nbCivilians){ - //1 : for each cities from 1 to S (0 is the depot) - for(int i=1; i<truck->nbcities; i++){ - //spreads the bikes evenly - //too many bikes in this cities - tmp_nbBikes = truck->n_bike; - if(cities[i].nb_bikesParked > (truck->nslotss-2) && truck->n_bike < TRUCK_CAPACITE_MAX){ - while(cities[i].nb_bikesParked > (truck->nslotss-2) && truck->n_bike < TRUCK_CAPACITE_MAX){ - sem_wait(&sem_BikeFree[i]); - pthread_mutex_lock(&mutex[i]); - truck->n_bike += 1; - cities[i].nb_bikesParked -= 1; - pthread_mutex_unlock(&mutex[i]); - sem_post(&sem_FreeSlot[i]); - } - - pthread_mutex_lock(&mutex[i]); - printf("truck removes %d bikes at terminal %d (%d bikes, %d persons waiting, %d left in truck)\n", truck->n_bike-tmp_nbBikes, i, cities[i].nb_bikesParked, cities[i].nb_P_waiting, truck->n_bike); - pthread_mutex_unlock(&mutex[i]); - } - // not enough bikes in this cities - // until he gets at least 2 bikes (or the truck is empty) - else if(cities[i].nb_bikesParked < CITY_CAPACITY_MIN && truck->n_bike > 0){ - tmp_nbBikes = truck->n_bike; - while(cities[i].nb_bikesParked < CITY_CAPACITY_MIN && truck->n_bike > 0){ - sem_wait(&sem_FreeSlot[i]); - pthread_mutex_lock(&mutex[i]); - truck->n_bike -= 1; - cities[i].nb_bikesParked += 1; - pthread_mutex_unlock(&mutex[i]); - sem_post(&sem_BikeFree[i]); - } - - pthread_mutex_lock(&mutex[i]); - printf("truck adds %d bikes at terminal %d (%d bikes, %d persons waiting, %d left in truck)\n", tmp_nbBikes-truck->n_bike, i, cities[i].nb_bikesParked, cities[i].nb_P_waiting, truck->n_bike); - pthread_mutex_unlock(&mutex[i]); - } - } - //2: goes to the depot to rebalance the truck's content to 2 bycycles - tmp_nbBikes = truck->n_bike; - if(truck->n_bike < TRUCK_CAPACITE_TYP){ - if(cities[DEPOT].nb_bikesParked > 0){ - while(truck->n_bike < TRUCK_CAPACITE_TYP && cities[DEPOT].nb_bikesParked > 0){ - truck->n_bike += 1; - pthread_mutex_lock(&mutex[DEPOT]); - cities[DEPOT].nb_bikesParked -= 1; - pthread_mutex_unlock(&mutex[DEPOT]); - } - pthread_mutex_lock(&mutex[DEPOT]); - printf("(DEPOT) truck gets %d bikes at terminal %d (%d bikes, %d persons waiting, %d left in truck)\n", truck->n_bike-tmp_nbBikes, DEPOT, cities[DEPOT].nb_bikesParked, cities[DEPOT].nb_P_waiting, truck->n_bike); - pthread_mutex_unlock(&mutex[DEPOT]); - } - } - else if (truck->n_bike > TRUCK_CAPACITE_TYP){ - while(truck->n_bike > TRUCK_CAPACITE_TYP){ - truck->n_bike -= 1; - pthread_mutex_lock(&mutex[DEPOT]); - cities[DEPOT].nb_bikesParked += 1; - pthread_mutex_unlock(&mutex[DEPOT]); - } - pthread_mutex_lock(&mutex[DEPOT]); - printf("(DEPOT) truck deposit %d bikes at terminal %d (%d bikes, %d persons waiting, %d left in truck)\n", tmp_nbBikes-truck->n_bike, DEPOT, cities[DEPOT].nb_bikesParked, cities[DEPOT].nb_P_waiting, truck->n_bike); - pthread_mutex_unlock(&mutex[DEPOT]); - } - //3: pause betweem 100us up to 199us -#if USLEEP - usleep(rand_Dc()); -#endif - //printf("TRUCK new loop, person's threads finished: %d\n", personsFinished); - } - printf("TRUCK end loop\n"); - return NULL; -} -int rand_D(){ - return (rand()%999)+1000; -} + //free the memory + free(threadpersons); + free(threadTruck); + free(persons); -int rand_Dc(){ - return (rand()%99)+100; + return EXIT_SUCCESS; } \ No newline at end of file