From d8475537e04579fcd1d1578ca1f099bdedd116cd Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Tue, 25 Jan 2022 18:48:49 +0100 Subject: [PATCH] nimp dans timestamp --- headers/philo_proto.h | 2 + headers/philo_struct.h | 13 ++++-- srcs/exec.c | 90 +++++++++++++++++++++++++++++++----------- srcs/generic.c | 37 +++++++++++++---- srcs/init.c | 10 ++--- srcs/main.c | 9 +---- 6 files changed, 115 insertions(+), 46 deletions(-) diff --git a/headers/philo_proto.h b/headers/philo_proto.h index 0bc73b1..561ec4e 100644 --- a/headers/philo_proto.h +++ b/headers/philo_proto.h @@ -10,6 +10,8 @@ void *philo_exec(void *arg); // generic.c 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); #endif diff --git a/headers/philo_struct.h b/headers/philo_struct.h index afa245c..87ec596 100644 --- a/headers/philo_struct.h +++ b/headers/philo_struct.h @@ -3,6 +3,12 @@ typedef pthread_mutex_t t_mtx; +typedef struct s_time +{ + long int ts; + long int tu; +} t_time; + typedef struct s_params { int n_phi; // number_of_philosophers @@ -15,8 +21,7 @@ typedef struct s_params typedef struct s_global { int dead; - long int t_start_s; - long int t_start_u; + t_time t_start; t_mtx m_print; t_mtx m_dead; } t_global; @@ -27,8 +32,8 @@ typedef struct s_philo t_global *global; int p_nbr; t_mtx m_fork; - long int t_last_meal_s; - long int t_last_meal_u; + t_time t_last_meal; + int eat; struct s_philo *next; } t_philo; diff --git a/srcs/exec.c b/srcs/exec.c index d4bdfee..c7070c9 100644 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -1,46 +1,88 @@ #include "philo.h" -// TODO : compare time with time before dying -void go_sleep(t_philo *philo, int time) +void go_sleep(t_philo *philo, int action_time) { - long int start_time; - long int death_time; - long int next_step_time; + struct timeval stime; + int time_to_death; - start_time = philo->t_last_meal_u; - if (start_time == 0) - start_time = philo->global->t_start_u; + // 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 + // + // - death_time = start_time + (philo->params->t_die * 1000); - next_step_time = start_time + (time * 1000); - if (death_time < next_step_time) + gettimeofday(&stime, NULL); + time_to_death = diff_time(philo->t_last_meal, stime); + time_to_death = philo->params->t_die - time_to_death; + if (time_to_death > action_time) + usleep(action_time * 1000); + else if (time_to_death > 0) usleep(philo->params->t_die * 1000); - else - usleep(time * 1000); } -int ret_unlock(t_mtx *mutex_1, t_mtx *mutex_2, int ret) +int ret_unlock(t_philo *philo, int nbr_fork, int ret) { - if (mutex_1) - pthread_mutex_unlock(mutex_1); - if (mutex_2) - pthread_mutex_unlock(mutex_2); + pthread_mutex_unlock(&philo->m_fork); + if (nbr_fork == 2) + pthread_mutex_unlock(&philo->next->m_fork); return (ret); } int take_forks(t_philo *philo) { pthread_mutex_lock(&(philo->m_fork)); - if (philo->global->dead != 0) - return (ret_unlock(&(philo->m_fork), NULL, 1)); + if (is_dead(philo)) + return (ret_unlock(philo, 1, 1)); print_message(philo, "has taken a fork"); pthread_mutex_lock(&(philo->next->m_fork)); - if (philo->global->dead != 0) - return (ret_unlock(&(philo->m_fork), &(philo->next->m_fork), 1)); + if (is_dead(philo)) + return (ret_unlock(philo, 2, 1)); print_message(philo, "has taken a fork"); update_time(philo); print_message(philo, "is eating"); + 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)); @@ -60,8 +102,12 @@ void *philo_exec(void *arg) { if (take_forks(philo) != 0) break ; + if (is_dead(philo)) + break ; print_message(philo, "is sleeping"); go_sleep(philo, philo->params->t_slp); + if (is_dead(philo)) + break ; print_message(philo, "is thinking"); } return (NULL); diff --git a/srcs/generic.c b/srcs/generic.c index 777ac95..5b49c30 100644 --- a/srcs/generic.c +++ b/srcs/generic.c @@ -4,11 +4,11 @@ void init_time(t_philo *philo) { struct timeval stime; - if (philo->global->t_start_s == 0) + if (philo->global->t_start.ts == 0) { gettimeofday(&stime, NULL); - philo->global->t_start_s = stime.tv_sec; - philo->global->t_start_u = stime.tv_usec; + philo->global->t_start.ts = stime.tv_sec; + philo->global->t_start.tu = stime.tv_usec; } } @@ -17,8 +17,31 @@ void update_time(t_philo *philo) struct timeval stime; gettimeofday(&stime, NULL); - philo->t_last_meal_s = stime.tv_sec; - philo->t_last_meal_u = stime.tv_usec; + philo->t_last_meal.ts = stime.tv_sec; + philo->t_last_meal.tu = stime.tv_usec; +} + +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) +{ + struct timeval stime; + int time; + + if (philo->global->dead) + return (1); + gettimeofday(&stime, NULL); + 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 (0); } void print_message(t_philo *philo, char *msg) @@ -28,8 +51,8 @@ void print_message(t_philo *philo, char *msg) char *color; gettimeofday(&stime, NULL); - time_stamp = (stime.tv_sec - philo->global->t_start_s) * 1000; - time_stamp += (stime.tv_usec - philo->global->t_start_u) / 1000; + 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; diff --git a/srcs/init.c b/srcs/init.c index af61d88..5d4e1de 100644 --- a/srcs/init.c +++ b/srcs/init.c @@ -12,13 +12,13 @@ t_philo *lst_add_philo(t_params *params, t_global *global, int i) new->p_nbr = i + 1; if (pthread_mutex_init(&(new->m_fork), NULL) != 0) return (NULL); - new->t_last_meal_s = 0; - new->t_last_meal_u = 0; + new->t_last_meal.ts = 0; + new->t_last_meal.tu = 0; + new->eat = 0; new->next = NULL; return (new); } -// looping chained list t_philo *init_chain_philo(t_params *params, t_global *global) { t_philo *philo; @@ -71,8 +71,8 @@ t_global *init_global(void) if (!global) return (NULL); global->dead = 0; - global->t_start_s = 0; - global->t_start_u = 0; + global->t_start.ts = 0; + global->t_start.tu = 0; if (pthread_mutex_init(&(global->m_print), NULL) != 0) return (NULL); if (pthread_mutex_init(&(global->m_dead), NULL) != 0) diff --git a/srcs/main.c b/srcs/main.c index ebc2ff6..eb90891 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -13,15 +13,9 @@ void launch_threads(t_philo *philo, pthread_t *id) } } -int is_dead(t_philo *philo) -{ - (void)philo; - return (0); -} - void watch_threads(t_philo *philo, pthread_t *id) { - int i; + int i; i = 0; while (1) @@ -46,6 +40,5 @@ int main(int ac, char **av) return (0); launch_threads(philo, id); watch_threads(philo, id); -write(1, "main function\n", 14); return (0); }