Add the check for well fed philosophers
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 9 May 2024 10:38:31 +0000 (12:38 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 9 May 2024 10:38:31 +0000 (12:38 +0200)
philo/main.c
philo/parsing.c
philo/philo.h

index 3f2151d46f6bd02b1775ca9fa4475b143f7cf8b0..11c1ceb3e50b80546eceb68ba0babcb81ae2f9c7 100644 (file)
@@ -6,15 +6,17 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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 <stddef.h>
 #include <pthread.h>
+#include <limits.h>
+#include <stdio.h>
 
-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);
 }
index 1c95776ee441c7d7ebc511a93bb287ca8dfb6922..ec9a39f259b8a3524300668d382b3f2ba363518f 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include <stddef.h>
 #include <stdlib.h>
 
+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));
 }
index b63b290253e644ed5c3ded62787c7754917040e5..ad3ffc9bf6c8ad1b275582521366e5ad2c0859a1 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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