From a124a0c436276fb0a6578a940d7b7259580df202 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Tue, 28 May 2024 14:48:39 +0200 Subject: [PATCH] Rewrite semaphore initialization, add end semaphore It is better to create semaphores at 0 than at some value and then subtracting them to 0. The end semaphore will be used to signal to philosophers they should end. This mechanism was chosen instead of kill function, because the philosophers have to clean after themselves and signal handlers cannot be used. --- philo_bonus/mem_management.c | 24 +++++++++--------------- philo_bonus/philo.h | 2 ++ 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/philo_bonus/mem_management.c b/philo_bonus/mem_management.c index 1fc290d..b79c3d5 100644 --- a/philo_bonus/mem_management.c +++ b/philo_bonus/mem_management.c @@ -22,35 +22,27 @@ static int open_semaphores(unsigned int count, t_sems *semaphores) semaphores->forks = sem_open(FORKS_SEM, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, count); semaphores->fed = sem_open(WELL_FED_SEM, O_CREAT | O_EXCL, - S_IRUSR | S_IWUSR, count); + S_IRUSR | S_IWUSR, 0); semaphores->death = sem_open(DEATH_SEM, O_CREAT | O_EXCL, - S_IRUSR | S_IWUSR, count); + S_IRUSR | S_IWUSR, 0); semaphores->term = sem_open(TERM_SEM, O_CREAT | O_EXCL, - S_IRUSR | S_IWUSR, 1); + S_IRUSR | S_IWUSR, 0); + semaphores->end = sem_open(END_SEM, O_CREAT | O_EXCL, + S_IRUSR | S_IWUSR, 0); if (semaphores->forks == SEM_FAILED || semaphores->fed == SEM_FAILED - || semaphores->death == SEM_FAILED || semaphores->term == SEM_FAILED) + || semaphores->death == SEM_FAILED || semaphores->term == SEM_FAILED + || semaphores->end == SEM_FAILED) return (1); return (0); } int init(unsigned int count, pid_t **philo_pids, t_sems *semaphores) { - size_t i; - *philo_pids = malloc(count * sizeof(**philo_pids)); if (!*philo_pids) return (1); if (open_semaphores(count, semaphores)) return (1); - i = 0; - while (i < count) - { - (*philo_pids)[i] = 0; - sem_wait(semaphores->death); - sem_wait(semaphores->fed); - ++i; - } - sem_wait(semaphores->term); return (0); } @@ -60,6 +52,7 @@ void close_semaphores(t_sems *semaphores) sem_close(semaphores->fed); sem_close(semaphores->death); sem_close(semaphores->term); + sem_close(semaphores->end); return ; } @@ -70,6 +63,7 @@ void cleanup(pid_t *philo_pids, t_sems *semaphores) sem_unlink(WELL_FED_SEM); sem_unlink(DEATH_SEM); sem_unlink(TERM_SEM); + sem_unlink(END_SEM); free(philo_pids); return ; } diff --git a/philo_bonus/philo.h b/philo_bonus/philo.h index b7d461c..b31e659 100644 --- a/philo_bonus/philo.h +++ b/philo_bonus/philo.h @@ -23,6 +23,7 @@ # define WELL_FED_SEM "philosophers_fed" # define DEATH_SEM "philosophers_death" # define TERM_SEM "philosophers_terminal" +# define END_SEM "philosophers_end" // The .start member holds the result of gettimeofday // Every other member that holds time info counts @@ -44,6 +45,7 @@ typedef struct s_important_semaphores sem_t *fed; sem_t *death; sem_t *term; + sem_t *end; } t_sems; // As mutex is not available in bonus, the idea is to have -- 2.30.2