From 074ce180b725a6da873a7ad02204782c235d2f97 Mon Sep 17 00:00:00 2001 From: adjoly Date: Tue, 6 Aug 2024 17:42:36 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=20testing=20things,=20might=20broke.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- philo/check_death.c | 4 +-- philo/eat.c | 31 ++++++++++++++++++ philo/{start_philo.c => end_philo.c} | 8 ++--- philo/get_time_in_ms.c | 4 +-- philo/init_fork.c | 14 ++++----- philo/init_philo.c | 22 +++++++------ philo/note | 15 --------- philo/parsing.c | 2 +- philo/philo.c | 2 +- philo/philo.h | 30 ++++++++++-------- philo/philo_routine.c | 47 ++++++++++++++-------------- philo/sleep.c | 41 +++++++++++++++--------- philo/utils.c | 21 +++++++++++++ 13 files changed, 149 insertions(+), 92 deletions(-) create mode 100644 philo/eat.c rename philo/{start_philo.c => end_philo.c} (79%) delete mode 100644 philo/note create mode 100644 philo/utils.c diff --git a/philo/check_death.c b/philo/check_death.c index 2801b82..2989499 100644 --- a/philo/check_death.c +++ b/philo/check_death.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/24 23:03:08 by adjoly #+# #+# */ -/* Updated: 2024/07/24 23:05:15 by adjoly ### ########.fr */ +/* Updated: 2024/07/26 17:32:52 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ bool get_death(bool in, bool ret) { static bool death; - + if (ret == false) death = in; return (death); diff --git a/philo/eat.c b/philo/eat.c new file mode 100644 index 0000000..0f8c69a --- /dev/null +++ b/philo/eat.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* eat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/30 02:29:39 by adjoly #+# #+# */ +/* Updated: 2024/07/31 21:53:13 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo.h" + +bool eat(t_philo *philo) +{ + bool death; + + philo->state = FORK_TAKEN; + take_fork(&philo->fork, philo->id); + log_philo(*philo); + if (&(philo->fork.left) == philo->fork.right) + return (true); + take_fork(&philo->fork, philo->id + 1); + log_philo(*philo); + philo->state = EAT; + gettimeofday(&(philo->eat), NULL); + log_philo(*philo); + death = sleep_phil(*philo); + return (death); +} diff --git a/philo/start_philo.c b/philo/end_philo.c similarity index 79% rename from philo/start_philo.c rename to philo/end_philo.c index d22704d..cc51479 100644 --- a/philo/start_philo.c +++ b/philo/end_philo.c @@ -1,18 +1,18 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* start_philo.c :+: :+: :+: */ +/* end_philo.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/24 18:19:14 by adjoly #+# #+# */ -/* Updated: 2024/07/25 16:46:57 by adjoly ### ########.fr */ +/* Updated: 2024/07/26 16:50:54 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" -void start_philo(t_init *init, uint16_t philo_nbr) +void end_philo(pthread_t *thread, uint16_t philo_nbr) { uint16_t i; int r; @@ -20,7 +20,7 @@ void start_philo(t_init *init, uint16_t philo_nbr) i = 0; while (i < philo_nbr) { - r = pthread_join(init[i].thread, NULL); + r = pthread_join(thread[i], NULL); if (r != 0) return ; i++; diff --git a/philo/get_time_in_ms.c b/philo/get_time_in_ms.c index 093f291..ae7817b 100644 --- a/philo/get_time_in_ms.c +++ b/philo/get_time_in_ms.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/09 12:02:24 by adjoly #+# #+# */ -/* Updated: 2024/07/24 23:51:20 by adjoly ### ########.fr */ +/* Updated: 2024/07/31 20:35:51 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ #include #include "philo.h" -uint16_t get_time_in_ms(struct timeval t0, struct timeval t1) +uint32_t get_time_in_ms(struct timeval t0, struct timeval t1) { return (((t1.tv_sec - t0.tv_sec) * 1000000 + \ t1.tv_usec - t0.tv_usec) / 1000); diff --git a/philo/init_fork.c b/philo/init_fork.c index 51d00ff..2189359 100644 --- a/philo/init_fork.c +++ b/philo/init_fork.c @@ -6,24 +6,24 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/22 15:29:08 by adjoly #+# #+# */ -/* Updated: 2024/07/26 15:24:07 by adjoly ### ########.fr */ +/* Updated: 2024/07/31 17:40:57 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" +#include void init_fork(t_pdata data) { - uint16_t i; - t_init init[PHILO_MAX]; - + uint8_t i; + t_philo philo[PHILO_MAX]; i = 0; while (i < data.philo_nbr) { - pthread_mutex_init(&(init[i].data.fork.left), NULL); - init[i].data.fork.right = &init[(i - 1) % data.philo_nbr].data.fork.left; + pthread_mutex_init(&(philo[i].fork.left), NULL); + philo[i].fork.right = &philo[(i + 1) % data.philo_nbr].fork.left; i++; } - init_philo(data, init); + init_philo(data, philo); } diff --git a/philo/init_philo.c b/philo/init_philo.c index 4b9168a..f2fd2bb 100644 --- a/philo/init_philo.c +++ b/philo/init_philo.c @@ -6,33 +6,35 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/11 14:36:59 by adjoly #+# #+# */ -/* Updated: 2024/07/26 15:25:58 by adjoly ### ########.fr */ +/* Updated: 2024/07/31 19:00:58 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" #include -void init_philo(t_pdata data, t_init *init) +void init_philo(t_pdata data, t_philo *philo) { int r; pthread_mutex_t check; - uint16_t i; + pthread_t thread[PHILO_MAX]; + struct timeval time; + uint8_t i; i = 0; pthread_mutex_init(&check, NULL); get_death(false, false); + gettimeofday(&time, NULL); while (i < data.philo_nbr) { - init[i].data.id = i + 1; - init[i].data.state = SLEEP; - init[i].data.data = data; - init[i].data.check = ✓ - r = pthread_create(&init[i].thread, NULL, - philo_routine, &(init[i].data)); + philo[i].id = i + 1; + philo[i].data = data; + philo[i].check = ✓ + philo[i].t0 = time; + r = pthread_create(&thread[i], NULL, philo_routine, &philo[i]); if (r != 0) break ; i++; } - start_philo(init, i); + end_philo(thread, data.philo_nbr); } diff --git a/philo/note b/philo/note deleted file mode 100644 index f1f20a1..0000000 --- a/philo/note +++ /dev/null @@ -1,15 +0,0 @@ -EAT -lock left -lock right -geteattime -print -sleep time_to_eat ms - -SLEEP -print -sleep time_to_sleep ms - -THINK -print - -REPEAT diff --git a/philo/parsing.c b/philo/parsing.c index 9f4aa14..d8dc188 100644 --- a/philo/parsing.c +++ b/philo/parsing.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/08 15:30:46 by adjoly #+# #+# */ -/* Updated: 2024/07/21 21:23:35 by adjoly ### ########.fr */ +/* Updated: 2024/07/31 17:52:08 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/philo/philo.c b/philo/philo.c index c1c248b..455dbdb 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/26 15:23:45 by adjoly ### ########.fr */ +/* Updated: 2024/07/31 15:59:39 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/philo/philo.h b/philo/philo.h index b8dc448..9fba4e1 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/26 15:25:57 by adjoly ### ########.fr */ +/* Updated: 2024/07/31 21:55:13 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,8 +37,9 @@ typedef struct s_pdata uint32_t die_time; uint32_t sleep_time; uint32_t eat_time; - uint16_t philo_nbr; - uint8_t meal_nbr; + uint8_t philo_nbr; + uint16_t meal_nbr; + bool no_meal; bool error; } t_pdata; @@ -52,38 +53,41 @@ typedef struct s_philo { uint16_t id; struct timeval t0; + struct timeval eat; + uint16_t meal_left; t_fork fork; t_pstate state; t_pdata data; pthread_mutex_t *check; } t_philo; -typedef struct s_init -{ - pthread_t thread; - t_philo data; -} t_init; - /** * Utils */ t_pdata philo_parse(char **argv, int ac); long long ft_atoll(const char *nptr); -uint16_t get_time_in_ms(struct timeval t0, struct timeval t1); +uint32_t get_time_in_ms(struct timeval t0, struct timeval t1); void log_philo(t_philo philo); -bool sleep_phil(uint32_t sleep_time, pthread_mutex_t *death); bool get_death(bool in, bool ret); +bool sleep_phil(t_philo philo); +uint16_t get_meal_nb(uint16_t meal_nbr, bool no_meal); /** * Main path * by order of call */ 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 init_philo(t_pdata data, t_philo *philo); +void end_philo(pthread_t *thread, uint16_t philo_nbr); void *philo_routine(void *content); +/** + * Routine func + */ +void take_fork(t_fork *fork, int id); +bool eat(t_philo *philo); + /** * For debug purpose to be REMOVED */ diff --git a/philo/philo_routine.c b/philo/philo_routine.c index 5750283..6e91b78 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/26 15:15:13 by adjoly ### ########.fr */ +/* Updated: 2024/07/31 22:10:31 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,39 +16,40 @@ void take_fork(t_fork *fork, int id) { - if (id % 2) - { + if (id % 2 && !(&fork->left == fork->right)) pthread_mutex_lock(&fork->left); - } - else - { + else if (!(id % 2)) pthread_mutex_lock(fork->right); - } } void *philo_routine(void *content) { - t_philo philo; + t_philo *philo; bool death; - int i = 0; - philo = *(t_philo *)content; - gettimeofday(&(philo.t0), NULL); - while (i < philo.data.meal_nbr) + philo = content; + gettimeofday(&(philo->eat), NULL); + philo->meal_left = get_meal_nb(philo->data.meal_nbr, philo->data.no_meal); + while (1) { - 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); + if (eat(philo) == true) + break ; + // unlock pas les fourchet si po la + pthread_mutex_unlock(&philo->fork.left); + pthread_mutex_unlock(philo->fork.right); + if (philo->data.no_meal == false) + philo->meal_left--; + philo->state = SLEEP; + log_philo(*philo); + death = sleep_phil(*philo); if (death == true) + break ; + if (!philo->meal_left) return (NULL); - pthread_mutex_unlock(philo.fork.right); - pthread_mutex_unlock(&philo.fork.left); - i++; + philo->state = THINK; + log_philo(*philo); } + philo->state = DEAD; + log_philo(*philo); return (NULL); } diff --git a/philo/sleep.c b/philo/sleep.c index a756ea7..ad9889b 100644 --- a/philo/sleep.c +++ b/philo/sleep.c @@ -6,31 +6,44 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/23 17:15:24 by adjoly #+# #+# */ -/* Updated: 2024/07/25 19:57:03 by adjoly ### ########.fr */ +/* Updated: 2024/07/30 02:48:34 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" - -uint32_t time_to_ms(void) -{ - struct timeval time; - - gettimeofday(&time, NULL); - return (time.tv_sec * 1000 + time.tv_usec / 1000); -} - - #define SLEEP_SLICE_MS 10 -bool sleep_phil(uint32_t sleep_time, pthread_mutex_t *death) +bool sleep_phil(t_philo philo) { + struct timeval t1; + uint32_t sleep_time; + + sleep_time = 0; + if (philo.state == EAT) + sleep_time = philo.data.eat_time; + else if (philo.state == SLEEP) + sleep_time = philo.data.sleep_time; while (sleep_time) { - pthread_mutex_lock(death); + pthread_mutex_lock(philo.check); if (get_death(false, true) == true) + { + pthread_mutex_unlock(philo.check); return (true); - pthread_mutex_unlock(death); + } + pthread_mutex_unlock(philo.check); + gettimeofday(&t1, NULL); + if (get_time_in_ms(philo.eat, t1) > philo.data.die_time) + { + pthread_mutex_lock(philo.check); + get_death(true, false); + pthread_mutex_unlock(philo.check); + pthread_mutex_unlock(&philo.fork.left); + pthread_mutex_unlock(philo.fork.right); + philo.state = DEAD; + log_philo(philo); + return (true); + } usleep(SLEEP_SLICE_MS * 1000); sleep_time -= SLEEP_SLICE_MS; } diff --git a/philo/utils.c b/philo/utils.c new file mode 100644 index 0000000..48df49a --- /dev/null +++ b/philo/utils.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/31 21:20:49 by adjoly #+# #+# */ +/* Updated: 2024/07/31 21:55:16 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo.h" + +uint16_t get_meal_nb(uint16_t meal_nbr, bool no_meal) +{ + if (no_meal == true) + return (1); + else + return (meal_nbr); +}