42_Philosopher/time.c
Camille Chauvet 796e186c36 clean: norm
2023-04-27 12:05:14 +00:00

43 lines
1.5 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* time.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:35:26 by cchauvet #+# #+# */
/* Updated: 2023/04/27 11:54:04 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include <bits/types/struct_timeval.h>
#include <sys/time.h>
#include <stddef.h>
#include <pthread.h>
#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_perfect(t_philo *philo, t_data *data)
{
size_t time;
time = get_time();
if (time - philo->last_eat + data->eat_time > data->life_expectency)
return ((time - philo->last_eat + data->eat_time));
else
return (data->eat_time);
}