diff --git a/philo/init_fork.c b/philo/init_fork.c index da5295a..51d00ff 100644 --- a/philo/init_fork.c +++ b/philo/init_fork.c @@ -6,22 +6,24 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/22 15:29:08 by adjoly #+# #+# */ -/* Updated: 2024/07/25 19:49:35 by adjoly ### ########.fr */ +/* Updated: 2024/07/26 15:24:07 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" -t_init *init_fork(t_init *init, uint16_t philo_nbr) +void init_fork(t_pdata data) { uint16_t i; + t_init init[PHILO_MAX]; + i = 0; - while (i < philo_nbr) + while (i < data.philo_nbr) { pthread_mutex_init(&(init[i].data.fork.left), NULL); - init[i].data.fork.right = &init[(i - 1) % philo_nbr].data.fork.left; + init[i].data.fork.right = &init[(i - 1) % data.philo_nbr].data.fork.left; i++; } - return (init); + init_philo(data, init); } diff --git a/philo/init_philo.c b/philo/init_philo.c index 0e7ad29..4b9168a 100644 --- a/philo/init_philo.c +++ b/philo/init_philo.c @@ -6,16 +6,15 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/11 14:36:59 by adjoly #+# #+# */ -/* Updated: 2024/07/25 19:30:28 by adjoly ### ########.fr */ +/* Updated: 2024/07/26 15:25:58 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" #include -void init_philo(t_pdata data) +void init_philo(t_pdata data, t_init *init) { - t_init *init = malloc((sizeof(t_init) + 1) * data.philo_nbr); int r; pthread_mutex_t check; uint16_t i; @@ -23,7 +22,6 @@ void init_philo(t_pdata data) i = 0; pthread_mutex_init(&check, NULL); get_death(false, false); - init_fork(init, data.philo_nbr); while (i < data.philo_nbr) { init[i].data.id = i + 1; diff --git a/philo/philo.c b/philo/philo.c index 268ac04..c1c248b 100644 --- a/philo/philo.c +++ b/philo/philo.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/07 15:10:29 by adjoly #+# #+# */ -/* Updated: 2024/07/25 16:24:01 by adjoly ### ########.fr */ +/* Updated: 2024/07/26 15:23:45 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,6 @@ int main(int ac, char **av) data = philo_parse(av, ac); if (data.error == true) return (EXIT_FAILURE); - init_philo(data); + init_fork(data); return (EXIT_SUCCESS); } diff --git a/philo/philo.h b/philo/philo.h index ab98c6f..b8dc448 100644 --- a/philo/philo.h +++ b/philo/philo.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/07 15:11:02 by adjoly #+# #+# */ -/* Updated: 2024/07/25 17:06:09 by adjoly ### ########.fr */ +/* Updated: 2024/07/26 15:25:57 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -78,11 +78,11 @@ bool get_death(bool in, bool ret); * Main path * by order of call */ -void init_philo(t_pdata data); -t_init *init_fork(t_init *init, uint16_t philo_nbr); -void start_philo(t_init *init, uint16_t philo_nbr); +void init_fork(t_pdata data); +void init_philo(t_pdata data, t_init *init); +void start_philo(t_init *init, uint16_t philo_nbr); -void *philo_routine(void *content); +void *philo_routine(void *content); /** * For debug purpose to be REMOVED diff --git a/philo/philo_routine.c b/philo/philo_routine.c index 815ecef..5750283 100644 --- a/philo/philo_routine.c +++ b/philo/philo_routine.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/22 21:24:53 by adjoly #+# #+# */ -/* Updated: 2024/07/25 20:06:20 by adjoly ### ########.fr */ +/* Updated: 2024/07/26 15:15:13 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,26 +36,18 @@ void *philo_routine(void *content) gettimeofday(&(philo.t0), NULL); while (i < philo.data.meal_nbr) { - log_philo(philo); - take_fork(&philo.fork, philo.id); philo.state = FORK_TAKEN; + take_fork(&philo.fork, philo.id); log_philo(philo); take_fork(&philo.fork, philo.id + 1); log_philo(philo); philo.state = EAT; log_philo(philo); death = sleep_phil(philo.data.eat_time, philo.check); - pthread_mutex_unlock(&philo.fork.left); + if (death == true) + return (NULL); pthread_mutex_unlock(philo.fork.right); - if (death == true) - return (NULL); - philo.state = SLEEP; - log_philo(philo); - death = sleep_phil(philo.data.sleep_time, philo.check); - if (death == true) - return (NULL); - philo.state = THINK; - log_philo(philo); + pthread_mutex_unlock(&philo.fork.left); i++; } return (NULL);