Add handling of special case
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 19 Mar 2024 13:14:35 +0000 (14:14 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 22 Mar 2024 10:01:34 +0000 (11:01 +0100)
Handle the case where number_of_times_each_philosopher_must_eat is 0.

philo/main.c

index 0b73accff4e430237212a84dd0e9a6ea060e1d29..f24c5caa642c26367235b4edcb2cc3029b165a17 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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 <stdio.h>
 #include <unistd.h>
 
+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);