Add and fix error messaging, stop removal of files
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 30 May 2024 11:35:30 +0000 (13:35 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 30 May 2024 11:35:30 +0000 (13:35 +0200)
The philo program checks whether it created the semaphores.
When not, it assumes another instance is running and errors out.
Until this commit it would erase the semaphore files nontheless
because the unlinking was not conditional.

philo_bonus/main.c
philo_bonus/mem_management.c
philo_bonus/philo.c

index f9976ab917ae25c903e5b28293ac7dc932f44a9a..9b6f87d469c6ee9b111de21336c94e0f53f3deff 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:16:05 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/30 13:31:16 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -112,16 +112,22 @@ int       main(int argc, char **argv)
 
        if (parse_input(&settings, argc, argv))
        {
+               printf("The input should be in form:\n"
+                               "\t%s number_of_philosophers time_to_die time_to_eat"
+                               "time_to_sleep [number_of_times_each_philosopher_must_eat]\n",
+                               argv[0]);
                return (1);
        }
        if (init(settings.philo_count, &semaphores))
        {
+               printf("An error has occured.\n");
                cleanup(&semaphores);
                return (2);
        }
        if (seat_philosophers(&settings, &semaphores))
        {
                kill_philosophers(&semaphores);
+               printf("An error has occured.\n");
                cleanup(&semaphores);
                return (3);
        }
index 5f004b1747a25aaa67959ead6d016c47eaa56541..4f12de904a43157df7950dd9ff3648cb9ae7367f 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/28 09:39:55 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/30 13:17:33 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/30 13:25:28 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -65,12 +65,18 @@ void        close_semaphores(t_sems *semaphores)
 
 void   cleanup(t_sems *semaphores)
 {
+       if (semaphores->forks != SEM_FAILED)
+               sem_unlink(FORKS_SEM);
+       if (semaphores->fed != SEM_FAILED)
+               sem_unlink(WELL_FED_SEM);
+       if (semaphores->death != SEM_FAILED)
+               sem_unlink(DEATH_SEM);
+       if (semaphores->term != SEM_FAILED)
+               sem_unlink(TERM_SEM);
+       if (semaphores->end != SEM_FAILED)
+               sem_unlink(END_SEM);
+       if (semaphores->check_end != SEM_FAILED)
+               sem_unlink(CHECK_END_SEM);
        close_semaphores(semaphores);
-       sem_unlink(FORKS_SEM);
-       sem_unlink(WELL_FED_SEM);
-       sem_unlink(DEATH_SEM);
-       sem_unlink(TERM_SEM);
-       sem_unlink(END_SEM);
-       sem_unlink(CHECK_END_SEM);
        return ;
 }
index 1e14c137743fa51ac547f6e4d3177ec138ec01f8..63455c8aee59f14e2c789d232221269663edb93c 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:00:26 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/05/30 13:31:16 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -87,7 +87,7 @@ 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 as occured.\n");
+       printf("An error has occured.\n");
        sem_post(philo->semaphores.death);
        sem_post(philo->semaphores.end);
        clear_philo(philo);