From 3d2f40c1727b07ef710e56a1df03467799b37965 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Fri, 24 May 2024 15:26:10 +0200 Subject: [PATCH] Change semaphores array for a structure This change is mainly done because norminette sees the old declaration of semaphores[sem_num] as VLA. Instead of changing the enum, it may be better to reimplement semaphores as a structure. This may make a little bit more sense syntactically also? (compare semaphores[forks] with semaphores.forks) --- philo_bonus/helpers.c | 6 ++--- philo_bonus/hunger_watcher.c | 4 ++-- philo_bonus/main.c | 16 ++++++------- philo_bonus/mem_management.c | 44 +++++++++++++++++++----------------- philo_bonus/philo.c | 26 ++++++++++----------- philo_bonus/philo.h | 26 ++++++++++----------- 6 files changed, 62 insertions(+), 60 deletions(-) diff --git a/philo_bonus/helpers.c b/philo_bonus/helpers.c index 46bddff..f8caa76 100644 --- a/philo_bonus/helpers.c +++ b/philo_bonus/helpers.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 09:13:40 by ljiriste #+# #+# */ -/* Updated: 2024/05/24 14:08:05 by ljiriste ### ########.fr */ +/* Updated: 2024/05/24 15:32:49 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,9 +45,9 @@ void report(t_philo *philo, const char *str) { if (is_alive(philo)) { - sem_wait(philo->semaphores[term]); + sem_wait(philo->semaphores.term); print_timestamp(philo, str); - sem_post(philo->semaphores[term]); + sem_post(philo->semaphores.term); } return ; } diff --git a/philo_bonus/hunger_watcher.c b/philo_bonus/hunger_watcher.c index b22b59b..254dfd8 100644 --- a/philo_bonus/hunger_watcher.c +++ b/philo_bonus/hunger_watcher.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/24 08:49:46 by ljiriste #+# #+# */ -/* Updated: 2024/05/24 10:33:27 by ljiriste ### ########.fr */ +/* Updated: 2024/05/24 15:33:10 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,7 +44,7 @@ static void *watch_for_starvation(void *philo_void) time = time_till_death(philo); } philo->alive = 0; - sem_post(philo->semaphores[death]); + sem_post(philo->semaphores.death); sem_post(philo->philo_sem); return (NULL); } diff --git a/philo_bonus/main.c b/philo_bonus/main.c index 48f6e42..1605e32 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/24 09:21:34 by ljiriste ### ########.fr */ +/* Updated: 2024/05/24 15:21:33 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,25 +57,25 @@ int main(int argc, char **argv) { t_settings settings; pid_t *philo_pids; - sem_t *semaphores[sem_num]; + t_sems semaphores; if (parse_input(&settings, argc, argv)) { return (1); } - if (init(settings.philo_count, &philo_pids, semaphores)) + if (init(settings.philo_count, &philo_pids, &semaphores)) { - cleanup(philo_pids, semaphores); + cleanup(philo_pids, &semaphores); return (2); } if (seat_philosophers(&settings, philo_pids)) { kill_philosophers(settings.philo_count, philo_pids); - cleanup(philo_pids, semaphores); + cleanup(philo_pids, &semaphores); return (3); } - sem_post(semaphores[term]); - handle_philosophers(semaphores); - cleanup(philo_pids, semaphores); + sem_post(semaphores.term); + handle_philosophers(&semaphores); + cleanup(philo_pids, &semaphores); return (0); } diff --git a/philo_bonus/mem_management.c b/philo_bonus/mem_management.c index a8a3cd8..1fc290d 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/12 21:54:03 by ljiriste ### ########.fr */ +/* Updated: 2024/05/24 15:22:19 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,26 +17,23 @@ #include #include -static int open_semaphores(unsigned int count, sem_t *semaphores[sem_num]) +static int open_semaphores(unsigned int count, t_sems *semaphores) { - size_t i; - - semaphores[forks] = sem_open(FORKS_SEM, O_CREAT | O_EXCL, + 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, + semaphores->fed = sem_open(WELL_FED_SEM, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, count); - semaphores[death] = sem_open(DEATH_SEM, O_CREAT | O_EXCL, + semaphores->death = sem_open(DEATH_SEM, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, count); - semaphores[term] = sem_open(TERM_SEM, O_CREAT | O_EXCL, + semaphores->term = sem_open(TERM_SEM, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 1); - i = 0; - while (i < sem_num) - if (semaphores[i++] == SEM_FAILED) - return (1); + if (semaphores->forks == SEM_FAILED || semaphores->fed == SEM_FAILED + || semaphores->death == SEM_FAILED || semaphores->term == SEM_FAILED) + return (1); return (0); } -int init(unsigned int count, pid_t **philo_pids, sem_t *semaphores[sem_num]) +int init(unsigned int count, pid_t **philo_pids, t_sems *semaphores) { size_t i; @@ -49,21 +46,26 @@ int init(unsigned int count, pid_t **philo_pids, sem_t *semaphores[sem_num]) while (i < count) { (*philo_pids)[i] = 0; - sem_wait(semaphores[death]); - sem_wait(semaphores[fed]); + sem_wait(semaphores->death); + sem_wait(semaphores->fed); ++i; } - sem_wait(semaphores[term]); + sem_wait(semaphores->term); return (0); } -void cleanup(pid_t *philo_pids, sem_t *semaphores[sem_num]) +void close_semaphores(t_sems *semaphores) { - size_t i; + sem_close(semaphores->forks); + sem_close(semaphores->fed); + sem_close(semaphores->death); + sem_close(semaphores->term); + return ; +} - i = 0; - while (i < sem_num) - sem_close(semaphores[i++]); +void cleanup(pid_t *philo_pids, t_sems *semaphores) +{ + close_semaphores(semaphores); sem_unlink(FORKS_SEM); sem_unlink(WELL_FED_SEM); sem_unlink(DEATH_SEM); diff --git a/philo_bonus/philo.c b/philo_bonus/philo.c index 92211f9..c56ea19 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 14:08:23 by ljiriste ### ########.fr */ +/* Updated: 2024/05/24 15:22:54 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,19 +20,19 @@ static char *open_semaphores(t_philo *philo) { char *sem_name; - 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); - philo->semaphores[term] = sem_open(TERM_SEM, 0); + 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); + philo->semaphores.term = sem_open(TERM_SEM, 0); 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); if (philo->philo_sem == SEM_FAILED) { - sem_wait(philo->semaphores[term]); + sem_wait(philo->semaphores.term); printf("An error has occured.\n"); - sem_wait(philo->semaphores[death]); + sem_wait(philo->semaphores.death); free(sem_name); return (NULL); } @@ -47,9 +47,9 @@ static void philo_think(t_philo *philo) static void philo_eat(t_philo *philo) { - sem_wait(philo->semaphores[forks]); + sem_wait(philo->semaphores.forks); report(philo, "has taken a fork"); - sem_wait(philo->semaphores[forks]); + sem_wait(philo->semaphores.forks); report(philo, "has taken a fork"); sem_wait(philo->philo_sem); philo->last_eaten = usecs_since_start(philo->settings.start); @@ -58,10 +58,10 @@ static void philo_eat(t_philo *philo) report(philo, "is eating"); if (philo->settings.should_check_hunger && philo->num_eaten == philo->settings.min_eat_num) - sem_post(philo->semaphores[fed]); + sem_post(philo->semaphores.fed); usleep(philo->settings.time_to_eat); - sem_post(philo->semaphores[forks]); - sem_post(philo->semaphores[forks]); + sem_post(philo->semaphores.forks); + sem_post(philo->semaphores.forks); return ; } @@ -89,7 +89,7 @@ void be_a_philosopher(t_philo *philo) philo_sleep(philo); } destroy_hunger_watcher(hunger_watcher); - close_semaphores(philo); + close_semaphores(&philo->semaphores); sem_unlink(philo_sem_name); free(philo_sem_name); exit(0); diff --git a/philo_bonus/philo.h b/philo_bonus/philo.h index 1916549..b7d461c 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/24 14:08:05 by ljiriste ### ########.fr */ +/* Updated: 2024/05/24 15:23:21 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,15 +24,6 @@ # define DEATH_SEM "philosophers_death" # define TERM_SEM "philosophers_terminal" -typedef enum e_semaphores -{ - forks, - fed, - death, - term, - sem_num, -} t_my_sems; - // The .start member holds the result of gettimeofday // Every other member that holds time info counts // microseconds from that point (in other structures also) @@ -47,6 +38,14 @@ typedef struct s_settings struct timeval start; } t_settings; +typedef struct s_important_semaphores +{ + sem_t *forks; + sem_t *fed; + sem_t *death; + sem_t *term; +} t_sems; + // As mutex is not available in bonus, the idea is to have // a semaphore with value 1 to for the access to terminal. // The well_fed_sem(aphore) and death_sem(aphore) will be taken @@ -61,7 +60,7 @@ typedef struct s_philosopher unsigned int id; int alive; useconds_t last_eaten; - sem_t *semaphores[sem_num]; + t_sems semaphores; sem_t *philo_sem; t_settings settings; } t_philo; @@ -69,8 +68,9 @@ typedef struct s_philosopher int parse_input(t_settings *settings, int argc, char **argv); int init(unsigned int count, pid_t **philo_pids, - sem_t *semaphores[sem_num]); -void cleanup(pid_t *philo_pids, sem_t *semaphores[sem_num]); + t_sems *semaphores); +void cleanup(pid_t *philo_pids, t_sems *semaphores); +void close_semaphores(t_sems *semaphores); useconds_t usecs_since_start(struct timeval start); int is_alive(t_philo *philo); -- 2.30.2