From 6561340fb97026934871ec8d282cf00a1215b3de Mon Sep 17 00:00:00 2001 From: hugogogo Date: Wed, 26 Jan 2022 10:52:06 +0100 Subject: [PATCH] check died dans un autre thread et reorganisation fichiers avec main thread --- Makefile | 1 + headers/philo_proto.h | 9 +++++--- headers/philo_struct.h | 1 + srcs/exec.c | 14 +++++++----- srcs/generic.c | 43 +++++----------------------------- srcs/init.c | 1 + srcs/main.c | 13 ----------- srcs/main_thread.c | 52 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 75 insertions(+), 59 deletions(-) create mode 100644 srcs/main_thread.c diff --git a/Makefile b/Makefile index 0a4a811..18eb30a 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,7 @@ LIBFT = $(LIBFT_D)/libft.a SRCS = main.c \ init.c \ + main_thread.c \ exec.c \ generic.c diff --git a/headers/philo_proto.h b/headers/philo_proto.h index 8217f97..46276a8 100644 --- a/headers/philo_proto.h +++ b/headers/philo_proto.h @@ -4,15 +4,18 @@ // init.c 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); + // exec.c 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, struct timeval stime); -int print_message(t_philo *philo, char *clr, char *msg); +int print_message(t_philo *philo, char *clr, char *msg, int key); #endif diff --git a/headers/philo_struct.h b/headers/philo_struct.h index 87ec596..dd4518c 100644 --- a/headers/philo_struct.h +++ b/headers/philo_struct.h @@ -21,6 +21,7 @@ typedef struct s_params typedef struct s_global { int dead; + int n_eat; t_time t_start; t_mtx m_print; t_mtx m_dead; diff --git a/srcs/exec.c b/srcs/exec.c index 72e9b14..5c91235 100644 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -25,16 +25,18 @@ int ret_err_unlock(t_philo *philo, int nbr_fork) int eat(t_philo *philo) { pthread_mutex_lock(&(philo->m_fork)); - if (print_message(philo, WHITE, "has taken a fork") == 1) + if (print_message(philo, WHITE, "has taken a fork", philo->global->dead)) return (ret_err_unlock(philo, 1)); pthread_mutex_lock(&(philo->next->m_fork)); - if (print_message(philo, WHITE, "has taken a fork") == 1) + if (print_message(philo, WHITE, "has taken a fork", philo->global->dead)) return (ret_err_unlock(philo, 2)); update_time(philo); - if (print_message(philo, B_YELLOW, "is eating") == 1) + if (print_message(philo, B_YELLOW, "is eating", philo->global->dead)) 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); pthread_mutex_unlock(&(philo->next->m_fork)); @@ -49,15 +51,15 @@ void *philo_exec(void *arg) philo = (t_philo*)arg; init_time(philo); if (philo->p_nbr % 2 == 0) - usleep(10 * 1000); + usleep(10); while (1) { if (eat(philo) != 0) break ; - if (print_message(philo, B_BLUE, "is sleeping") == 1) + if (print_message(philo, B_BLUE, "is sleeping", philo->global->dead)) break ; go_sleep(philo, philo->params->t_slp); - if (print_message(philo, B_GREEN, "is thinking") == 1) + if (print_message(philo, B_GREEN, "is thinking", philo->global->dead)) break ; } return (NULL); diff --git a/srcs/generic.c b/srcs/generic.c index c05574e..134f74b 100644 --- a/srcs/generic.c +++ b/srcs/generic.c @@ -21,51 +21,20 @@ 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 is_dead(t_philo *philo, struct timeval stime) -{ - int time; - - if (philo->global->dead) - return (-1); - if (philo->params->n_eat != -1 && philo->eat >= philo->params->n_eat) - { - philo->global->dead = 1; - return (philo->p_nbr); - } - time = diff_time(philo->t_last_meal, stime); - if (time >= philo->params->t_die) - { - philo->global->dead = 1; - return (philo->p_nbr); - } - return (0); -} - -int print_message(t_philo *philo, char *clr, char *msg) +int print_message(t_philo *philo, char *clr, char *msg, int key) { long int time_stamp; struct timeval stime; - char p_dead; + if (key) + return (1); 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; - if (p_dead != 0) - { - if (p_dead > 0) - ft_printf("%s%i %i died%s\n", B_RED, time_stamp, p_dead, RESET); - pthread_mutex_unlock(&(philo->global->m_print)); - return (1); - } - else - ft_printf("%s%i %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET); + ft_printf("%s%i %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET); pthread_mutex_unlock(&(philo->global->m_print)); + if (philo->global->dead) + return (1); return (0); } diff --git a/srcs/init.c b/srcs/init.c index 520ca91..4ffb585 100644 --- a/srcs/init.c +++ b/srcs/init.c @@ -72,6 +72,7 @@ t_global *init_global(void) if (!global) return (NULL); global->dead = 0; + global->n_eat = 0; global->t_start.ts = 0; global->t_start.tu = 0; if (pthread_mutex_init(&(global->m_print), NULL) != 0) diff --git a/srcs/main.c b/srcs/main.c index 13d1ee7..4eb6e7b 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -1,18 +1,5 @@ #include "philo.h" -void launch_threads(t_philo *philo, pthread_t *id) -{ - int i; - - i = 0; - while (i < philo->params->n_phi) - { - pthread_create(&id[i], NULL, &philo_exec, philo); - philo = philo->next; - i++; - } -} - int main(int ac, char **av) { pthread_t *id; diff --git a/srcs/main_thread.c b/srcs/main_thread.c new file mode 100644 index 0000000..3b654b8 --- /dev/null +++ b/srcs/main_thread.c @@ -0,0 +1,52 @@ +#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; + int time; + + if (philo->global->n_eat == philo->params->n_phi) + { + philo->global->dead = 1; + 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, B_RED, "died", 0); + return (1); + } + return (0); +} + +void launch_threads(t_philo *philo, pthread_t *id) +{ + int i; + + i = 0; + while (i < philo->params->n_phi) + { + pthread_create(&id[i], NULL, &philo_exec, philo); + philo = philo->next; + i++; + } + while (philo->global->dead == 0) + { + is_dead(philo); + philo = philo->next; + } + i = 0; + while (i < philo->params->n_phi) + { + pthread_create(&id[i], NULL, &philo_exec, philo); + philo = philo->next; + i++; + } +}