41 lines
937 B
C
41 lines
937 B
C
#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 print_message(t_philo *philo, char *clr, char *msg, int key)
|
|
{
|
|
long int time_stamp;
|
|
struct timeval stime;
|
|
|
|
if (key)
|
|
return (1);
|
|
pthread_mutex_lock(&(philo->global->m_print));
|
|
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);
|
|
}
|