1
0
This repository has been archived on 2024-10-25. You can view files and clone it, but cannot push or open issues or pull requests.
philosopher/philo/monitor.c

53 lines
1.7 KiB
C
Raw Normal View History

/* ************************************************************************** */
/* */
/* ::: :::::::: */
2024-08-12 20:52:02 +02:00
/* monitor.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
2024-08-12 20:52:02 +02:00
/* Created: 2024/08/12 12:13:35 by adjoly #+# #+# */
/* Updated: 2024/08/12 21:00:27 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
2024-08-12 20:52:02 +02:00
bool philo_check(t_philo *philo, t_pdata data)
{
uint8_t i;
2024-08-12 20:52:02 +02:00
struct timeval t1;
uint8_t eaten;
i = 0;
2024-08-12 20:52:02 +02:00
eaten = 0;
while (i < data.philo_nbr)
{
2024-08-12 20:52:02 +02:00
gettimeofday(&t1, NULL);
pthread_mutex_lock(philo->check);
2024-08-12 20:52:02 +02:00
if (get_time_in_ms(philo[i].eat, t1) > data.die_time)
{
pthread_mutex_unlock(philo->check);
2024-08-12 20:52:02 +02:00
pthread_mutex_unlock(&philo[i].fork.left);
pthread_mutex_unlock(philo[i].fork.right);
return (print_death(&philo[i]));
}
pthread_mutex_unlock(philo->check);
2024-08-12 20:52:02 +02:00
if (philo[i].meal_left == 0)
eaten++;
if (get_death(philo, RETURN))
return (true);
i++;
}
2024-08-12 20:52:02 +02:00
if (eaten == data.philo_nbr)
return (true);
return (false);
}
void monitor(t_philo *philo, t_pdata data)
{
while (!philo_check(philo, data))
usleep(100);
get_death(philo, true);
}