Implement seat_philosophers
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 28 Mar 2024 13:56:54 +0000 (14:56 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 28 Mar 2024 14:15:02 +0000 (15:15 +0100)
Thanks to the change of how the program keeps track of time
it is possible to set the "last_eaten" member of philo in advance
(to zero - the start) without slowing the seating.

Thanks to this, no delay is necessary before the program checks whether
philos died of hunger. (It was needed in old branch, because
philosophers initialized their own time at the start of their
existance, and the check could happen before the time was set)

philo/main.c
philo/mutex.c
philo/philo.h

index c4a9e7ceea43b2912c01af5481200b8174498e27..0226beeba70919e865a5f8487daf5a45d1370b37 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/22 11:19:48 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/03/28 10:00:52 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/03/28 15:13:20 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include <stddef.h>
 #include <pthread.h>
 
+int    seat_philosophers(t_diner *diner, pthread_t *threads)
+{
+       size_t  i;
+
+       gettimeofday(&diner->setting.start, NULL);
+       i = 0;
+       while (i < diner->setting.philo_count)
+       {
+               if (pthread_create(threads + i, NULL, be_a_philosopher, diner->philos + i))
+               {
+                       mutex_lock(&diner->setting.end_lock);
+                       diner->setting.end = 1;
+                       mutex_unlock(&diner->setting.end_lock);
+                       while (i > 0)
+                       {
+                               pthread_join(threads[--i], NULL);
+                       }
+                       return (1);
+               }
+               ++i;
+       }
+       return (0);
+}
+
 int    main(int argc, char **argv)
 {
        t_diner         diner;
@@ -28,8 +52,11 @@ int  main(int argc, char **argv)
                cleanup(&diner, threads);
                return (2);
        }
-       seat_philosophers(&diner, threads);
-       wait_till_death_possible(diner.setting.time_to_die, diner.setting.start);
+       if (seat_philosophers(&diner, threads))
+       {
+               cleanup(&diner, threads);
+               return (3);
+       }
        report_end(watch_philosophers(&diner));
        cleanup(&diner, threads);
        return (0);
index 54ae20b501aaa0b9f87225ccffcbaaf9ecd23355..6073e71f723b92f3dffc990e5617ef5d75033ce4 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/28 10:35:16 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/03/28 10:49:35 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/03/28 15:12:37 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -26,6 +26,20 @@ void destroy_mutex(t_mutex *mutex)
        mutex->is_init = 0;
 }
 
+int    mutex_lock(t_mutex *mutex)
+{
+       if (!mutex->is_init)
+               return (1);
+       return (pthread_mutex_lock(&mutex->mutex));
+}
+
+int    mutex_unlock(t_mutex *mutex)
+{
+       if (!mutex->is_init)
+               return (1);
+       return (pthread_mutex_unlock(&mutex->mutex));
+}
+
 int    all_mutexes_initialized(t_diner *diner)
 {
        int             res;
index 8076801b1ea98f528d2896ecac9c847327e60f82..f3f27762c1b8f2ca26ea14786282e047b1eea684 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/22 11:10:17 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/03/28 11:27:15 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/03/28 15:14:17 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -65,11 +65,13 @@ void                cleanup(t_diner *diner, pthread_t *threads);
 
 int                    init_mutex(t_mutex *mutex, pthread_mutexattr_t *attr);
 void           destroy_mutex(t_mutex *mutex);
+int                    mutex_lock(t_mutex *mutex);
+int                    mutex_unlock(t_mutex *mutex);
 int                    all_mutexes_initialized(t_diner *diner);
 
+void           *be_a_philosopher(void *philo);
+
 int                    seat_philosophers(t_diner *diner, pthread_t *threads);
-void           wait_till_death_possible(useconds_t time_to_die,
-                               struct timeval start);
 void           report_end(int end);
 int                    watch_philosophers(t_diner *diner);