This commit is contained in:
Raphael 2024-07-18 18:43:38 +02:00
parent 5215c0e3f4
commit d948c639ec
3 changed files with 14 additions and 8 deletions

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/10 00:23:09 by rparodi #+# #+# */ /* Created: 2024/06/10 00:23:09 by rparodi #+# #+# */
/* Updated: 2024/07/07 20:00:00 by rparodi ### ########.fr */ /* Updated: 2024/07/18 18:42:02 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,6 +14,10 @@
void ft_logs(t_str msg, t_philo *philo) void ft_logs(t_str msg, t_philo *philo)
{ {
pthread_mutex_lock(philo->dead_lock);
if (philo->dead_check == true)
return ((void) pthread_mutex_unlock(philo->dead_lock));
pthread_mutex_unlock(philo->dead_lock);
pthread_mutex_lock(philo->print_lock); pthread_mutex_lock(philo->print_lock);
const t_usize time = ft_time() - philo->start_time; const t_usize time = ft_time() - philo->start_time;
if (msg != NULL) if (msg != NULL)
@ -34,17 +38,17 @@ t_error ft_start_eating(t_philo *philo)
{ {
if (philo->id % 2 == 0) if (philo->id % 2 == 0)
{ {
pthread_mutex_lock(philo->l_fork);
ft_logs("has taken a fork\n", philo);
pthread_mutex_lock(philo->r_fork); pthread_mutex_lock(philo->r_fork);
ft_logs("has taken a fork\n", philo); ft_logs("has taken a fork\n", philo);
pthread_mutex_lock(philo->l_fork);
ft_logs("has taken a fork\n", philo);
} }
else else
{ {
pthread_mutex_lock(philo->r_fork);
ft_logs("has taken a fork\n", philo);
pthread_mutex_lock(philo->l_fork); pthread_mutex_lock(philo->l_fork);
ft_logs("has taken a fork\n", philo); ft_logs("has taken a fork\n", philo);
pthread_mutex_lock(philo->r_fork);
ft_logs("has taken a fork\n", philo);
} }
return (ft_ending_eating(philo)); return (ft_ending_eating(philo));
} }

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 11:42:37 by rparodi #+# #+# */ /* Created: 2024/06/04 11:42:37 by rparodi #+# #+# */
/* Updated: 2024/07/11 17:31:53 by rparodi ### ########.fr */ /* Updated: 2024/07/18 18:37:43 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,7 +23,7 @@ t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo)
philo->nb_eat = tmp; philo->nb_eat = tmp;
} }
else else
philo->t_eat = ~0; philo->nb_eat = -1;
ft_atou(argv[1], &tmp); ft_atou(argv[1], &tmp);
philo->nb_philo = tmp; philo->nb_philo = tmp;
ft_atou(argv[2], &tmp); ft_atou(argv[2], &tmp);

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 11:42:31 by rparodi #+# #+# */ /* Created: 2024/06/11 11:42:31 by rparodi #+# #+# */
/* Updated: 2024/07/11 18:10:49 by rparodi ### ########.fr */ /* Updated: 2024/07/18 18:38:04 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -48,6 +48,8 @@ void counting_launch(t_philo *philo, t_u8 *counter)
t_usize i; t_usize i;
i = 0; i = 0;
if (philo->nb_eat == -1)
return ;
while (i < philo[0].nb_philo) while (i < philo[0].nb_philo)
{ {
pthread_mutex_lock(philo[i].check_eating_count); pthread_mutex_lock(philo[i].check_eating_count);