diff --git a/philo/Makefile b/philo/Makefile index 64f3128..e669a45 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2024/06/16 12:23:39 by rparodi ### ########.fr # +# Updated: 2024/06/26 17:25:08 by rparodi ### ########.fr # # # # **************************************************************************** # @@ -120,3 +120,4 @@ footer: .PHONY: all bonus clean fclean re -include ${OBJ:.o=.d} + diff --git a/philo/includes/philo.h b/philo/includes/philo.h index 4081e98..073f9b6 100644 --- a/philo/includes/philo.h +++ b/philo/includes/philo.h @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 11:34:04 by rparodi #+# #+# */ -/* Updated: 2024/06/17 14:12:53 by rparodi ### ########.fr */ +/* Updated: 2024/06/26 17:15:16 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,10 +36,6 @@ # define MAXSUPPORT 200 # endif -# ifndef EATTIME -# define EATTIME 3 -# endif - typedef struct s_philo { t_usize nb_philo; @@ -54,6 +50,7 @@ typedef struct s_philo t_usize t_last_eat; t_usize start_time; t_i32 *dead_check; + t_mutex *check_eating_count; t_mutex *r_fork; t_mutex *l_fork; t_mutex *print_lock; @@ -67,6 +64,7 @@ typedef struct s_program t_mutex dead_lock; t_mutex meal_lock; t_mutex print_lock; + t_mutex check_eating_count; t_philo *philos; } t_program; diff --git a/philo/sources/ft_actions.c b/philo/sources/ft_actions.c index 187491d..7598b83 100644 --- a/philo/sources/ft_actions.c +++ b/philo/sources/ft_actions.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/10 00:23:09 by rparodi #+# #+# */ -/* Updated: 2024/06/18 15:50:49 by rparodi ### ########.fr */ +/* Updated: 2024/06/26 17:24:47 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,6 +30,7 @@ t_error ft_start_eating(t_philo *philo) { const t_usize time = ft_time() - philo->start_time; + printf("\n\nOn as au pheno ?\n\n"); if (philo->id % 2 == 0) { pthread_mutex_lock(philo->l_fork); @@ -47,8 +48,10 @@ t_error ft_start_eating(t_philo *philo) philo->eating = true; ft_logs("is eating\n", philo); philo->t_last_eat = time; + pthread_mutex_lock(philo->check_eating_count); philo->eating_count++; - ft_pause(EATTIME * 1000); + pthread_mutex_unlock(philo->check_eating_count); + ft_pause(philo->t_eat); pthread_mutex_unlock(philo->r_fork); pthread_mutex_unlock(philo->l_fork); philo->eating = false; diff --git a/philo/sources/parsing_args.c b/philo/sources/parsing_args.c index 496df83..d38a553 100644 --- a/philo/sources/parsing_args.c +++ b/philo/sources/parsing_args.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 11:42:37 by rparodi #+# #+# */ -/* Updated: 2024/06/18 15:09:06 by rparodi ### ########.fr */ +/* Updated: 2024/06/26 17:16:09 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -81,6 +81,7 @@ t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks) prog->philos[i].dead_lock = &prog->dead_lock; prog->philos[i].meal_lock = &prog->meal_lock; prog->philos[i].dead_check = &prog->dead_flag; + prog->philos[i].check_eating_count = &prog->check_eating_count; prog->philos[i].l_fork = &forks[i]; if (i == 0) prog->philos[i].r_fork = &forks[prog->philos[i].nb_philo - 1]; @@ -100,8 +101,9 @@ t_error ft_init(t_i32 argc, t_str *argv, t_program *prog, t_philo *philo) pthread_mutex_init(&prog->print_lock, NULL); pthread_mutex_init(&prog->dead_lock, NULL); pthread_mutex_init(&prog->meal_lock, NULL); + pthread_mutex_init(&prog->check_eating_count, NULL); prog->philos = philo; - prog->dead_flag = 0; + prog->dead_flag = false; if (ft_init_philo(argc, argv, prog, forks)) return (ERROR); if (philo->nb_philo != 0 && philo->nb_philo > MAXSUPPORT) diff --git a/philo/sources/routine.c b/philo/sources/routine.c index 689f46f..a5af511 100644 --- a/philo/sources/routine.c +++ b/philo/sources/routine.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/11 11:42:31 by rparodi #+# #+# */ -/* Updated: 2024/06/20 15:04:13 by rparodi ### ########.fr */ +/* Updated: 2024/06/26 17:23:11 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,8 +30,8 @@ t_bool check_dead(t_philo *philo) { if (ft_time() - philo[i].t_last_eat > philo[i].t_eat) return (true); + i++; } - i++; return (false); } @@ -46,8 +46,11 @@ t_bool check_eat(t_philo *philo) return (false); while (i < philo[0].nb_philo) { + pthread_mutex_lock(philo[i].check_eating_count); + printf("\n\n\n eating_time for philo n%d = %d\n\n\n", philo[i].id, philo[i].eating_count); if (philo[i].eating_count >= philo[i].nb_eat) check++; + pthread_mutex_unlock(philo[i].check_eating_count); i++; } if (check == philo[0].nb_philo) @@ -64,9 +67,9 @@ void *ft_watch_dogs(void *ptr) return (NULL); while (true) { - if (check_dead(philo)) + if (check_dead(philo) == true) break ; - if (check_eat(philo)) + if (check_eat(philo) == true) break ; } return (philo); @@ -94,6 +97,7 @@ t_error ft_init_thread(t_program *prog, t_mutex *forks) t_usize i; i = 0; + ft_logs("garfi lpb\n", prog->philos); if (pthread_create(&o_block, NULL, &ft_watch_dogs, prog->philos)) ft_destroy_exit("Allocation of watch_dogs failed\n", \ ERROR, prog, forks);