From 4814f4ebc2a1e924626a60dd84d2f4b2bbec52c1 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 30 May 2024 12:50:12 +0200 Subject: [PATCH] Further improve philos memory, make 1 philo work 1 philo did not ever exit because he did not ever get out of waiting for a fork. This is rectified by main posting new forks at the end. Philo cleanup has been refactored to ease with the error printing. --- philo_bonus/main.c | 3 ++- philo_bonus/philo.c | 49 +++++++++++++++++++++++---------------------- philo_bonus/philo.h | 3 ++- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/philo_bonus/main.c b/philo_bonus/main.c index 477aba3..ef7cae7 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 12:05:00 by ljiriste ### ########.fr */ +/* Updated: 2024/05/30 12:46:31 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -100,6 +100,7 @@ void wait_for_end(unsigned int philo_count, t_sems *semaphores) { sem_wait(semaphores->end); sem_post(semaphores->fed); + sem_post(semaphores->forks); --philo_count; } sem_post(semaphores->term); diff --git a/philo_bonus/philo.c b/philo_bonus/philo.c index 30f9229..bb44b9c 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 12:08:48 by ljiriste ### ########.fr */ +/* Updated: 2024/05/30 12:49:14 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,14 +16,13 @@ #include #include -static char *open_semaphores(t_philo *philo) +static int open_semaphores(t_philo *philo) { - char *sem_name; - - sem_name = create_philo_sem_name(philo->id); - if (!sem_name) - return (NULL); - philo->philo_sem = sem_open(sem_name, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 1); + philo->philo_sem_name = create_philo_sem_name(philo->id); + if (!philo->philo_sem_name) + return (1); + philo->philo_sem = sem_open(philo->philo_sem_name, O_CREAT | O_EXCL, + S_IRUSR | S_IWUSR, 1); philo->semaphores.forks = sem_open(FORKS_SEM, 0); philo->semaphores.fed = sem_open(WELL_FED_SEM, 0); philo->semaphores.death = sem_open(DEATH_SEM, 0); @@ -35,11 +34,9 @@ static char *open_semaphores(t_philo *philo) || philo->semaphores.death == SEM_FAILED || philo->semaphores.term == SEM_FAILED || philo->semaphores.end == SEM_FAILED) - { - free(sem_name); - return (NULL); - } - return (sem_name); + return (2); + philo->semaphores.check_end = NULL; + return (0); } static void philo_think(t_philo *philo) @@ -75,30 +72,36 @@ static void philo_sleep(t_philo *philo) return ; } +static void clear_philo(t_philo *philo) +{ + close_semaphores(&philo->semaphores); + if (philo->philo_sem_name && philo->philo_sem != SEM_FAILED) + { + sem_close(philo->philo_sem); + sem_unlink(philo->philo_sem_name); + } + free(philo->philo_sem_name); + return ; +} + static void error_exit(t_philo *philo, int exit_code) { sem_wait(philo->semaphores.term); printf("An error as occured.\n"); sem_post(philo->semaphores.death); sem_post(philo->semaphores.end); - close_semaphores(&philo->semaphores); + clear_philo(philo); exit(exit_code); } void be_a_philosopher(t_philo *philo) { pthread_t watchers[2]; - char *philo_sem_name; - philo_sem_name = open_semaphores(philo); - if (!philo_sem_name) + if (open_semaphores(philo)) error_exit(philo, 2); if (create_watchers(philo, watchers)) - { - sem_unlink(philo_sem_name); - free(philo_sem_name); error_exit(philo, 3); - } while (is_alive(philo)) { philo_think(philo); @@ -106,8 +109,6 @@ void be_a_philosopher(t_philo *philo) philo_sleep(philo); } destroy_watchers(watchers); - close_semaphores(&philo->semaphores); - sem_unlink(philo_sem_name); - free(philo_sem_name); + clear_philo(philo); exit(0); } diff --git a/philo_bonus/philo.h b/philo_bonus/philo.h index 8e311ae..4602019 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:40:40 by ljiriste ### ########.fr */ +/* Updated: 2024/05/30 12:18:20 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,6 +65,7 @@ typedef struct s_philosopher int alive; useconds_t last_eaten; t_sems semaphores; + char *philo_sem_name; sem_t *philo_sem; t_settings settings; } t_philo; -- 2.30.2