From: Lukas Jiriste Date: Thu, 9 May 2024 10:38:31 +0000 (+0200) Subject: Add the check for well fed philosophers X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=574f9fd856578d92b7650ca744314306de411685;p=42%2Fphilosophers.git Add the check for well fed philosophers --- diff --git a/philo/main.c b/philo/main.c index 3f2151d..11c1ceb 100644 --- a/philo/main.c +++ b/philo/main.c @@ -6,15 +6,17 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/22 11:19:48 by ljiriste #+# #+# */ -/* Updated: 2024/05/09 11:58:04 by ljiriste ### ########.fr */ +/* Updated: 2024/05/09 12:31:31 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" #include #include +#include +#include -int seat_philosophers(t_diner *diner, pthread_t *threads) +static int seat_philosophers(t_diner *diner, pthread_t *threads) { size_t i; @@ -39,7 +41,7 @@ int seat_philosophers(t_diner *diner, pthread_t *threads) return (0); } -int is_dead(t_philo *philo) +static int is_dead(t_philo *philo) { mutex_lock(&philo->philo_lock); if (usecs_since_start(philo->settings->start) - philo->last_eaten @@ -52,7 +54,27 @@ int is_dead(t_philo *philo) return (0); } -void watch_philosophers(t_diner *diner) +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; @@ -67,10 +89,17 @@ void watch_philosophers(t_diner *diner) diner->setting.end = 1; mutex_unlock(&diner->setting.end_lock); print_timestamp(diner->philos + i, "died"); - return ; + return (death); } ++i; } + if (all_full(diner)) + { + mutex_lock(&diner->setting.end_lock); + diner->setting.end = 1; + mutex_unlock(&diner->setting.end_lock); + return (well_fed); + } } } @@ -78,6 +107,7 @@ int main(int argc, char **argv) { t_diner diner; pthread_t *threads; + enum e_end res; if (parse_input(&diner.setting, argc, argv)) { @@ -93,7 +123,9 @@ int main(int argc, char **argv) cleanup(&diner, threads); return (3); } - watch_philosophers(&diner); + res = watch_philosophers(&diner); cleanup(&diner, threads); + if (res == well_fed) + printf("All philosophers are well fed.\n"); return (0); } diff --git a/philo/parsing.c b/philo/parsing.c index 1c95776..ec9a39f 100644 --- a/philo/parsing.c +++ b/philo/parsing.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/26 09:31:35 by ljiriste #+# #+# */ -/* Updated: 2024/03/28 11:32:40 by ljiriste ### ########.fr */ +/* Updated: 2024/05/09 12:35:58 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,11 @@ #include #include +int is_sane(t_settings *settings) +{ + return (settings->philo_count > 0); +} + int parse_input(t_settings *settings, int argc, char **argv) { if (argc != 5 && argc != 6) @@ -34,5 +39,5 @@ int parse_input(t_settings *settings, int argc, char **argv) return (1); settings->should_check_hunger = 1; } - return (0); + return (!is_sane(settings)); } diff --git a/philo/philo.h b/philo/philo.h index b63b290..ad3ffc9 100644 --- a/philo/philo.h +++ b/philo/philo.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/22 11:10:17 by ljiriste #+# #+# */ -/* Updated: 2024/05/09 10:25:30 by ljiriste ### ########.fr */ +/* Updated: 2024/05/09 12:21:57 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,12 +43,12 @@ typedef struct s_settings typedef struct s_philosopher { - int num_eaten; - size_t id; - useconds_t last_eaten; - t_settings *settings; - t_mutex *forks[2]; - t_mutex philo_lock; + unsigned int num_eaten; + size_t id; + useconds_t last_eaten; + t_settings *settings; + t_mutex *forks[2]; + t_mutex philo_lock; } t_philo; typedef struct s_diner @@ -58,6 +58,12 @@ typedef struct s_diner t_mutex *forks; } t_diner; +enum e_end +{ + death, + well_fed, +}; + int parse_input(t_settings *settings, int argc, char **argv); int init(t_diner *diner, pthread_t **threads); @@ -76,7 +82,4 @@ void print_timestamp(t_philo *philo, const char *str); void *be_a_philosopher(void *philo); -int seat_philosophers(t_diner *diner, pthread_t *threads); -void watch_philosophers(t_diner *diner); - #endif //PHILO_H