Implement the philo side of the protocol
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 30 May 2024 09:45:43 +0000 (11:45 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 30 May 2024 09:45:43 +0000 (11:45 +0200)
The protocol is drafted in previous commit (commit 6980661).
The program now produces the correct output but needs to be checked
for bugs and races.

The hunger_watcher.c is renamed due to it containing more than
just the hunger watcher.

philo_bonus/Makefile
philo_bonus/main.c
philo_bonus/philo.c
philo_bonus/philo.h
philo_bonus/watchers.c [moved from philo_bonus/hunger_watcher.c with 66% similarity]

index ebd147c77289a7ebf3b8efb069b881145fb28e2b..4f85edbe02eb9696b5986cfbf9df43f2fad1d9aa 100644 (file)
@@ -14,7 +14,7 @@ SRCS :=       main.c                          \
                pars_arg.c                      \
                inner_pars_arg.c        \
                mem_management.c        \
-               hunger_watcher.c        \
+               watchers.c                      \
                libft_helpers.c         \
 
 SRCS := $(addprefix $(SRCDIR)/, $(SRCS))
index 41b985cc652ac0e87abe9102a41c42bb340525db..3b7687fa2970a83455ac1b10688e4e12a8d5fec0 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/22 11:19:48 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/30 10:15:37 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/30 11:41:40 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -91,6 +91,7 @@ void  wait_for_end(unsigned int philo_count, t_sems *semaphores)
        check_input.semaphores = semaphores;
        pthread_create(&thread_fed, NULL, check_fed, &check_input);
        sem_wait(semaphores->death);
+       sem_post(semaphores->death);
        sem_wait(semaphores->check_end);
        end = 1;
        sem_post(semaphores->check_end);
index c56ea195ef7c72c84fa02a0eea60ebd9e459a7f5..40bc31022e6a34f80c052b3d5309939bbc068be1 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/09 08:45:21 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/24 15:22:54 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/30 11:40:22 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -24,6 +24,7 @@ static char   *open_semaphores(t_philo *philo)
        philo->semaphores.fed = sem_open(WELL_FED_SEM, 0);
        philo->semaphores.death = sem_open(DEATH_SEM, 0);
        philo->semaphores.term = sem_open(TERM_SEM, 0);
+       philo->semaphores.end = sem_open(END_SEM, 0);
        sem_name = create_philo_sem_name(philo->id);
        if (!sem_name)
                return (NULL);
@@ -74,13 +75,13 @@ static void philo_sleep(t_philo *philo)
 
 void   be_a_philosopher(t_philo *philo)
 {
-       pthread_t       hunger_watcher;
+       pthread_t       watchers[2];
        char            *philo_sem_name;
 
        philo_sem_name = open_semaphores(philo);
        if (!philo_sem_name)
                exit(2);
-       if (create_hunger_watcher(philo, &hunger_watcher))
+       if (create_watchers(philo, watchers))
                exit(3);
        while (is_alive(philo))
        {
@@ -88,7 +89,7 @@ void  be_a_philosopher(t_philo *philo)
                philo_eat(philo);
                philo_sleep(philo);
        }
-       destroy_hunger_watcher(hunger_watcher);
+       destroy_watchers(watchers);
        close_semaphores(&philo->semaphores);
        sem_unlink(philo_sem_name);
        free(philo_sem_name);
index 0895457c13fbfc82216f092f95c9fcc4bffe637d..8e311ae101cd23f8821d718e6bda5cffc86fab54 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/22 11:10:17 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/30 10:10:11 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/30 10:40:40 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -89,8 +89,8 @@ void          report(t_philo *philo, const char *str);
 void           print_timestamp(t_philo *philo, const char *str);
 char           *create_philo_sem_name(unsigned int id);
 
-int                    create_hunger_watcher(t_philo *philo, pthread_t *hunger_watcher);
-void           destroy_hunger_watcher(pthread_t hunger_watcher);
+int                    create_watchers(t_philo *philo, pthread_t watchers[2]);
+void           destroy_watchers(pthread_t watchers[2]);
 
 size_t         ft_strlen(const char *str);
 char           *ft_uitoa_base(uintmax_t n, const char *base);
similarity index 66%
rename from philo_bonus/hunger_watcher.c
rename to philo_bonus/watchers.c
index 3dbe35fdfa55b1333ffc2d16519727f70deaa2bd..817649929b1ceaaf0fd83ea794993f5ffd7325db 100644 (file)
@@ -1,12 +1,12 @@
 /* ************************************************************************** */
 /*                                                                            */
 /*                                                        :::      ::::::::   */
-/*   hunger_watcher.c                                   :+:      :+:    :+:   */
+/*   watchers.c                                         :+:      :+:    :+:   */
 /*                                                    +:+ +:+         +:+     */
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/24 08:49:46 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/30 10:00:31 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/30 11:39:51 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -38,6 +38,11 @@ static void  *watch_for_starvation(void *philo_void)
        time = time_till_death(philo);
        while (time > 0)
        {
+               if (!philo->alive)
+               {
+                       sem_post(philo->philo_sem);
+                       return (NULL);
+               }
                sem_post(philo->philo_sem);
                usleep(time / 2);
                sem_wait(philo->philo_sem);
@@ -51,13 +56,38 @@ static void *watch_for_starvation(void *philo_void)
        return (NULL);
 }
 
-int    create_hunger_watcher(t_philo *philo, pthread_t *hunger_watcher)
+void   *watch_for_end(void *philo_void)
 {
-       return (pthread_create(hunger_watcher, NULL, watch_for_starvation, philo));
+       t_philo *philo;
+
+       philo = (t_philo *)philo_void;
+       sem_wait(philo->semaphores.death);
+       sem_post(philo->semaphores.death);
+       sem_wait(philo->philo_sem);
+       philo->alive = 0;
+       sem_post(philo->philo_sem);
+       sem_post(philo->semaphores.end);
+       return (NULL);
+}
+
+int    create_watchers(t_philo *philo, pthread_t watchers[2])
+{
+       if (pthread_create(&watchers[0], NULL, watch_for_starvation, philo))
+               return (1);
+       if (pthread_create(&watchers[1], NULL, watch_for_end, philo))
+       {
+               sem_wait(philo->philo_sem);
+               philo->alive = 0;
+               sem_post(philo->philo_sem);
+               pthread_join(watchers[0], NULL);
+               return (1);
+       }
+       return (0);
 }
 
-void   destroy_hunger_watcher(pthread_t hunger_watcher)
+void   destroy_watchers(pthread_t watchers[2])
 {
-       pthread_join(hunger_watcher, NULL);
+       pthread_join(watchers[0], NULL);
+       pthread_join(watchers[1], NULL);
        return ;
 }