#include "philo.h" void init_time(t_philo *philo) { struct timeval stime; if (philo->global->t_start.ts == 0) { gettimeofday(&stime, NULL); philo->global->t_start.ts = stime.tv_sec; philo->global->t_start.tu = stime.tv_usec; } } void update_time(t_philo *philo) { struct timeval stime; gettimeofday(&stime, NULL); 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) { long int time_stamp; struct timeval stime; char *color; 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; color = WHITE; if (ft_strnstr(msg, "eating", ft_strlen(msg))) color = B_YELLOW; else if (ft_strnstr(msg, "sleeping", ft_strlen(msg))) color = B_BLUE; else if (ft_strnstr(msg, "thinking", ft_strlen(msg))) color = B_GREEN; pthread_mutex_lock(&(philo->global->m_print)); ft_printf("%s%i %i %s%s\n", color, time_stamp, philo->p_nbr, msg, RESET); pthread_mutex_unlock(&(philo->global->m_print)); }