From ec9ee12c29cec300bf876d479aa515f85cf93902 Mon Sep 17 00:00:00 2001 From: Adam Joly Date: Thu, 8 Aug 2024 18:48:47 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat:=20nearly=20?= =?UTF-8?q?working=20to=5Fdo:=20implement=20all=20philo=20eat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- philo/__debug_print_philo_data.c | 18 --------- philo/check_death.c | 22 ---------- philo/eat.c | 37 +++++++++++++++++ philo/ft_atoll.c | 35 ---------------- philo/init_philo.c | 5 ++- philo/log.c | 26 ++++++------ philo/parsing.c | 3 +- philo/philo.h | 9 +++-- philo/philo_routine.c | 51 +++++------------------- philo/sleep.c | 37 +++++++---------- philo/{get_time_in_ms.c => util_philo.c} | 44 +++++++++++++++++--- philo/utils.c | 36 ++++++++++++----- 12 files changed, 149 insertions(+), 174 deletions(-) delete mode 100644 philo/__debug_print_philo_data.c delete mode 100644 philo/check_death.c create mode 100644 philo/eat.c delete mode 100644 philo/ft_atoll.c rename philo/{get_time_in_ms.c => util_philo.c} (51%) diff --git a/philo/__debug_print_philo_data.c b/philo/__debug_print_philo_data.c deleted file mode 100644 index d91c410..0000000 --- a/philo/__debug_print_philo_data.c +++ /dev/null @@ -1,18 +0,0 @@ -#include "philo.h" -#include - -void print_philo_data(t_pdata data) -{ - if (data.error == true) - { - printf("error = true\n"); - return ; - } - else - printf("error = false\n"); - printf("die_time = %u\n", data.die_time); - printf("sleep_time = %u\n", data.sleep_time); - printf("eat_time = %u\n", data.eat_time); - printf("philo_nbr = %u\n", data.philo_nbr); - printf("meal_nbr = %u\n", data.meal_nbr); -} diff --git a/philo/check_death.c b/philo/check_death.c deleted file mode 100644 index 2989499..0000000 --- a/philo/check_death.c +++ /dev/null @@ -1,22 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* check_death.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/07/24 23:03:08 by adjoly #+# #+# */ -/* Updated: 2024/07/26 17:32:52 by adjoly ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "philo.h" - -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..365a5bb --- /dev/null +++ b/philo/eat.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* eat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/08 17:29:51 by adjoly #+# #+# */ +/* Updated: 2024/08/08 18:05:07 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo.h" + +bool philo_eat(t_philo *philo) +{ + philo->state = FORK_TAKEN; + take_fork(&philo->fork, philo->id); + if (get_death(false, true, philo)) + return (true); + log_philo(philo); + if (&(philo->fork.left) == philo->fork.right) + return (true); + take_fork(&philo->fork, philo->id + 1); + if (get_death(false, true, philo)) + return (true); + log_philo(philo); + philo->state = EAT; + gettimeofday(&(philo->eat), NULL); + log_philo(philo); + if (sleep_phil(philo) == true) + return (true); + pthread_mutex_unlock(&philo->fork.left); + pthread_mutex_unlock(philo->fork.right); + philo->meal_left--; + return (false); +} diff --git a/philo/ft_atoll.c b/philo/ft_atoll.c deleted file mode 100644 index a724d16..0000000 --- a/philo/ft_atoll.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_atoll.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/07/08 17:29:13 by adjoly #+# #+# */ -/* Updated: 2024/07/08 17:29:15 by adjoly ### ########.fr */ -/* */ -/* ************************************************************************** */ - -long long ft_atoll(const char *nptr) -{ - char sign; - long long nbr; - - sign = 1; - nbr = 0; - while ((*nptr >= 7 && *nptr <= 13) || *nptr == 32) - nptr++; - if (*nptr == '-') - { - sign *= -1; - nptr++; - } - else if (*nptr == '+') - nptr++; - while (*nptr >= '0' && *nptr <= '9') - { - nbr = nbr * 10 + (*nptr - '0'); - nptr++; - } - return (nbr * sign); -} diff --git a/philo/init_philo.c b/philo/init_philo.c index f2fd2bb..842a58f 100644 --- a/philo/init_philo.c +++ b/philo/init_philo.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/11 14:36:59 by adjoly #+# #+# */ -/* Updated: 2024/07/31 19:00:58 by adjoly ### ########.fr */ +/* Updated: 2024/08/08 18:07:47 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,8 @@ void init_philo(t_pdata data, t_philo *philo) i = 0; pthread_mutex_init(&check, NULL); - get_death(false, false); + philo[i].check = ✓ + get_death(false, false, philo); gettimeofday(&time, NULL); while (i < data.philo_nbr) { diff --git a/philo/log.c b/philo/log.c index 1ea18c0..77e5086 100644 --- a/philo/log.c +++ b/philo/log.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/07 16:12:20 by adjoly #+# #+# */ -/* Updated: 2024/07/24 23:56:07 by adjoly ### ########.fr */ +/* Updated: 2024/08/08 18:01:52 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,24 +14,24 @@ #include "philo_msg.h" #include -void log_philo(t_philo philo) +void log_philo(t_philo *philo) { uint32_t timestamp; struct timeval t1; static pthread_mutex_t print = {0}; gettimeofday(&t1, NULL); - timestamp = get_time_in_ms(philo.t0, t1); + timestamp = get_time_in_ms(philo->t0, t1); pthread_mutex_lock(&print); - if (philo.state == EAT) - printf("%u %hu %s", timestamp, philo.id, EATING_MSG); - else if (philo.state == THINK) - printf("%u %hu %s", timestamp, philo.id, THINK_MSG); - else if (philo.state == SLEEP) - printf("%u %hu %s", timestamp, philo.id, SLEEP_MSG); - else if (philo.state == DEAD) - printf("%u %hu %s", timestamp, philo.id, DIED_MSG); - else if (philo.state == FORK_TAKEN) - printf("%u %hu %s", timestamp, philo.id, FORK_MSG); + if (philo->state == EAT) + printf("%u %hu %s", timestamp, philo->id, EATING_MSG); + else if (philo->state == THINK) + printf("%u %hu %s", timestamp, philo->id, THINK_MSG); + else if (philo->state == SLEEP) + printf("%u %hu %s", timestamp, philo->id, SLEEP_MSG); + else if (philo->state == DEAD) + printf("%u %hu %s", timestamp, philo->id, DIED_MSG); + else if (philo->state == FORK_TAKEN) + printf("%u %hu %s", timestamp, philo->id, FORK_MSG); pthread_mutex_unlock(&print); } diff --git a/philo/parsing.c b/philo/parsing.c index 00d9d13..ff4cab4 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/08/06 19:48:54 by adjoly ### ########.fr */ +/* Updated: 2024/08/08 17:26:05 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,7 +47,6 @@ bool check_av(char **av) return (false); } - t_pdata ret_err(t_pdata data, uint8_t error) { print_err(error); diff --git a/philo/philo.h b/philo/philo.h index 2d2be4f..33070ef 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/08/06 19:22:09 by adjoly ### ########.fr */ +/* Updated: 2024/08/08 18:12:39 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,13 +66,16 @@ typedef struct s_philo */ size_t ft_strlen(char *s); uint16_t get_meal_nb(uint16_t meal_nbr, bool no_meal); -bool get_death(bool in, bool ret); +bool get_death(bool in, bool ret, t_philo *philo); long long ft_atoll(const char *nptr); uint32_t get_time_in_ms(struct timeval t0, struct timeval t1); +uint32_t get_current_time(void); -void log_philo(t_philo philo); +void log_philo(t_philo *philo); bool sleep_phil(t_philo *philo); t_pdata philo_parse(char **argv, int ac); +bool print_death(t_philo *philo); +bool philo_eat(t_philo *philo); /** * Main path * by order of call diff --git a/philo/philo_routine.c b/philo/philo_routine.c index 1fa06e5..191d3a8 100644 --- a/philo/philo_routine.c +++ b/philo/philo_routine.c @@ -6,15 +6,14 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/22 21:24:53 by adjoly #+# #+# */ -/* Updated: 2024/08/06 18:55:55 by adjoly ### ########.fr */ +/* Updated: 2024/08/08 18:37:39 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" #include - -void take_fork(t_fork *fork, int id) +void take_fork(t_fork *fork, int id) { if (id % 2 && !(&fork->left == fork->right)) pthread_mutex_lock(&fork->left); @@ -25,54 +24,26 @@ void take_fork(t_fork *fork, int id) void *philo_routine(void *content) { t_philo *philo; - bool death; philo = content; gettimeofday(&(philo->eat), NULL); philo->meal_left = get_meal_nb(philo->data.meal_nbr, philo->data.no_meal); + if (!(philo->id % 2)) + usleep(10); while (1) { - philo->state = FORK_TAKEN; - take_fork(&philo->fork, philo->id); - pthread_mutex_lock(philo->check); - death = get_death(false, true); - pthread_mutex_unlock(philo->check); - if (death == true) + if (philo_eat(philo) == true) break ; - log_philo(*philo); - if (&(philo->fork.left) == philo->fork.right) - { - philo->state = DEAD; - sleep_phil(philo); - log_philo(*philo); - return (NULL); - } - take_fork(&philo->fork, philo->id + 1); - pthread_mutex_lock(philo->check); - death = get_death(false, true); - pthread_mutex_unlock(philo->check); - if (death == true) - break ; - log_philo(*philo); - philo->state = EAT; - gettimeofday(&(philo->eat), NULL); - log_philo(*philo); - if (sleep_phil(philo) == true) - break ; - pthread_mutex_unlock(&philo->fork.left); - pthread_mutex_unlock(philo->fork.right); - philo->meal_left--; - philo->state = SLEEP; - log_philo(*philo); - sleep_phil(philo); - philo->state = THINK; - log_philo(*philo); if (philo->meal_left == 0) return (NULL); + philo->state = SLEEP; + log_philo(philo); + if (sleep_phil(philo)) + break ; + philo->state = THINK; + log_philo(philo); } pthread_mutex_unlock(&philo->fork.left); pthread_mutex_unlock(philo->fork.right); - philo->state = DEAD; - log_philo(*philo); return (NULL); } diff --git a/philo/sleep.c b/philo/sleep.c index 58c3778..190d0ec 100644 --- a/philo/sleep.c +++ b/philo/sleep.c @@ -6,48 +6,39 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/23 17:15:24 by adjoly #+# #+# */ -/* Updated: 2024/08/06 18:45:19 by adjoly ### ########.fr */ +/* Updated: 2024/08/08 18:06:32 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" -#define SLEEP_SLICE_MS 2 +#define SLEEP_SLICE_MS 5 -uint32_t get_current_time(void) +uint32_t get_sleep_time(t_philo *philo) { - struct timeval time; - - gettimeofday(&time, NULL); - return (time.tv_sec * 1000 + time.tv_usec / 1000); + if (philo->state == EAT) + return (philo->data.eat_time + get_current_time()); + else if (philo->state == SLEEP) + return (philo->data.sleep_time + get_current_time()); + else if (philo->state == DEAD) + return (philo->data.die_time + get_current_time()); + return (0); } bool sleep_phil(t_philo *philo) { struct timeval t1; uint32_t sleep_time; - bool death; - sleep_time = 0; - if (philo->state == EAT) - sleep_time = philo->data.eat_time + get_current_time(); - else if (philo->state == SLEEP) - sleep_time = philo->data.sleep_time + get_current_time(); - else if (philo->state == DEAD) - sleep_time = philo->data.die_time + get_current_time(); + sleep_time = get_sleep_time(philo); while (get_current_time() < sleep_time) { - pthread_mutex_lock(philo->check); - death = get_death(false, true); - pthread_mutex_unlock(philo->check); - if (death == true) + if (get_death(false, true, philo)) return (true); 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); - return (true); + get_death(true, false, philo); + return (print_death(philo)); } usleep(SLEEP_SLICE_MS * 1000); } diff --git a/philo/get_time_in_ms.c b/philo/util_philo.c similarity index 51% rename from philo/get_time_in_ms.c rename to philo/util_philo.c index ae7817b..543303f 100644 --- a/philo/get_time_in_ms.c +++ b/philo/util_philo.c @@ -1,21 +1,53 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* get_time_in_ms.c :+: :+: :+: */ +/* util_philo.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/07/09 12:02:24 by adjoly #+# #+# */ -/* Updated: 2024/07/31 20:35:51 by adjoly ### ########.fr */ +/* Created: 2024/08/08 18:16:45 by adjoly #+# #+# */ +/* Updated: 2024/08/08 18:16:53 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include -#include #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); +} + +uint32_t get_current_time(void) +{ + struct timeval time; + + gettimeofday(&time, NULL); + return (time.tv_sec * 1000 + time.tv_usec / 1000); +} + +bool get_death(bool in, bool ret, t_philo *philo) +{ + static bool death; + + pthread_mutex_lock(philo->check); + if (ret == false) + death = in; + pthread_mutex_unlock(philo->check); + return (death); +} + 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); + t1.tv_usec - t0.tv_usec) / 1000); +} + +bool print_death(t_philo *philo) +{ + philo->state = DEAD; + log_philo(philo); + return (true); } diff --git a/philo/utils.c b/philo/utils.c index 1356338..0fba6f2 100644 --- a/philo/utils.c +++ b/philo/utils.c @@ -6,26 +6,42 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/31 21:20:49 by adjoly #+# #+# */ -/* Updated: 2024/08/06 19:16:42 by adjoly ### ########.fr */ +/* Updated: 2024/08/08 18:17:09 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); -} - size_t ft_strlen(char *s) { char *tmp; tmp = s; - while(*tmp) + while (*tmp) tmp++; return (tmp - s); } + +long long ft_atoll(const char *nptr) +{ + char sign; + long long nbr; + + sign = 1; + nbr = 0; + while ((*nptr >= 7 && *nptr <= 13) || *nptr == 32) + nptr++; + if (*nptr == '-') + { + sign *= -1; + nptr++; + } + else if (*nptr == '+') + nptr++; + while (*nptr >= '0' && *nptr <= '9') + { + nbr = nbr * 10 + (*nptr - '0'); + nptr++; + } + return (nbr * sign); +}