From 4d1c7c55264f4fdd45abb4cbcb45ad6289463853 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 30 May 2024 11:45:43 +0200 Subject: [PATCH] Implement the philo side of the protocol The protocol is drafted in previous commit (commit 6980661). The program now produces the correct output but needs to be checked for bugs and races. The hunger_watcher.c is renamed due to it containing more than just the hunger watcher. --- philo_bonus/Makefile | 2 +- philo_bonus/main.c | 3 +- philo_bonus/philo.c | 9 +++-- philo_bonus/philo.h | 6 +-- philo_bonus/{hunger_watcher.c => watchers.c} | 42 +++++++++++++++++--- 5 files changed, 47 insertions(+), 15 deletions(-) rename philo_bonus/{hunger_watcher.c => watchers.c} (66%) diff --git a/philo_bonus/Makefile b/philo_bonus/Makefile index ebd147c..4f85edb 100644 --- a/philo_bonus/Makefile +++ b/philo_bonus/Makefile @@ -14,7 +14,7 @@ SRCS := main.c \ pars_arg.c \ inner_pars_arg.c \ mem_management.c \ - hunger_watcher.c \ + watchers.c \ libft_helpers.c \ SRCS := $(addprefix $(SRCDIR)/, $(SRCS)) diff --git a/philo_bonus/main.c b/philo_bonus/main.c index 41b985c..3b7687f 100644 --- a/philo_bonus/main.c +++ b/philo_bonus/main.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/22 11:19:48 by ljiriste #+# #+# */ -/* Updated: 2024/05/30 10:15:37 by ljiriste ### ########.fr */ +/* Updated: 2024/05/30 11:41:40 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -91,6 +91,7 @@ void wait_for_end(unsigned int philo_count, t_sems *semaphores) check_input.semaphores = semaphores; pthread_create(&thread_fed, NULL, check_fed, &check_input); sem_wait(semaphores->death); + sem_post(semaphores->death); sem_wait(semaphores->check_end); end = 1; sem_post(semaphores->check_end); diff --git a/philo_bonus/philo.c b/philo_bonus/philo.c index c56ea19..40bc310 100644 --- a/philo_bonus/philo.c +++ b/philo_bonus/philo.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 08:45:21 by ljiriste #+# #+# */ -/* Updated: 2024/05/24 15:22:54 by ljiriste ### ########.fr */ +/* Updated: 2024/05/30 11:40:22 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,7 @@ static char *open_semaphores(t_philo *philo) philo->semaphores.fed = sem_open(WELL_FED_SEM, 0); philo->semaphores.death = sem_open(DEATH_SEM, 0); philo->semaphores.term = sem_open(TERM_SEM, 0); + philo->semaphores.end = sem_open(END_SEM, 0); sem_name = create_philo_sem_name(philo->id); if (!sem_name) return (NULL); @@ -74,13 +75,13 @@ static void philo_sleep(t_philo *philo) void be_a_philosopher(t_philo *philo) { - pthread_t hunger_watcher; + pthread_t watchers[2]; char *philo_sem_name; philo_sem_name = open_semaphores(philo); if (!philo_sem_name) exit(2); - if (create_hunger_watcher(philo, &hunger_watcher)) + if (create_watchers(philo, watchers)) exit(3); while (is_alive(philo)) { @@ -88,7 +89,7 @@ void be_a_philosopher(t_philo *philo) philo_eat(philo); philo_sleep(philo); } - destroy_hunger_watcher(hunger_watcher); + destroy_watchers(watchers); close_semaphores(&philo->semaphores); sem_unlink(philo_sem_name); free(philo_sem_name); diff --git a/philo_bonus/philo.h b/philo_bonus/philo.h index 0895457..8e311ae 100644 --- a/philo_bonus/philo.h +++ b/philo_bonus/philo.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/22 11:10:17 by ljiriste #+# #+# */ -/* Updated: 2024/05/30 10:10:11 by ljiriste ### ########.fr */ +/* Updated: 2024/05/30 10:40:40 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -89,8 +89,8 @@ void report(t_philo *philo, const char *str); void print_timestamp(t_philo *philo, const char *str); char *create_philo_sem_name(unsigned int id); -int create_hunger_watcher(t_philo *philo, pthread_t *hunger_watcher); -void destroy_hunger_watcher(pthread_t hunger_watcher); +int create_watchers(t_philo *philo, pthread_t watchers[2]); +void destroy_watchers(pthread_t watchers[2]); size_t ft_strlen(const char *str); char *ft_uitoa_base(uintmax_t n, const char *base); diff --git a/philo_bonus/hunger_watcher.c b/philo_bonus/watchers.c similarity index 66% rename from philo_bonus/hunger_watcher.c rename to philo_bonus/watchers.c index 3dbe35f..8176499 100644 --- a/philo_bonus/hunger_watcher.c +++ b/philo_bonus/watchers.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* hunger_watcher.c :+: :+: :+: */ +/* watchers.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/24 08:49:46 by ljiriste #+# #+# */ -/* Updated: 2024/05/30 10:00:31 by ljiriste ### ########.fr */ +/* Updated: 2024/05/30 11:39:51 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,6 +38,11 @@ static void *watch_for_starvation(void *philo_void) time = time_till_death(philo); while (time > 0) { + if (!philo->alive) + { + sem_post(philo->philo_sem); + return (NULL); + } sem_post(philo->philo_sem); usleep(time / 2); sem_wait(philo->philo_sem); @@ -51,13 +56,38 @@ static void *watch_for_starvation(void *philo_void) return (NULL); } -int create_hunger_watcher(t_philo *philo, pthread_t *hunger_watcher) +void *watch_for_end(void *philo_void) { - return (pthread_create(hunger_watcher, NULL, watch_for_starvation, philo)); + t_philo *philo; + + philo = (t_philo *)philo_void; + sem_wait(philo->semaphores.death); + sem_post(philo->semaphores.death); + sem_wait(philo->philo_sem); + philo->alive = 0; + sem_post(philo->philo_sem); + sem_post(philo->semaphores.end); + return (NULL); +} + +int create_watchers(t_philo *philo, pthread_t watchers[2]) +{ + if (pthread_create(&watchers[0], NULL, watch_for_starvation, philo)) + return (1); + if (pthread_create(&watchers[1], NULL, watch_for_end, philo)) + { + sem_wait(philo->philo_sem); + philo->alive = 0; + sem_post(philo->philo_sem); + pthread_join(watchers[0], NULL); + return (1); + } + return (0); } -void destroy_hunger_watcher(pthread_t hunger_watcher) +void destroy_watchers(pthread_t watchers[2]) { - pthread_join(hunger_watcher, NULL); + pthread_join(watchers[0], NULL); + pthread_join(watchers[1], NULL); return ; } -- 2.30.2