Improve error messages and a minor change
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 30 May 2024 13:00:14 +0000 (15:00 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 30 May 2024 13:00:14 +0000 (15:00 +0200)
philo_bonus/main.c
philo_bonus/philo.c

index 9b6f87d469c6ee9b111de21336c94e0f53f3deff..f2a7044fc23346623a7316c09108931afce27bed 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/22 11:19:48 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/30 13:31:16 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/30 14:56:37 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -47,6 +47,7 @@ static int    seat_philosophers(t_settings *settings, t_sems *semaphores)
 
 void   kill_philosophers(t_sems *semaphores)
 {
+       sem_wait(semaphores->term);
        sem_post(semaphores->death);
        return ;
 }
@@ -69,11 +70,11 @@ void        *check_fed(void *input)
        if (!*end)
        {
                sem_wait(semaphores->term);
+               sem_post(semaphores->death);
                printf("All philosophers are well fed\n");
                *end = 1;
        }
        sem_post(semaphores->check_end);
-       sem_post(semaphores->death);
        return (NULL);
 }
 
@@ -120,14 +121,14 @@ int       main(int argc, char **argv)
        }
        if (init(settings.philo_count, &semaphores))
        {
-               printf("An error has occured.\n");
+               printf("An initialization error has occured.\n");
                cleanup(&semaphores);
                return (2);
        }
        if (seat_philosophers(&settings, &semaphores))
        {
                kill_philosophers(&semaphores);
-               printf("An error has occured.\n");
+               printf("A forking error has occured.\n");
                cleanup(&semaphores);
                return (3);
        }
index 63455c8aee59f14e2c789d232221269663edb93c..a8b62f8b46efe9d958b3a907e98b0c7012eef280 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/09 08:45:21 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/30 13:31:16 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/30 14:43:45 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -87,7 +87,12 @@ static void  clear_philo(t_philo *philo)
 static void    error_exit(t_philo *philo, int exit_code)
 {
        sem_wait(philo->semaphores.term);
-       printf("An error has occured.\n");
+       if (exit_code == 1)
+               printf("Philosopher %u has encountered an error creating the name of a semaphore.\n", philo->id);
+       else if (exit_code == 2)
+               printf("Philosopher %u has encountered an error opening a semaphore.\n", philo->id);
+       else if (exit_code == 3)
+               printf("Philosopher %u has encountered an error creating a watcher.\n", philo->id);
        sem_post(philo->semaphores.death);
        sem_post(philo->semaphores.end);
        clear_philo(philo);
@@ -96,10 +101,12 @@ static void        error_exit(t_philo *philo, int exit_code)
 
 void   be_a_philosopher(t_philo *philo)
 {
+       int                     res;
        pthread_t       watchers[2];
 
-       if (open_semaphores(philo))
-               error_exit(philo, 2);
+       res = open_semaphores(philo);
+       if (res)
+               error_exit(philo, res);
        if (create_watchers(philo, watchers))
                error_exit(philo, 3);
        while (is_alive(philo))