From 3ee91785f6a812bd6669fd10f893e20eca221a5b Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Fri, 21 Jan 2022 12:05:52 +0100 Subject: [PATCH] routine des philosophers ok et debut usage time --- headers/philo.h | 1 + headers/philo_struct.h | 6 ++- srcs/exec.c | 93 +++++++++++++++++++++++------------------- srcs/init.c | 2 +- 4 files changed, 57 insertions(+), 45 deletions(-) diff --git a/headers/philo.h b/headers/philo.h index 074f399..5b15273 100644 --- a/headers/philo.h +++ b/headers/philo.h @@ -7,6 +7,7 @@ # include # include # include +# include # include "philo_struct.h" # include "philo_proto.h" diff --git a/headers/philo_struct.h b/headers/philo_struct.h index 7084cf8..71b1421 100644 --- a/headers/philo_struct.h +++ b/headers/philo_struct.h @@ -15,9 +15,13 @@ typedef struct s_params typedef struct s_philo { t_params *params; - int philo_nbr; + int p_nbr; t_mtx m_fork; t_mtx *m_print; + long int t_start_s; + long int t_start_u; + long int t_last_meal_s; + long int t_last_meal_u; struct s_philo *prev; struct s_philo *next; } t_philo; diff --git a/srcs/exec.c b/srcs/exec.c index 9541523..4cd665b 100644 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -3,66 +3,73 @@ void print_message(t_philo *philo, char *msg) { pthread_mutex_lock(philo->m_print); - ft_printf("%i %s\n", philo->philo_nbr, msg); + ft_printf("%i %s\n", philo->p_nbr, msg); pthread_mutex_unlock(philo->m_print); } +// long int t_start_s; +// long int t_start_u; +// long int t_last_meal_s; +// long int t_last_meal_u; +void init_time(t_philo *philo) +{ + struct timeval stime; + + gettimeofday(&stime, NULL); + philo->t_start_s = stime.tv_sec; + philo->t_start_u = stime.tv_usec; +} + +void update_time(t_philo *philo) +{ + struct timeval stime; +// long int time_stamp; + + gettimeofday(&stime, NULL); + philo->t_last_meal_s = stime.tv_sec; + philo->t_last_meal_u = stime.tv_usec; +// time_stamp = (stime.tv_sec - scd) * 1000 + (stime.tv_usec - mcr) / 1000; +} + void take_forks(t_philo *philo) { pthread_mutex_lock(&(philo->m_fork)); - pthread_mutex_lock(&(philo->next->m_fork)); - print_message(philo, "has taken a fork"); - usleep(philo->params->t_eat); + pthread_mutex_lock(&(philo->next->m_fork)); + print_message(philo, "has taken a fork"); + + print_message(philo, "is eating"); + usleep(philo->params->t_eat * 1000); pthread_mutex_unlock(&(philo->next->m_fork)); pthread_mutex_unlock(&(philo->m_fork)); } +// print_message(philo, "has taken a fork"); +// print_message(philo, "is eating"); +// print_message(philo, "is thinking"); +// print_message(philo, "is sleeping"); +// +// int n_phi; // number_of_philosophers +// int t_die; // time_to_die +// int t_eat; // time_to_eat +// int t_slp; // time_to_sleep +// int n_eat; // [number_of_times_each_philosopher_must_eat] void *philo_exec(void *arg) { t_philo *philo; philo = (t_philo*)arg; - print_message(philo, "is thinking"); - take_forks(philo); + init_time(philo); + if (philo->p_nbr % 2 == 0) + usleep(10 * 1000); + while (1) + { + take_forks(philo); - -// eat - // "has taken a fork" - // "is eating" -// sleep - // "is sleeping" -// think - // "is thinking" -// die - // "died" -//int i = 0; -// while (i < 2) -// { -// if (!take_forks(philo)) -// continue ; -// -// usleep(philo->params->t_eat); -// -// pthread_mutex_lock(&mutex); -// philo->fork = 1; -// philo->next->fork = 1; -// ft_printf("%i is sleeping\n", philo->philo_nbr); -// pthread_mutex_unlock(&mutex); -// -// usleep(philo->params->t_slp); -// -// pthread_mutex_lock(&mutex); -// ft_printf("%i is thinking\n", philo->philo_nbr); -// pthread_mutex_unlock(&mutex); - -// pthread_mutex_lock(philo->mutex); -// pthread_mutex_lock(&mutex); -// ft_printf("%i is thinking\n", philo->philo_nbr); -// pthread_mutex_unlock(philo->mutex); -// pthread_mutex_unlock(&mutex); -// i++; -// } + print_message(philo, "is sleeping"); + usleep(philo->params->t_slp * 1000); + print_message(philo, "is sleeping"); + } return (NULL); } diff --git a/srcs/init.c b/srcs/init.c index c3a0b27..48dee44 100644 --- a/srcs/init.c +++ b/srcs/init.c @@ -8,7 +8,7 @@ t_philo *lst_add_philo(t_philo *philo, t_params *params, t_mtx *m_print, int i) if (new == NULL) return (NULL); new->params = params; - new->philo_nbr = i + 1; + new->p_nbr = i + 1; if (pthread_mutex_init(&(new->m_fork), NULL) != 0) return (NULL); new->m_print = m_print;