Implement checking for the death of philosopher
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 9 May 2024 08:27:02 +0000 (10:27 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 9 May 2024 08:27:02 +0000 (10:27 +0200)
Also minor refactoring

philo/Makefile
philo/helpers.c [moved from philo/philo_helpers.c with 68% similarity]
philo/main.c
philo/philo.c
philo/philo.h

index 2679ee868269bdb3c8407cd829b6687eca23e30e..3c4245cff8c7e7a0b98f60c7a872d7f3bfb8ced1 100644 (file)
@@ -8,7 +8,7 @@ INCDIR := .
 SRCDIR := .
 SRCS :=        main.c                          \
                philo.c                         \
-               philo_helpers.c         \
+               helpers.c                       \
                parsing.c                       \
                parsing_misc.c          \
                pars_arg.c                      \
similarity index 68%
rename from philo/philo_helpers.c
rename to philo/helpers.c
index 4e5a62512ae27e7901f414144747c79cfc2d591d..09b71c63886c3f6b9ae493a7dd05f076b88cc4be 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/09 09:13:40 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/09 09:40:43 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/09 10:16:19 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -24,18 +24,27 @@ int end(t_settings *set)
        return (res);
 }
 
-int    print_timestamp(t_philo *philo, const char *str)
+useconds_t     usecs_since_start(struct timeval start)
 {
        struct timeval  time;
-       useconds_t              since_start;
 
        gettimeofday(&time, NULL);
-       since_start = (time.tv_sec - philo->settings->start.tv_sec) * 1000000 + time.tv_usec - philo->settings->start.tv_usec;
+       return ((time.tv_sec - start.tv_sec) * 1000000 + time.tv_usec - start.tv_usec);
+}
+
+void   print_timestamp(t_philo *philo, const char *str)
+{
+       mutex_lock(&philo->settings->terminal_lock);
+       printf("%u %lu %s\n", usecs_since_start(philo->settings->start), philo->id, str);
+       mutex_unlock(&philo->settings->terminal_lock);
+       return ;
+}
+
+int    report(t_philo *philo, const char *str)
+{
        if (!end(philo->settings))
        {
-               mutex_lock(&philo->settings->terminal_lock);
-               printf("%u %lu %s\n", since_start, philo->id, str);
-               mutex_unlock(&philo->settings->terminal_lock);
+               print_timestamp(philo, str);
                return (0);
        }
        return (1);
index 0226beeba70919e865a5f8487daf5a45d1370b37..97ef11ba8cb728e33e25caa2ea6776819664b2ac 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/22 11:19:48 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/03/28 15:13:20 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/09 10:24:12 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -38,6 +38,39 @@ int  seat_philosophers(t_diner *diner, pthread_t *threads)
        return (0);
 }
 
+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);
+       }
+       mutex_unlock(&philo->philo_lock);
+       return (0);
+}
+
+void   watch_philosophers(t_diner *diner)
+{
+       size_t  i;
+
+       while (1)
+       {
+               i = 0;
+               while (i < diner->setting.philo_count)
+               {
+                       if (is_dead(diner->philos + i))
+                       {
+                               mutex_lock(&diner->setting.end_lock);
+                               diner->setting.end = 1;
+                               mutex_unlock(&diner->setting.end_lock);
+                               print_timestamp(diner->philos + i, "died");
+                               return ;
+                       }
+               }
+       }
+}
+
 int    main(int argc, char **argv)
 {
        t_diner         diner;
@@ -57,7 +90,7 @@ int   main(int argc, char **argv)
                cleanup(&diner, threads);
                return (3);
        }
-       report_end(watch_philosophers(&diner));
+       watch_philosophers(&diner);
        cleanup(&diner, threads);
        return (0);
 }
index ec61597a776b031766e5c77180bc1f068bfd5b47..6dfa0fb03458a8f0399e72ca77553e82c6d9fce6 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/09 08:45:21 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/09 09:53:45 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/09 10:25:46 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 
 static void    philo_think(t_philo *philo)
 {
-       print_timestamp(philo, "is thinking");
+       report(philo, "is thinking");
        return ;
 }
 
 static void    philo_eat(t_philo *philo)
 {
        mutex_lock(philo->forks[0]);
-       if (print_timestamp(philo, "has taken a fork"))
+       if (report(philo, "has taken a fork"))
        {
                mutex_unlock(philo->forks[0]);
                return ;
        }
        mutex_lock(philo->forks[1]);
-       print_timestamp(philo, "has taken a fork");
+       report(philo, "has taken a fork");
        mutex_lock(&philo->philo_lock);
-       last_eaten = usecs_since_start(philo->settings->start);
+       philo->last_eaten = usecs_since_start(philo->settings->start);
        ++philo->num_eaten;
        mutex_unlock(&philo->philo_lock);
-       if (!print_timestamp(philo, "is eating"))
+       if (!report(philo, "is eating"))
                usleep(philo->settings->time_to_eat);
        mutex_unlock(philo->forks[0]);
        mutex_unlock(philo->forks[1]);
@@ -42,7 +42,7 @@ static void   philo_eat(t_philo *philo)
 
 static void    philo_sleep(t_philo *philo)
 {
-       if (!print_timestamp(philo, "is sleeping"))
+       if (!report(philo, "is sleeping"))
                usleep(philo->settings->time_to_sleep);
        return ;
 }
index 62dd21e65ec03cf399502a74739123822801c291..b63b290253e644ed5c3ded62787c7754917040e5 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/22 11:10:17 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/09 09:38:31 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/09 10:25:30 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -70,12 +70,13 @@ int                 mutex_unlock(t_mutex *mutex);
 int                    all_mutexes_initialized(t_diner *diner);
 
 int                    end(t_settings *set);
-int                    print_timestamp(t_philo *philo, const char *str);
+useconds_t     usecs_since_start(struct timeval start);
+int                    report(t_philo *philo, const char *str);
+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           report_end(int end);
-int                    watch_philosophers(t_diner *diner);
+void           watch_philosophers(t_diner *diner);
 
 #endif //PHILO_H