From 248deefb118577f116d44ea22706d8214f7d94fa Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 30 May 2024 14:29:08 +0200 Subject: [PATCH] Change watch_for_starvation to not print after end This change may prevent some printing but I am not sure whther it stops it all. Further testing (or thinking) needed. --- philo_bonus/watchers.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/philo_bonus/watchers.c b/philo_bonus/watchers.c index 8176499..0ae0db2 100644 --- a/philo_bonus/watchers.c +++ b/philo_bonus/watchers.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/24 08:49:46 by ljiriste #+# #+# */ -/* Updated: 2024/05/30 11:39:51 by ljiriste ### ########.fr */ +/* Updated: 2024/05/30 14:25:33 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,22 +36,23 @@ static void *watch_for_starvation(void *philo_void) philo = philo_void; sem_wait(philo->philo_sem); time = time_till_death(philo); - while (time > 0) + while (time > 0 && philo->alive) { - if (!philo->alive) - { - sem_post(philo->philo_sem); - return (NULL); - } sem_post(philo->philo_sem); usleep(time / 2); sem_wait(philo->philo_sem); time = time_till_death(philo); } sem_wait(philo->semaphores.term); - print_timestamp(philo, "died"); - philo->alive = 0; - sem_post(philo->semaphores.death); + if (philo->alive) + { + print_timestamp(philo, "died"); + philo->alive = 0; + sem_post(philo->semaphores.death); + sem_post(philo->semaphores.end); + } + else + sem_post(philo->semaphores.term); sem_post(philo->philo_sem); return (NULL); } -- 2.30.2