From e45ca1331f6cff52ea25b73c1ea5f62d14b76944 Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Wed, 26 Jan 2022 18:58:39 +0100 Subject: [PATCH] gestion cas n_eat a zero --- srcs/exec.c | 6 ++---- srcs/init.c | 2 +- srcs/launch.c | 16 +++++++++++++++- srcs/utils.c | 18 +++++++++--------- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/srcs/exec.c b/srcs/exec.c index 2360bdb..901f856 100644 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/26 15:30:28 by hulamy #+# #+# */ -/* Updated: 2022/01/26 16:29:49 by hulamy ### ########.fr */ +/* Updated: 2022/01/26 18:51:44 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,10 +44,8 @@ static int eat(t_philo *philo) update_time(philo); if (print_message(philo, B_YELLOW, "is eating")) return (ret_err_unlock(philo, 2)); - philo->eat_count++; - if (philo->eat_count == philo->params->n_eat) - philo->global->satiated_count++; action_delay(philo, philo->params->t_eat); + philo->eat_count++; pthread_mutex_unlock(&(philo->next->m_fork)); pthread_mutex_unlock(&(philo->m_fork)); return (0); diff --git a/srcs/init.c b/srcs/init.c index 3b03b91..37bfba1 100644 --- a/srcs/init.c +++ b/srcs/init.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/26 15:30:34 by hulamy #+# #+# */ -/* Updated: 2022/01/26 17:55:34 by hulamy ### ########.fr */ +/* Updated: 2022/01/26 18:15:42 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/srcs/launch.c b/srcs/launch.c index 33e9dcb..f178ebc 100644 --- a/srcs/launch.c +++ b/srcs/launch.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/26 15:30:49 by hulamy #+# #+# */ -/* Updated: 2022/01/26 16:29:13 by hulamy ### ########.fr */ +/* Updated: 2022/01/26 18:52:32 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,17 +36,31 @@ static int is_finished(t_philo *philo, struct timeval *stime) return (0); } +int is_satiated(t_philo *philo) +{ + if (philo->eat_count == philo->params->n_eat) + { + philo->global->satiated_count++; + return (1); + } + return (0); +} + static void pere_fouettard(t_philo *philo) { struct timeval stime; int i; + int satiated; while (philo->global->stop == 0) { i = 0; + satiated = 1; gettimeofday(&stime, NULL); while (i < philo->params->n_phi) { + if (satiated == 1 && is_satiated(philo) == 0) + satiated = 0; if (is_finished(philo, &stime)) break ; philo = philo->next; diff --git a/srcs/utils.c b/srcs/utils.c index 53546e5..7eeb48a 100644 --- a/srcs/utils.c +++ b/srcs/utils.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/26 15:30:19 by hulamy #+# #+# */ -/* Updated: 2022/01/26 17:50:31 by hulamy ### ########.fr */ +/* Updated: 2022/01/26 18:04:08 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ int ft_isdigit_2d_arr(char **str) size_t ft_strlen(char *str) { - size_t i; + size_t i; i = 0; while (str[i]) @@ -56,9 +56,9 @@ int ft_strncmp(char *s1, char *s2, size_t n) return (res); } -int ft_int_overflow(char *str) +int ft_int_overflow(char *str) { - size_t len; + size_t len; char *int_xtrem; int_xtrem = "2147483647"; @@ -67,11 +67,11 @@ int ft_int_overflow(char *str) if (str[0] == '+' || str[0] == '-') str++; len = ft_strlen(str); - if (len < 10) - return (0); - else if (len > 10 || ft_strncmp(str, int_xtrem, len) > 0) - return (1); - return (0); + if (len < 10) + return (0); + else if (len > 10 || ft_strncmp(str, int_xtrem, len) > 0) + return (1); + return (0); } int ft_atoi(const char *str)