From 475817540cb3a07fdba582913ebf15517619af1a Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Tue, 25 Jan 2022 22:05:39 +0100 Subject: [PATCH] erreur lors des arrets --- headers/philo_proto.h | 4 +-- srcs/exec.c | 74 +++++++------------------------------------ srcs/generic.c | 35 ++++++++++---------- srcs/init.c | 1 + srcs/main.c | 25 ++++----------- 5 files changed, 40 insertions(+), 99 deletions(-) diff --git a/headers/philo_proto.h b/headers/philo_proto.h index 561ec4e..8217f97 100644 --- a/headers/philo_proto.h +++ b/headers/philo_proto.h @@ -11,8 +11,8 @@ void *philo_exec(void *arg); void init_time(t_philo *philo); void update_time(t_philo *philo); int diff_time(t_time old, struct timeval stime); -int is_dead(t_philo *philo); -void print_message(t_philo *philo, char *msg); +int is_dead(t_philo *philo, struct timeval stime); +int print_message(t_philo *philo, char *clr, char *msg); #endif diff --git a/srcs/exec.c b/srcs/exec.c index c7070c9..72e9b14 100644 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -5,51 +5,6 @@ void go_sleep(t_philo *philo, int action_time) struct timeval stime; int time_to_death; - // now_s - // now_u - // last_meal_s - // last_meal_u - // - // last_meal now - // v v - // ------|-----------|------------------------------------------ - // | | - // |_d_e_a_t_h__t_i_m_e__________________| - // | - // |_e_a_t__t_i_m_e_________________|->action_time - // | - // |_d_i_f_f___| - // | - // |_d_e_a_t_h_-_d_i_f_f_____|->time_to_death - // - // - // last_meal now - // v v - // ------|-----------|------------------------------------------ - // | - // |_d_e_a_t_h__t_i_m_e_______________________________| - // | - // |_e_a_t__t_i_m_e_________________|->action_time - // | - // |_d_i_f_f___| - // | - // |_d_e_a_t_h_-_d_i_f_f__________________|->time_to_death - // - // - // last_meal now - // v v - // ------|-----------|------------------------------------------ - // | - // |_death___| - // | - // |_e_a_t__t_i_m_e_________________|->action_time - // | - // |_d_i_f_f___| - // | - // |_|->time_to_death - // - // - gettimeofday(&stime, NULL); time_to_death = diff_time(philo->t_last_meal, stime); time_to_death = philo->params->t_die - time_to_death; @@ -59,30 +14,27 @@ void go_sleep(t_philo *philo, int action_time) usleep(philo->params->t_die * 1000); } -int ret_unlock(t_philo *philo, int nbr_fork, int ret) +int ret_err_unlock(t_philo *philo, int nbr_fork) { pthread_mutex_unlock(&philo->m_fork); if (nbr_fork == 2) pthread_mutex_unlock(&philo->next->m_fork); - return (ret); + return (1); } -int take_forks(t_philo *philo) +int eat(t_philo *philo) { pthread_mutex_lock(&(philo->m_fork)); - if (is_dead(philo)) - return (ret_unlock(philo, 1, 1)); - print_message(philo, "has taken a fork"); + if (print_message(philo, WHITE, "has taken a fork") == 1) + return (ret_err_unlock(philo, 1)); pthread_mutex_lock(&(philo->next->m_fork)); - if (is_dead(philo)) - return (ret_unlock(philo, 2, 1)); - print_message(philo, "has taken a fork"); + if (print_message(philo, WHITE, "has taken a fork") == 1) + return (ret_err_unlock(philo, 2)); update_time(philo); - print_message(philo, "is eating"); + if (print_message(philo, B_YELLOW, "is eating") == 1) + return (ret_err_unlock(philo, 2)); philo->eat++; - if (philo->params->n_eat && philo->eat >= philo->params->n_eat) - return (ret_unlock(philo, 2, 1)); go_sleep(philo, philo->params->t_eat); pthread_mutex_unlock(&(philo->next->m_fork)); @@ -100,15 +52,13 @@ void *philo_exec(void *arg) usleep(10 * 1000); while (1) { - if (take_forks(philo) != 0) + if (eat(philo) != 0) break ; - if (is_dead(philo)) + if (print_message(philo, B_BLUE, "is sleeping") == 1) break ; - print_message(philo, "is sleeping"); go_sleep(philo, philo->params->t_slp); - if (is_dead(philo)) + if (print_message(philo, B_GREEN, "is thinking") == 1) break ; - print_message(philo, "is thinking"); } return (NULL); } diff --git a/srcs/generic.c b/srcs/generic.c index 5b49c30..2fd16c2 100644 --- a/srcs/generic.c +++ b/srcs/generic.c @@ -26,41 +26,42 @@ int diff_time(t_time old, struct timeval new) return ((new.tv_sec - old.ts) * 1000 + (new.tv_usec - old.tu) / 1000); } -int is_dead(t_philo *philo) +int is_dead(t_philo *philo, struct timeval stime) { - struct timeval stime; int time; if (philo->global->dead) - return (1); - gettimeofday(&stime, NULL); + return (-1); + if (philo->params->n_eat != -1 && philo->eat >= philo->params->n_eat) + return (-1); time = diff_time(philo->t_last_meal, stime); if (time >= philo->params->t_die) { philo->global->dead = 1; - print_message(philo, "died"); - return (1); + return (philo->p_nbr); } return (0); } -void print_message(t_philo *philo, char *msg) +int print_message(t_philo *philo, char *clr, char *msg) { long int time_stamp; struct timeval stime; - char *color; + char p_dead; + pthread_mutex_lock(&(philo->global->m_print)); gettimeofday(&stime, NULL); + p_dead = is_dead(philo, stime); time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000; time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000; - color = WHITE; - if (ft_strnstr(msg, "eating", ft_strlen(msg))) - color = B_YELLOW; - else if (ft_strnstr(msg, "sleeping", ft_strlen(msg))) - color = B_BLUE; - else if (ft_strnstr(msg, "thinking", ft_strlen(msg))) - color = B_GREEN; - pthread_mutex_lock(&(philo->global->m_print)); - ft_printf("%s%i %i %s%s\n", color, time_stamp, philo->p_nbr, msg, RESET); + if (p_dead != 0) + { + if (p_dead > 0) + ft_printf("%s%i %i died%s\n", B_RED, time_stamp, p_dead, RESET); + return (1); + } + else + ft_printf("%s%i %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET); pthread_mutex_unlock(&(philo->global->m_print)); + return (0); } diff --git a/srcs/init.c b/srcs/init.c index 5d4e1de..520ca91 100644 --- a/srcs/init.c +++ b/srcs/init.c @@ -55,6 +55,7 @@ t_params *init_params(int ac, char **av) params->t_die = ft_atoi(av[2]); params->t_eat = ft_atoi(av[3]); params->t_slp = ft_atoi(av[4]); + params->n_eat = -1; if (ac == 6) params->n_eat = ft_atoi(av[5]); } diff --git a/srcs/main.c b/srcs/main.c index eb90891..13d1ee7 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -13,32 +13,21 @@ void launch_threads(t_philo *philo, pthread_t *id) } } -void watch_threads(t_philo *philo, pthread_t *id) -{ - int i; - - i = 0; - while (1) - { - if (is_dead(philo) == 1) - break ; - } - while (i < philo->params->n_phi) - { - pthread_join(id[i], NULL); - i++; - } -} - int main(int ac, char **av) { pthread_t *id; t_philo *philo; + int i; philo = init(ac, av, &id); if (philo == NULL) return (0); launch_threads(philo, id); - watch_threads(philo, id); + i = 0; + while (i < philo->params->n_phi) + { + pthread_join(id[i], NULL); + i++; + } return (0); }