From eb85c4c385435c19d88a6ad117f124658c0ba15c Mon Sep 17 00:00:00 2001 From: whaffman Date: Fri, 24 Jan 2025 16:43:46 +0100 Subject: [PATCH] WIP geen idee --- philo/inc/philo.h | 1 + philo/src/philo_routine.c | 49 +++++++++++++++++++++++----------- philo/src/utils/print_status.c | 2 ++ 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/philo/inc/philo.h b/philo/inc/philo.h index 6604b5c..4b34017 100644 --- a/philo/inc/philo.h +++ b/philo/inc/philo.h @@ -55,6 +55,7 @@ typedef struct s_philo pthread_mutex_t *l_fork; pthread_mutex_t *r_fork; int death; + pthread_mutex_t *death_lock; int last_meal; t_rules *rules; int n_eat; diff --git a/philo/src/philo_routine.c b/philo/src/philo_routine.c index cf2cf36..599fed8 100644 --- a/philo/src/philo_routine.c +++ b/philo/src/philo_routine.c @@ -31,6 +31,23 @@ int get_finished(t_rules *rules) return (finished); } +void set_death(t_philo *philo) +{ + pthread_mutex_lock(philo->death_lock); + philo->death = 1; + pthread_mutex_unlock(philo->death_lock); +} + +int get_death(t_philo *philo) +{ + int death; + + pthread_mutex_lock(philo->death_lock); + death = philo->death; + pthread_mutex_unlock(philo->death_lock); + return (death); +} + void philo_die(t_philo *philo) { print_status(philo, "died"); @@ -40,17 +57,17 @@ void philo_die(t_philo *philo) pthread_mutex_unlock(philo->r_fork); } -int philo_died_while_sleeping(t_philo *philo, int time) +int philo_sleep(t_philo *philo, int time) { const int start = get_time(); while (get_time() - start < time) { - if (philo->last_meal - get_time() >= philo->rules->time_to_die) - return (SUCCESS); - usleep(500); + if (philo->death) + return (FAILURE); + usleep(50); } - return (FAILURE); + return (SUCCESS); } int philo_eat(t_philo *philo) { @@ -66,8 +83,8 @@ int philo_eat(t_philo *philo) pthread_mutex_lock(philo->r_fork); print_status(philo, "has taken a fork"); } - if (check_death(philo)) - return (philo_die(philo), FAILURE); + if (philo->death) + return (FAILURE); print_status(philo, "is eating"); philo->last_meal = get_time(); philo->n_eat++; @@ -93,19 +110,19 @@ void *philo_routine(void *arg) synchronize_philos(philo); while (!philo->death && !get_finished(philo->rules)) { - if (check_death(philo)) - return (philo_die(philo), NULL); + if (philo->death) + break ; if (!philo_eat(philo)) - return (NULL); - if (philo_died_while_sleeping(philo, philo->rules->time_to_eat)) - return (philo_die(philo), NULL); + break ; + if (!philo_sleep(philo, philo->rules->time_to_eat)) + break ; pthread_mutex_unlock(philo->l_fork); pthread_mutex_unlock(philo->r_fork); - if (!philo->rules->finished) + if (!philo->death) print_status(philo, "is sleeping"); - if (philo_died_while_sleeping(philo, philo->rules->time_to_sleep)) - return (philo_die(philo), NULL); - if(!philo->rules->finished) + if (!philo_sleep(philo, philo->rules->time_to_sleep)) + break; + if(!philo->death) print_status(philo, "is thinking"); } return (NULL); diff --git a/philo/src/utils/print_status.c b/philo/src/utils/print_status.c index 7f0533e..99e3115 100644 --- a/philo/src/utils/print_status.c +++ b/philo/src/utils/print_status.c @@ -14,6 +14,8 @@ void print_status(t_philo *philo, char *status) { + if (philo->death) + return ; pthread_mutex_lock(philo->rules->print_lock); printf("%d %d %s\n", get_time() - philo->rules->start_time,