From dde531f8dddc022ae55bac3b7a3c919ff2a611d0 Mon Sep 17 00:00:00 2001 From: adjoly Date: Tue, 6 Aug 2024 19:51:13 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat:=20parsing?= =?UTF-8?q?=20protection=20working?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- philo/eat.c | 31 ----------------- philo/parsing.c | 78 +++++++++++++++++++++++++++++++++++++------ philo/philo.c | 4 +-- philo/philo.h | 13 ++++---- philo/philo_msg.h | 9 ++++- philo/philo_routine.c | 47 +++++++++++++++++++------- philo/sleep.c | 48 ++++++++++++++------------ philo/utils.c | 12 ++++++- 8 files changed, 156 insertions(+), 86 deletions(-) delete mode 100644 philo/eat.c diff --git a/philo/eat.c b/philo/eat.c deleted file mode 100644 index 0f8c69a..0000000 --- a/philo/eat.c +++ /dev/null @@ -1,31 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* 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/parsing.c b/philo/parsing.c index d8dc188..00d9d13 100644 --- a/philo/parsing.c +++ b/philo/parsing.c @@ -6,34 +6,90 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/08 15:30:46 by adjoly #+# #+# */ -/* Updated: 2024/07/31 17:52:08 by adjoly ### ########.fr */ +/* Updated: 2024/08/06 19:48:54 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" +#include "philo_msg.h" + +bool print_err(uint8_t error) +{ + if (error == 0) + printf(ERR_MAX_PHIL); + else if (error == 1) + printf(ERR_MAX_DIE_TIME); + else if (error == 2) + printf(ERR_MAX_EAT_TIME); + else if (error == 3) + printf(ERR_MAX_SLEEP_TIME); + else if (error == 4) + printf(ERR_MAX_MEAL); + else if (error == 5) + printf(ERR_NB_ARG); + return (true); +} + +bool check_av(char **av) +{ + if (!av) + return (print_err(5)); + if (ft_strlen(av[0]) > 3) + return (print_err(0)); + if (ft_strlen(av[1]) > 11) + return (print_err(1)); + if (ft_strlen(av[2]) > 11) + return (print_err(2)); + if (ft_strlen(av[3]) > 11) + return (print_err(3)); + if (av[4] && ft_strlen(av[4]) > 4) + return (print_err(4)); + return (false); +} + + +t_pdata ret_err(t_pdata data, uint8_t error) +{ + print_err(error); + data.error = true; + return (data); +} t_pdata fill_pdata(char **av) { t_pdata data; - if (!av) + data.philo_nbr = 0; + if (!av && check_av(av)) + return (ret_err(data, 255)); + data.philo_nbr = ft_atoll(av[0]); + if (data.philo_nbr > 200) + return (ret_err(data, 0)); + data.die_time = ft_atoll(av[1]); + if (data.die_time > 2147483647) + return (ret_err(data, 1)); + data.eat_time = ft_atoll(av[2]); + if (data.eat_time > 2147483647) + return (ret_err(data, 2)); + data.sleep_time = ft_atoll(av[3]); + if (data.sleep_time > 2147483647) + return (ret_err(data, 3)); + if (av[4]) { - data.error = true; - return (data); + data.meal_nbr = ft_atoll(av[4]); + if (data.meal_nbr > 1000) + return (ret_err(data, 4)); } - data.philo_nbr = ft_atoll(av[1]); - data.die_time = ft_atoll(av[2]); - data.eat_time = ft_atoll(av[3]); - data.sleep_time = ft_atoll(av[4]); - if (av[5]) - data.meal_nbr = ft_atoll(av[5]); data.error = false; return (data); } t_pdata philo_parse(char **av, int ac) { + t_pdata data; + + data.philo_nbr = 0; if (!(ac > 4 && ac <= 6)) - return (fill_pdata(NULL)); + return (ret_err(data, 5)); return (fill_pdata(av)); } diff --git a/philo/philo.c b/philo/philo.c index 455dbdb..626df31 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/31 15:59:39 by adjoly ### ########.fr */ +/* Updated: 2024/08/06 19:46:32 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ int main(int ac, char **av) { t_pdata data; - data = philo_parse(av, ac); + data = philo_parse(av + 1, ac); if (data.error == true) return (EXIT_FAILURE); init_fork(data); diff --git a/philo/philo.h b/philo/philo.h index 9fba4e1..2d2be4f 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/31 21:55:13 by adjoly ### ########.fr */ +/* Updated: 2024/08/06 19:22:09 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -64,14 +64,15 @@ typedef struct s_philo /** * Utils */ -t_pdata philo_parse(char **argv, int ac); +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); long long ft_atoll(const char *nptr); uint32_t get_time_in_ms(struct timeval t0, struct timeval t1); -void log_philo(t_philo philo); -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); +void log_philo(t_philo philo); +bool sleep_phil(t_philo *philo); +t_pdata philo_parse(char **argv, int ac); /** * Main path * by order of call diff --git a/philo/philo_msg.h b/philo/philo_msg.h index 36dee37..64a014d 100644 --- a/philo/philo_msg.h +++ b/philo/philo_msg.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/07 17:50:46 by adjoly #+# #+# */ -/* Updated: 2024/07/07 17:53:53 by adjoly ### ########.fr */ +/* Updated: 2024/08/06 19:47:17 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,4 +19,11 @@ # define DIED_MSG "died\n" # define FORK_MSG "has taken a fork\n" +# define ERR_MAX_PHIL "Too much philo < 200\n" // err 0 +# define ERR_MAX_DIE_TIME "Time to die too long < INT_MAX\n" // err 1 +# define ERR_MAX_EAT_TIME "Time to eat too long < INT_MAX\n" // err 2 +# define ERR_MAX_SLEEP_TIME "Time to sleep too long < INT_MAX\n" // err 3 +# define ERR_MAX_MEAL "Too much meal < 1000\n" // err 4 +# define ERR_NB_ARG "Invalid number of args 4 or 5\n" // err 5 + #endif diff --git a/philo/philo_routine.c b/philo/philo_routine.c index 6e91b78..1fa06e5 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/31 22:10:31 by adjoly ### ########.fr */ +/* Updated: 2024/08/06 18:55:55 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,23 +32,46 @@ void *philo_routine(void *content) philo->meal_left = get_meal_nb(philo->data.meal_nbr, philo->data.no_meal); while (1) { - 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); + 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) break ; - if (!philo->meal_left) + 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); } + 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 ad9889b..58c3778 100644 --- a/philo/sleep.c +++ b/philo/sleep.c @@ -6,46 +6,50 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/23 17:15:24 by adjoly #+# #+# */ -/* Updated: 2024/07/30 02:48:34 by adjoly ### ########.fr */ +/* Updated: 2024/08/06 18:45:19 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" -#define SLEEP_SLICE_MS 10 +#define SLEEP_SLICE_MS 2 -bool sleep_phil(t_philo philo) +uint32_t get_current_time(void) +{ + struct timeval time; + + gettimeofday(&time, NULL); + return (time.tv_sec * 1000 + time.tv_usec / 1000); +} + +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; - else if (philo.state == SLEEP) - sleep_time = philo.data.sleep_time; - while (sleep_time) + 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(); + while (get_current_time() < sleep_time) { - pthread_mutex_lock(philo.check); - if (get_death(false, true) == true) - { - pthread_mutex_unlock(philo.check); + pthread_mutex_lock(philo->check); + death = get_death(false, true); + pthread_mutex_unlock(philo->check); + if (death == true) return (true); - } - pthread_mutex_unlock(philo.check); gettimeofday(&t1, NULL); - if (get_time_in_ms(philo.eat, t1) > philo.data.die_time) + if (get_time_in_ms(philo->eat, t1) > philo->data.die_time) { - pthread_mutex_lock(philo.check); + 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); + pthread_mutex_unlock(philo->check); return (true); } usleep(SLEEP_SLICE_MS * 1000); - sleep_time -= SLEEP_SLICE_MS; } return (false); } diff --git a/philo/utils.c b/philo/utils.c index 48df49a..1356338 100644 --- a/philo/utils.c +++ b/philo/utils.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/31 21:20:49 by adjoly #+# #+# */ -/* Updated: 2024/07/31 21:55:16 by adjoly ### ########.fr */ +/* Updated: 2024/08/06 19:16:42 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,3 +19,13 @@ uint16_t get_meal_nb(uint16_t meal_nbr, bool no_meal) else return (meal_nbr); } + +size_t ft_strlen(char *s) +{ + char *tmp; + + tmp = s; + while(*tmp) + tmp++; + return (tmp - s); +}