From a16de8c90699e2f82d839bf2b0529efa7da2d2af Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 30 May 2024 13:19:35 +0200 Subject: [PATCH] Get rid of philo_pids array as it is not needed --- philo_bonus/main.c | 35 +++++++++++++++-------------------- philo_bonus/mem_management.c | 14 ++++---------- philo_bonus/philo.h | 7 +++---- 3 files changed, 22 insertions(+), 34 deletions(-) diff --git a/philo_bonus/main.c b/philo_bonus/main.c index 2bf9e65..f9976ab 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:54:56 by ljiriste ### ########.fr */ +/* Updated: 2024/05/30 13:16:05 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,9 +19,10 @@ #include #include -static int seat_philosophers(t_settings *settings, pid_t *philo_pids, t_sems *semaphores) +static int seat_philosophers(t_settings *settings, t_sems *semaphores) { t_philo philo; + pid_t pid; philo.num_eaten = 0; gettimeofday(&settings->start, NULL); @@ -31,28 +32,23 @@ static int seat_philosophers(t_settings *settings, pid_t *philo_pids, t_sems *se philo.last_eaten = 0; while (philo.id <= settings->philo_count) { - philo_pids[philo.id - 1] = fork(); - if (philo_pids[philo.id - 1] == 0) + pid = fork(); + if (pid == 0) { close_semaphores(semaphores); - free(philo_pids); be_a_philosopher(&philo); } - else if (philo_pids[philo.id - 1] < 0) + else if (pid < 0) return (1); ++philo.id; } return (0); } -void kill_philosophers(unsigned int count, pid_t *philo_pids) +void kill_philosophers(t_sems *semaphores) { - while (count > 0) - { - --count; - if (philo_pids[count] != 0) - kill(philo_pids[count], SIGTERM); - } + sem_post(semaphores->death); + return ; } void *check_fed(void *input) @@ -112,26 +108,25 @@ void wait_for_end(unsigned int philo_count, t_sems *semaphores) int main(int argc, char **argv) { t_settings settings; - pid_t *philo_pids; t_sems semaphores; if (parse_input(&settings, argc, argv)) { return (1); } - if (init(settings.philo_count, &philo_pids, &semaphores)) + if (init(settings.philo_count, &semaphores)) { - cleanup(philo_pids, &semaphores); + cleanup(&semaphores); return (2); } - if (seat_philosophers(&settings, philo_pids, &semaphores)) + if (seat_philosophers(&settings, &semaphores)) { - kill_philosophers(settings.philo_count, philo_pids); - cleanup(philo_pids, &semaphores); + kill_philosophers(&semaphores); + cleanup(&semaphores); return (3); } sem_post(semaphores.term); wait_for_end(settings.philo_count, &semaphores); - cleanup(philo_pids, &semaphores); + cleanup(&semaphores); return (0); } diff --git a/philo_bonus/mem_management.c b/philo_bonus/mem_management.c index 76fc540..5f004b1 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:01:59 by ljiriste ### ########.fr */ +/* Updated: 2024/05/30 13:17:33 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,14 +41,9 @@ static int open_semaphores(unsigned int count, t_sems *semaphores) return (0); } -int init(unsigned int count, pid_t **philo_pids, t_sems *semaphores) +int init(unsigned int count, t_sems *semaphores) { - *philo_pids = malloc(count * sizeof(**philo_pids)); - if (!*philo_pids) - return (1); - if (open_semaphores(count, semaphores)) - return (1); - return (0); + return (open_semaphores(count, semaphores)); } void close_semaphores(t_sems *semaphores) @@ -68,7 +63,7 @@ void close_semaphores(t_sems *semaphores) return ; } -void cleanup(pid_t *philo_pids, t_sems *semaphores) +void cleanup(t_sems *semaphores) { close_semaphores(semaphores); sem_unlink(FORKS_SEM); @@ -77,6 +72,5 @@ void cleanup(pid_t *philo_pids, t_sems *semaphores) sem_unlink(TERM_SEM); sem_unlink(END_SEM); sem_unlink(CHECK_END_SEM); - free(philo_pids); return ; } diff --git a/philo_bonus/philo.h b/philo_bonus/philo.h index 4602019..2f6cf26 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 12:18:20 by ljiriste ### ########.fr */ +/* Updated: 2024/05/30 13:15:08 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -79,9 +79,8 @@ typedef struct s_check int parse_input(t_settings *settings, int argc, char **argv); -int init(unsigned int count, pid_t **philo_pids, - t_sems *semaphores); -void cleanup(pid_t *philo_pids, t_sems *semaphores); +int init(unsigned int count, t_sems *semaphores); +void cleanup(t_sems *semaphores); void close_semaphores(t_sems *semaphores); useconds_t usecs_since_start(struct timeval start); -- 2.30.2