/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* time.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cchauvet #include #include #include #include "./philo.h" #include "data.h" size_t get_time(void) { size_t time; static size_t start_time = 0; struct timeval tv; gettimeofday(&tv, NULL); time = tv.tv_sec * 1000000 + tv.tv_usec; if (start_time == 0) start_time = time; return ((time - start_time) / 1000); } size_t get_time_eat(t_philo *philo, t_data *data) { size_t time; size_t value; time = get_time(); value = (data->life_expectency - philo->last_eat - time); if (value > data->eat_time) value = data->eat_time; // printf("\n%zu: time: %zu, last: %zu, eat: %zu, expectencty: %zu, value: %zu\n", philo->id + 1, time, philo->last_eat, data->eat_time, data->life_expectency, value); return (value); } size_t get_time_sleep(t_philo *philo, t_data *data) { size_t time; size_t value; time = get_time(); value = (data->life_expectency - (time - philo->last_eat)); if (value > data->sleep_time) value = data->sleep_time; // printf("\n%zu: time: %zu, last: %zu, sleep: %zu, expectencty: %zu, value: %zu\n", philo->id + 1, time, philo->last_eat, data->sleep_time, data->life_expectency, value); return (value); }