From 79ba79ea49cd92cc85ba5dd709090ecb7521aacf Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 30 May 2024 15:00:14 +0200 Subject: [PATCH] Improve error messages and a minor change --- philo_bonus/main.c | 9 +++++---- philo_bonus/philo.c | 15 +++++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/philo_bonus/main.c b/philo_bonus/main.c index 9b6f87d..f2a7044 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 13:31:16 by ljiriste ### ########.fr */ +/* Updated: 2024/05/30 14:56:37 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,6 +47,7 @@ static int seat_philosophers(t_settings *settings, t_sems *semaphores) void kill_philosophers(t_sems *semaphores) { + sem_wait(semaphores->term); sem_post(semaphores->death); return ; } @@ -69,11 +70,11 @@ void *check_fed(void *input) if (!*end) { sem_wait(semaphores->term); + sem_post(semaphores->death); printf("All philosophers are well fed\n"); *end = 1; } sem_post(semaphores->check_end); - sem_post(semaphores->death); return (NULL); } @@ -120,14 +121,14 @@ int main(int argc, char **argv) } if (init(settings.philo_count, &semaphores)) { - printf("An error has occured.\n"); + printf("An initialization error has occured.\n"); cleanup(&semaphores); return (2); } if (seat_philosophers(&settings, &semaphores)) { kill_philosophers(&semaphores); - printf("An error has occured.\n"); + printf("A forking error has occured.\n"); cleanup(&semaphores); return (3); } diff --git a/philo_bonus/philo.c b/philo_bonus/philo.c index 63455c8..a8b62f8 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/30 13:31:16 by ljiriste ### ########.fr */ +/* Updated: 2024/05/30 14:43:45 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -87,7 +87,12 @@ static void clear_philo(t_philo *philo) static void error_exit(t_philo *philo, int exit_code) { sem_wait(philo->semaphores.term); - printf("An error has occured.\n"); + if (exit_code == 1) + printf("Philosopher %u has encountered an error creating the name of a semaphore.\n", philo->id); + else if (exit_code == 2) + printf("Philosopher %u has encountered an error opening a semaphore.\n", philo->id); + else if (exit_code == 3) + printf("Philosopher %u has encountered an error creating a watcher.\n", philo->id); sem_post(philo->semaphores.death); sem_post(philo->semaphores.end); clear_philo(philo); @@ -96,10 +101,12 @@ static void error_exit(t_philo *philo, int exit_code) void be_a_philosopher(t_philo *philo) { + int res; pthread_t watchers[2]; - if (open_semaphores(philo)) - error_exit(philo, 2); + res = open_semaphores(philo); + if (res) + error_exit(philo, res); if (create_watchers(philo, watchers)) error_exit(philo, 3); while (is_alive(philo)) -- 2.30.2