From f88abe96311d1566b3234bcf40689baf726154ab Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Thu, 25 May 2023 17:31:02 +0200 Subject: [PATCH] dsfsd --- philo/data.h | 4 ++-- philo/main.c | 5 +++-- philo/philo_routine.c | 27 +++++++++++++-------------- philo/print.c | 4 ++-- philo/time.c | 18 +++++++----------- 5 files changed, 27 insertions(+), 31 deletions(-) diff --git a/philo/data.h b/philo/data.h index 95b2f85..454437d 100644 --- a/philo/data.h +++ b/philo/data.h @@ -6,7 +6,7 @@ /* By: cchauvet stop == 0) { if (routine(data)) { return (NULL); } } + return (NULL); } int main(int ac, char **av) diff --git a/philo/philo_routine.c b/philo/philo_routine.c index d88ad04..57566df 100644 --- a/philo/philo_routine.c +++ b/philo/philo_routine.c @@ -6,7 +6,7 @@ /* By: cchauvet nb_meals) + if (data->nb_meals != -1 && get_min_meal(data) == (size_t) data->nb_meals) { - pthread_mutex_lock(&philo->stop_mutex); - philo->stop = 1; - pthread_mutex_unlock(&philo->stop_mutex); - return (1); + pthread_mutex_lock(&data->stop_mutex); + data->stop = 1; + pthread_mutex_unlock(&data->stop_mutex); } pthread_mutex_lock(&data->stop_mutex); stop = data->stop; @@ -37,14 +36,14 @@ int philo_take_forks(t_philo *philo, t_data *data) { pthread_mutex_lock(&data->forks[philo->id]); print_take_a_fork(philo); - if (check(philo, data)) + if (check(data)) { pthread_mutex_unlock(&data->forks[philo->id]); return (1); } while ((philo->id + 1) % data->nb_philos == philo->id) { - if (check(philo, data)) + if (check(data)) { pthread_mutex_unlock(&data->forks[philo->id]); return (1); @@ -52,7 +51,7 @@ int philo_take_forks(t_philo *philo, t_data *data) } pthread_mutex_lock(&data->forks[(philo->id + 1) % data->nb_philos]); print_take_a_fork(philo); - if (check(philo, data)) + if (check(data)) { pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]); pthread_mutex_unlock(&data->forks[philo->id]); @@ -72,7 +71,7 @@ bool philo_eat(t_philo *philo, t_data *data) usleep(get_time_eat(philo, data) * 1000); pthread_mutex_unlock(&data->forks[philo->id]); pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]); - if (check(philo, data)) + if (check(data)) return (1); pthread_mutex_lock(&philo->nb_meal_mutex); philo->nb_meal++; @@ -98,11 +97,11 @@ void *philo_routine(void *arg) usleep((philo->id % 2) * (get_time_eat(philo, data)) * 1000); while (true) { - if (check(philo, data) + if (check(data) || philo_eat(philo, data) - || check(philo, data) + || check(data) || philo_sleep(data, philo) - || check(philo, data)) + || check(data)) { pthread_mutex_lock(&philo->stop_mutex); philo->stop = 1; diff --git a/philo/print.c b/philo/print.c index e53feba..2a7869d 100644 --- a/philo/print.c +++ b/philo/print.c @@ -6,7 +6,7 @@ /* By: cchauvet print_mutex); } diff --git a/philo/time.c b/philo/time.c index 577a926..9e57a93 100644 --- a/philo/time.c +++ b/philo/time.c @@ -6,7 +6,7 @@ /* By: cchauvet life_expectency - philo->last_eat - time); - if (value > data->eat_time) - value = data->eat_time; - return (value); + if (time + data->eat_time > data->life_expectency + philo->last_eat) + return (data->life_expectency + philo->last_eat - time); + return (data->eat_time); } size_t get_time_sleep(t_philo *philo, t_data *data) { size_t time; - size_t value; time = get_time(); - value = (data->life_expectency - (time - philo->last_eat)); - if (value > data->sleep_time) - value = data->sleep_time; - return (value); + if (time + data->sleep_time > data->life_expectency + philo->last_eat) + return (data->life_expectency + philo->last_eat - time); + return (data->sleep_time); }