From: Lukas Jiriste Date: Fri, 10 May 2024 09:43:11 +0000 (+0200) Subject: Implement seat_philosophers function X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=89266379c47428841f881c19fa01c321af38cd92;p=42%2Fphilosophers.git Implement seat_philosophers function The terminal semaphore is held to be able to exit without philosophers outputing in case of an error. Mae some minor changes, fix minor problems, remove unneeded (?) code. --- diff --git a/philo_bonus/main.c b/philo_bonus/main.c index e96b6a6..e2667f3 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/10 11:03:38 by ljiriste ### ########.fr */ +/* Updated: 2024/05/10 11:42:24 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,64 +16,29 @@ #include #include -static int seat_philosophers(t_diner *diner, pthread_t *threads) +static int seat_philosophers(t_settings *settings, pid_t *philo_pids) { - size_t i; + t_philo philo; - gettimeofday(&diner->setting.start, NULL); - i = 0; - while (i < diner->setting.philo_count) + philo.num_eaten = 0; + gettimeofday(&settings->start, NULL); + philo.settings = *settings; + philo.id = 1; + while (philo.id <= setting->philo_count) { - if (pthread_create(threads + i, NULL, - be_a_philosopher, diner->philos + i)) + philo_pids[philo.id - 1] = fork(); + if (philo_pids[philo.id - 1] == 0) { - mutex_lock(&diner->setting.end_lock); - diner->setting.end = 1; - mutex_unlock(&diner->setting.end_lock); - while (i > 0) - { - pthread_join(threads[--i], NULL); - } - return (1); + free(philo_pids); + be_a_philosopher(*philo); } - ++i; - } - return (0); -} - -static int is_dead(t_philo *philo) -{ - mutex_lock(&philo->philo_lock); - if (usecs_since_start(philo->settings->start) - philo->last_eaten - > philo->settings->time_to_die) - { - mutex_unlock(&philo->philo_lock); - return (1); + else if (philo_pids[philo.id - 1] < 0) + return (1); + ++philo.id; } - mutex_unlock(&philo->philo_lock); return (0); } -static int all_full(t_diner *diner) -{ - size_t i; - unsigned int min_eaten; - - if (!diner->setting.should_check_hunger) - return (0); - i = 0; - min_eaten = UINT_MAX; - while (i < diner->setting.philo_count) - { - mutex_lock(&diner->philos[i].philo_lock); - if (min_eaten > diner->philos[i].num_eaten) - min_eaten = diner->philos[i].num_eaten; - mutex_unlock(&diner->philos[i].philo_lock); - ++i; - } - return (min_eaten >= diner->setting.min_eat_num); -} - static enum e_end watch_philosophers(t_diner *diner) { size_t i; @@ -107,7 +72,7 @@ int main(int argc, char **argv) { t_settings settings; pid_t *philo_pids; - sem_t *semaphores[SEM_NUM]; + sem_t *semaphores[sem_num]; if (parse_input(&settings, argc, argv)) { @@ -118,11 +83,13 @@ int main(int argc, char **argv) cleanup(philo_pids, semaphores); return (2); } - if (seat_philosophers(settings, philo_pids)) + if (seat_philosophers(*settings, philo_pids)) { + kill_philosophers(setings.philo_count, philo_pids); cleanup(philo_pids, semaphores); return (3); } + 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 8e1a04e..0ade198 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/10 10:59:28 by ljiriste ### ########.fr */ +/* Updated: 2024/05/10 11:25:43 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,10 +46,12 @@ int init(unsigned int count, pid_t **philo_pids, sem_t *semaphores[sem_num]) i = 0; while (i < count) { + *philo_pids[i] = 0; sem_wait(semaphores[death]); sem_wait(semaphores[fed]); ++i; } + sem_wait(semaphores[term]); return (0); }