From bd927e9156e394a7c577d2d470aeb751a027e83e Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 30 May 2024 13:35:30 +0200 Subject: [PATCH] Add and fix error messaging, stop removal of files The philo program checks whether it created the semaphores. When not, it assumes another instance is running and errors out. Until this commit it would erase the semaphore files nontheless because the unlinking was not conditional. --- philo_bonus/main.c | 8 +++++++- philo_bonus/mem_management.c | 20 +++++++++++++------- philo_bonus/philo.c | 4 ++-- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/philo_bonus/main.c b/philo_bonus/main.c index f9976ab..9b6f87d 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:16:05 by ljiriste ### ########.fr */ +/* Updated: 2024/05/30 13:31:16 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -112,16 +112,22 @@ int main(int argc, char **argv) if (parse_input(&settings, argc, argv)) { + printf("The input should be in form:\n" + "\t%s number_of_philosophers time_to_die time_to_eat" + "time_to_sleep [number_of_times_each_philosopher_must_eat]\n", + argv[0]); return (1); } if (init(settings.philo_count, &semaphores)) { + printf("An error has occured.\n"); cleanup(&semaphores); return (2); } if (seat_philosophers(&settings, &semaphores)) { kill_philosophers(&semaphores); + printf("An error has occured.\n"); cleanup(&semaphores); return (3); } diff --git a/philo_bonus/mem_management.c b/philo_bonus/mem_management.c index 5f004b1..4f12de9 100644 --- a/philo_bonus/mem_management.c +++ b/philo_bonus/mem_management.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 09:39:55 by ljiriste #+# #+# */ -/* Updated: 2024/05/30 13:17:33 by ljiriste ### ########.fr */ +/* Updated: 2024/05/30 13:25:28 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,12 +65,18 @@ void close_semaphores(t_sems *semaphores) void cleanup(t_sems *semaphores) { + if (semaphores->forks != SEM_FAILED) + sem_unlink(FORKS_SEM); + if (semaphores->fed != SEM_FAILED) + sem_unlink(WELL_FED_SEM); + if (semaphores->death != SEM_FAILED) + sem_unlink(DEATH_SEM); + if (semaphores->term != SEM_FAILED) + sem_unlink(TERM_SEM); + if (semaphores->end != SEM_FAILED) + sem_unlink(END_SEM); + if (semaphores->check_end != SEM_FAILED) + sem_unlink(CHECK_END_SEM); close_semaphores(semaphores); - sem_unlink(FORKS_SEM); - sem_unlink(WELL_FED_SEM); - sem_unlink(DEATH_SEM); - sem_unlink(TERM_SEM); - sem_unlink(END_SEM); - sem_unlink(CHECK_END_SEM); return ; } diff --git a/philo_bonus/philo.c b/philo_bonus/philo.c index 1e14c13..63455c8 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:00:26 by ljiriste ### ########.fr */ +/* Updated: 2024/05/30 13:31:16 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -87,7 +87,7 @@ 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 as occured.\n"); + printf("An error has occured.\n"); sem_post(philo->semaphores.death); sem_post(philo->semaphores.end); clear_philo(philo); -- 2.30.2