diff --git a/tp.c b/tp.c new file mode 100644 index 0000000000000000000000000000000000000000..115b733c12137794713450f7f33203ebe78a2966 --- /dev/null +++ b/tp.c @@ -0,0 +1,119 @@ +#include <stdio.h> +#include <stdlib.h> +#include <pthread.h> +#include <semaphore.h> +#include <unistd.h> + +#define SITES 10 +#define HABITANTS 100 +#define TRAJETS 10 +#define BORNES 10 +#define CAMION_CAPACITE 4 + +typedef struct { + sem_t velos; + sem_t bornes; + int velos_dispo; +} Site; + +Site sites[SITES]; +sem_t depot; +pthread_mutex_t mutex_trajets = PTHREAD_MUTEX_INITIALIZER; +int trajets_effectues = 0; // Compteur des trajets terminés +int velos_depot = 0; + +void *habitant(void *arg) { + int id = *(int *)arg; + int site_actuel = rand() % SITES; + for (int i = 0; i < TRAJETS; i++) { + sem_wait(&sites[site_actuel].velos); + printf("(GET) Habitant %d prend un vélo au site %d\n", id, site_actuel); + sem_post(&sites[site_actuel].bornes); + + int site_suivant; + + do { + site_suivant = rand() % SITES; + } while (site_suivant == site_actuel); + + usleep((rand() % 1000 + 1000)); + sem_wait(&sites[site_suivant].bornes); + printf("(PUT) Habitant %d dépose un vélo au site %d\n", id, site_suivant); + + site_actuel = site_suivant; + usleep((rand() % 1000 + 1000)); + } + free(arg); + // 🔹 Augmenter le compteur de trajets terminés + pthread_mutex_lock(&mutex_trajets); + trajets_effectues++; + pthread_mutex_unlock(&mutex_trajets); + return NULL; +} + + +void *gestion_camion(void *arg) { +int velos_camion = 2; +while (1) { + pthread_mutex_lock(&mutex_trajets); + if (trajets_effectues >= HABITANTS) { + pthread_mutex_unlock(&mutex_trajets); + break; // 🔹 Arrêt du camion lorsque tous les habitants ont terminé + } + pthread_mutex_unlock(&mutex_trajets); + for (int i = 0; i < SITES; i++) { + if (sites[i].velos_dispo > BORNES - 2 && velos_camion < CAMION_CAPACITE) { + while(sites[i].velos_dispo != BORNES - 2){ + sem_wait(&sites[i].velos); + sites[i].velos_dispo--; + velos_camion++; + } + + printf("Camion prend un vélo ou 2 vélos au site %d\n", i); + } else if (sites[i].velos_dispo < 2 && velos_camion > 0) { + while(sites[i].velos_dispo != 2 && velos_camion != 0){ + sem_post(&sites[i].velos); + sites[i].velos_dispo++; + velos_camion--; + } + printf("Camion dépose un vélo au site %d\n", i); + } + usleep((rand() % 100 + 100)); + while(velos_camion > 2){ + velos_camion--; + velos_depot++; + } + while(velos_camion < 2 && velos_depot > 0){ + velos_camion++; + velos_depot--; + } + } +} +return NULL; +} + +int main() { +pthread_t habitants[HABITANTS], camion; +sem_init(&depot, 0, 0); + +for (int i = 0; i < SITES; i++) { + sem_init(&sites[i].velos, 0, BORNES - 2); + sem_init(&sites[i].bornes, 0, 2); + sites[i].velos_dispo = BORNES - 2; +} + +for (int i = 0; i < HABITANTS; i++) { + int *id = malloc(sizeof(int)); + *id = i; + pthread_create(&habitants[i], NULL, habitant, id); +} +pthread_create(&camion, NULL, gestion_camion, NULL); + +for (int i = 0; i < HABITANTS; i++) { + pthread_join(habitants[i], NULL); +} + +pthread_cancel(camion); +printf("Simulation terminée.\n"); +return 0; +} \ No newline at end of file