diff --git a/data.c b/data.c index 9573ec1..55c35c3 100644 --- a/data.c +++ b/data.c @@ -6,12 +6,14 @@ #include "philo.h" #include "philos.h" #include "struct.h" +#include bool data_init(t_data *data) { data->forks = malloc(sizeof(bool) * data->nb_philos); if (data->forks == NULL) return (1); + memset(data->forks, 1, data->nb_philos); data->philos = malloc(sizeof(t_philo) * data->nb_philos); if (data->philos == NULL) { @@ -25,8 +27,9 @@ bool data_init(t_data *data) free(data->forks); return (1); } + pthread_mutex_init(&data->forks_mutex, NULL); + pthread_mutex_init(&data->stop_mutex, NULL); data->stop = 0; - data->nb_meals = 0; return (0); } @@ -34,7 +37,6 @@ void data_destroyer(t_data *data) { free(data->threads); free(data->forks); - pthread_mutex_destroy(&data->nb_meal_mutex); pthread_mutex_destroy(&data->forks_mutex); pthread_mutex_destroy(&data->stop_mutex); philos_destroyer(data); diff --git a/main.c b/main.c index a2b15f8..dd35aa2 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "./philos.h" #include "./data.h" @@ -49,8 +50,11 @@ void check_routine(t_data *data) while (true) { if ((ssize_t) get_min_meal(data) == data->nb_meals) + { + printf("g pu faim\n"); stop(data); - + return ; + } i = 0; while (i < data->nb_philos) { diff --git a/philo.c b/philo.c index f574fbc..2e5d392 100644 --- a/philo.c +++ b/philo.c @@ -4,6 +4,8 @@ #include "data.h" #include #include +#include +#include #include #include "./print.h" @@ -36,7 +38,7 @@ void eat(t_philo *philo, t_data *data) pthread_mutex_unlock(&philo->nb_meal_mutex); pthread_mutex_lock(&data->forks_mutex); data->forks[philo->id] = 1; - data->forks[(philo->id + 1) % (data->nb_philos) + 1] = 1; + data->forks[(philo->id + 1) % data->nb_philos] = 1; pthread_mutex_unlock(&data->forks_mutex); } @@ -53,13 +55,14 @@ bool philo_eat(t_philo *philo, t_data *data) return (1); pthread_mutex_lock(&data->forks_mutex); left_fork = data->forks[philo->id]; - right_fork = data->forks[(philo->id + 1) % (data->nb_philos) + 1]; + right_fork = data->forks[(philo->id + 1) % data->nb_philos]; if (right_fork && left_fork) { - data->forks[philo->id] = !left_fork; - data->forks[(philo->id + 1) % (data->nb_philos) + 1] = !right_fork; + data->forks[philo->id] = 0; + data->forks[(philo->id + 1) % data->nb_philos] = 0; } pthread_mutex_unlock(&data->forks_mutex); + usleep(10); } eat(philo, data); pthread_mutex_lock(&philo->last_eat_mutex); @@ -82,13 +85,15 @@ void *philo_routine(void *arg) philo = arg; data = philo->data; + if (philo->id % 2) + usleep(data->sleep_time * 1000); while (true) { print_thinking(philo); - check(data); - philo_eat(philo, data); - check(data); - print_thinking(philo); + if (check(data)) + return (NULL); + if (philo_eat(philo, data)) + return (NULL); if (check(data)) return (NULL); philo_sleep(data, philo); @@ -107,8 +112,9 @@ t_philo *philo_init(t_data *data) philo->id = id++; philo->data = data; philo->nb_meal = 0; + philo->last_eat = get_time(); pthread_mutex_init(&philo->nb_meal_mutex, NULL); - pthread_mutex_init(&philo->nb_meal_mutex, NULL); - pthread_mutex_init(&philo->nb_meal_mutex, NULL); + pthread_mutex_init(&philo->last_eat_mutex, NULL); + pthread_mutex_init(&philo->last_sleep_mutex, NULL); return (philo); } diff --git a/print.c b/print.c index f1a9904..f50192e 100644 --- a/print.c +++ b/print.c @@ -3,39 +3,47 @@ #include #include #include "./time.h" +#include "struct.h" -static void print(size_t id, char *str) +static void print(t_data *data, size_t id, char *str) { static pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER; size_t time; + pthread_mutex_lock(&data->stop_mutex); + if (data->stop) + { + pthread_mutex_unlock(&data->stop_mutex); + return ; + } + pthread_mutex_unlock(&data->stop_mutex); time = get_time(); pthread_mutex_lock(&print_mutex); - printf("%zu %zu %s\n", time, id, str); + printf("%6zu %3zu %s\n", time, id, str); pthread_mutex_unlock(&print_mutex); } void print_take_a_fork(t_philo *philo) { - print(philo->id, "has taken a fork"); + print(philo->data, philo->id, "has taken a fork"); } void print_eating(t_philo *philo) { - print(philo->id, "is eating"); + print(philo->data, philo->id, "is eating"); } void print_sleeping(t_philo *philo) { - print(philo->id, "is sleeping"); + print(philo->data, philo->id, "is sleeping"); } void print_thinking(t_philo *philo) { - print(philo->id, "is thinking"); + print(philo->data, philo->id, "is thinking"); } void print_died(t_philo *philo) { - print(philo->id, "is died"); + print(philo->data, philo->id, "is died"); } diff --git a/threads.c b/threads.c index 1677d9c..1414fe9 100644 --- a/threads.c +++ b/threads.c @@ -2,6 +2,7 @@ #include "struct.h" #include #include +#include #include bool threads_init(t_data *data) diff --git a/time.c b/time.c index b1b8cb7..fb42605 100644 --- a/time.c +++ b/time.c @@ -5,13 +5,10 @@ size_t get_time(void) { - size_t timedefault = 0; size_t time; struct timeval tv; gettimeofday(&tv, NULL); time = tv.tv_sec * 1000000 + tv.tv_usec; - if (timedefault == 0) - timedefault = time; - return ((time - timedefault) / 1000); + return (time / 1000); }