From bf80fde30b28a58bbd7f6a5e27fdd4fb3fbe3880 Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Wed, 26 Jan 2022 14:49:02 +0100 Subject: [PATCH] 1 philo ok, deplacement diff_time dans generic, renomme go_sleep, ferme lock en fin --- headers/philo_proto.h | 2 +- srcs/exec.c | 11 ++++------- srcs/generic.c | 7 +++++++ srcs/main.c | 7 +++++++ srcs/main_thread.c | 7 +------ 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/headers/philo_proto.h b/headers/philo_proto.h index 21b95f4..b2d9668 100644 --- a/headers/philo_proto.h +++ b/headers/philo_proto.h @@ -5,7 +5,6 @@ t_philo *init(int ac, char **av, pthread_t **id); // main_thread.c -int diff_time(t_time old, struct timeval new); int is_dead(t_philo *philo); void launch_threads(t_philo *philo, pthread_t *id); @@ -15,6 +14,7 @@ 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 new); int print_message(t_philo *philo, char *clr, char *msg); #endif diff --git a/srcs/exec.c b/srcs/exec.c index 630bdb1..6b11454 100644 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -1,13 +1,12 @@ #include "philo.h" -void go_sleep(t_philo *philo, int action_time) +void action_delay(t_philo *philo, int action_time) { struct timeval stime; int 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; + time_to_death = philo->params->t_die - diff_time(philo->t_last_meal, stime); if (time_to_death > action_time) usleep(action_time * 1000); else if (time_to_death > 0) @@ -30,15 +29,13 @@ int eat(t_philo *philo) pthread_mutex_lock(&(philo->next->m_fork)); if (print_message(philo, WHITE, "has taken a fork")) return (ret_err_unlock(philo, 2)); - update_time(philo); if (print_message(philo, B_YELLOW, "is eating")) return (ret_err_unlock(philo, 2)); philo->eat++; if (philo->eat == philo->params->n_eat) philo->global->n_eat++; - go_sleep(philo, philo->params->t_eat); - + action_delay(philo, philo->params->t_eat); pthread_mutex_unlock(&(philo->next->m_fork)); pthread_mutex_unlock(&(philo->m_fork)); return (0); @@ -58,7 +55,7 @@ void *philo_exec(void *arg) break ; if (print_message(philo, B_BLUE, "is sleeping")) break ; - go_sleep(philo, philo->params->t_slp); + action_delay(philo, philo->params->t_slp); if (print_message(philo, B_GREEN, "is thinking")) break ; } diff --git a/srcs/generic.c b/srcs/generic.c index d856ba3..8629fd8 100644 --- a/srcs/generic.c +++ b/srcs/generic.c @@ -10,6 +10,8 @@ void init_time(t_philo *philo) philo->global->t_start.ts = stime.tv_sec; philo->global->t_start.tu = stime.tv_usec; } + philo->t_last_meal.ts = philo->global->t_start.ts; + philo->t_last_meal.tu = philo->global->t_start.tu; } void update_time(t_philo *philo) @@ -21,6 +23,11 @@ void update_time(t_philo *philo) 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 print_message(t_philo *philo, char *clr, char *msg) { long int time_stamp; diff --git a/srcs/main.c b/srcs/main.c index 4eb6e7b..b97cb36 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -16,5 +16,12 @@ int main(int ac, char **av) pthread_join(id[i], NULL); i++; } + i = 0; + while (i < philo->params->n_phi) + { + pthread_create(&id[i], NULL, &philo_exec, philo); + philo = philo->next; + i++; + } return (0); } diff --git a/srcs/main_thread.c b/srcs/main_thread.c index aa778e1..ad3eb93 100644 --- a/srcs/main_thread.c +++ b/srcs/main_thread.c @@ -1,10 +1,5 @@ #include "philo.h" -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; @@ -51,7 +46,7 @@ void launch_threads(t_philo *philo, pthread_t *id) i = 0; while (i < philo->params->n_phi) { - pthread_create(&id[i], NULL, &philo_exec, philo); + pthread_mutex_unlock(&(philo->m_fork)); philo = philo->next; i++; }