From dc9b407880252a8e459013c1c3eea6fb7acbef1f Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Tue, 19 Mar 2024 14:14:35 +0100 Subject: [PATCH] Add handling of special case Handle the case where number_of_times_each_philosopher_must_eat is 0. --- philo/main.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/philo/main.c b/philo/main.c index 0b73acc..f24c5ca 100644 --- a/philo/main.c +++ b/philo/main.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 12:49:40 by ljiriste #+# #+# */ -/* Updated: 2024/03/19 13:39:27 by ljiriste ### ########.fr */ +/* Updated: 2024/03/19 14:14:15 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ #include #include +static const char *g_fed_str = "All philosophers are well fed"; // unsigned long would be more apropriate for min_eats_num // but for the ease of parsing I'm using size_t typedef struct s_settings @@ -253,7 +254,11 @@ int parse_input(int argc, char **argv, t_settings *set) res = res || parse_arg(argv[3], &set->time_to_eat); res = res || parse_arg(argv[4], &set->time_to_sleep); if (argc == 6) + { res = res || parse_args(argv[5], &set->min_eats_num); + if (set->min_eats_num == 0) + set->end = 1; + } else set->min_eats_num = 0; return (res); @@ -329,7 +334,7 @@ void check_death(t_philo *philos, t_settings *set) if (all_full) { pthread_mutex_lock(&set->terminal); - printf("%li All philosophers are well fed\n", (cur_time_sus() - set->program_start) / 1000); + printf("%li %s\n", (cur_time_sus() - set->program_start) / 1000, g_fed_str); set->end = 1; pthread_mutex_unlock(&set->terminal); return ; @@ -361,6 +366,11 @@ int main(int argc, char **argv) printf("Invalid input arguments.\n"); return (1); } + if (settings.end) + { + printf("0 %s\n", g_fed_str); + return (0); + } if (settings.philo_count == 1) return (handle_single_philo(&settings)); threads = malloc(sizeof(pthread_t) * settings.philo_count); -- 2.30.2