diff --git a/headers/philo_proto.h b/headers/philo_proto.h index 46276a8..21b95f4 100644 --- a/headers/philo_proto.h +++ b/headers/philo_proto.h @@ -15,7 +15,7 @@ void *philo_exec(void *arg); // generic.c void init_time(t_philo *philo); void update_time(t_philo *philo); -int print_message(t_philo *philo, char *clr, char *msg, int key); +int print_message(t_philo *philo, char *clr, char *msg); #endif diff --git a/srcs/exec.c b/srcs/exec.c index 5c91235..630bdb1 100644 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -25,14 +25,14 @@ 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", philo->global->dead)) + if (print_message(philo, WHITE, "has taken a fork")) return (ret_err_unlock(philo, 1)); pthread_mutex_lock(&(philo->next->m_fork)); - if (print_message(philo, WHITE, "has taken a fork", philo->global->dead)) + 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", philo->global->dead)) + if (print_message(philo, B_YELLOW, "is eating")) return (ret_err_unlock(philo, 2)); philo->eat++; if (philo->eat == philo->params->n_eat) @@ -51,15 +51,15 @@ void *philo_exec(void *arg) philo = (t_philo*)arg; init_time(philo); if (philo->p_nbr % 2 == 0) - usleep(10); + usleep(10 * 1000); while (1) { if (eat(philo) != 0) break ; - if (print_message(philo, B_BLUE, "is sleeping", philo->global->dead)) + if (print_message(philo, B_BLUE, "is sleeping")) break ; go_sleep(philo, philo->params->t_slp); - if (print_message(philo, B_GREEN, "is thinking", philo->global->dead)) + if (print_message(philo, B_GREEN, "is thinking")) break ; } return (NULL); diff --git a/srcs/generic.c b/srcs/generic.c index 134f74b..d856ba3 100644 --- a/srcs/generic.c +++ b/srcs/generic.c @@ -21,20 +21,21 @@ void update_time(t_philo *philo) philo->t_last_meal.tu = stime.tv_usec; } -int print_message(t_philo *philo, char *clr, char *msg, int key) +int print_message(t_philo *philo, char *clr, char *msg) { long int time_stamp; struct timeval stime; - if (key) - return (1); pthread_mutex_lock(&(philo->global->m_print)); + if (philo->global->dead) + { + pthread_mutex_unlock(&(philo->global->m_print)); + return (1); + } gettimeofday(&stime, NULL); time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000; time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000; 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/main_thread.c b/srcs/main_thread.c index 3b654b8..aa778e1 100644 --- a/srcs/main_thread.c +++ b/srcs/main_thread.c @@ -8,6 +8,7 @@ int diff_time(t_time old, struct timeval new) int is_dead(t_philo *philo) { struct timeval stime; + long int time_stamp; int time; if (philo->global->n_eat == philo->params->n_phi) @@ -19,8 +20,13 @@ int is_dead(t_philo *philo) time = diff_time(philo->t_last_meal, stime); if (time >= philo->params->t_die) { + pthread_mutex_lock(&(philo->global->m_print)); philo->global->dead = 1; - print_message(philo, B_RED, "died", 0); + + time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000; + time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000; + ft_printf("%s%i %i died%s\n", B_RED, time_stamp, philo->p_nbr, RESET); + pthread_mutex_unlock(&(philo->global->m_print)); return (1); } return (0);