philosophers_prep/rendu/simple_philosophers.h

51 lines
1.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* simple_philosophers.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ruiferna <ruiferna@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/09 18:23:48 by ruiferna #+# #+# */
/* Updated: 2025/10/09 18:24:44 by ruiferna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SIMPLE_PHILOSOPHERS_H
# define SIMPLE_PHILOSOPHERS_H
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
long long get_current_time()
{
struct timeval tv;
gettimeofday(&tv,NULL);
/*
A stuct tv tem tv_sec que sao os segundos desde a epoch e temos a tv_usec
que retorna os microsegundos ate completar 1 segundo (um milhão de microssegundos = 1 segundo).
Por isso, para conseguirmos realmente medir os milisegundos precisamos de juntar os dois
*/
return ((tv.tv_sec * 1000LL) + (tv.tv_usec / 1000LL));
}
long long time_diff(long long start, long long end)
{
return (end - start);
}
void precise_sleep(int ms)
{
long long start_time;
start_time = get_current_time();
while ((get_current_time() - start_time) < ms)
{
usleep(100);
}
}
#endif