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.
/* 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 */
/* */
/* ************************************************************************** */
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);
}
/* 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 */
/* */
/* ************************************************************************** */
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 ;
}
/* 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 */
/* */
/* ************************************************************************** */
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);