Files
42_INT_08_philosophers/srcs/generic.c
2022-01-22 19:19:39 +01:00

44 lines
1.1 KiB
C

#include "philo.h"
void init_time(t_philo *philo)
{
struct timeval stime;
if (philo->global->t_start_s == 0)
{
gettimeofday(&stime, NULL);
philo->global->t_start_s = stime.tv_sec;
philo->global->t_start_u = stime.tv_usec;
}
}
void update_time(t_philo *philo)
{
struct timeval stime;
gettimeofday(&stime, NULL);
philo->t_last_meal_s = stime.tv_sec;
philo->t_last_meal_u = stime.tv_usec;
}
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_s) * 1000;
time_stamp += (stime.tv_usec - philo->global->t_start_u) / 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));
}