/* 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 */
/* */
/* ************************************************************************** */
void kill_philosophers(t_sems *semaphores)
{
+ sem_wait(semaphores->term);
sem_post(semaphores->death);
return ;
}
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);
}
}
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);
}
/* 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 */
/* */
/* ************************************************************************** */
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);
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))