2024-07-09 13:18:59 +02:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
2024-08-12 20:52:02 +02:00
|
|
|
/* monitor.c :+: :+: :+: */
|
2024-07-09 13:18:59 +02:00
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
2024-08-12 20:52:02 +02:00
|
|
|
/* Created: 2024/08/12 12:13:35 by adjoly #+# #+# */
|
2024-08-19 23:19:48 +02:00
|
|
|
/* Updated: 2024/08/19 23:19:17 by adjoly ### ########.fr */
|
2024-07-09 13:18:59 +02:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
2024-07-21 20:55:35 +02:00
|
|
|
#include "philo.h"
|
|
|
|
|
2024-08-12 20:52:02 +02:00
|
|
|
bool philo_check(t_philo *philo, t_pdata data)
|
2024-07-09 13:18:59 +02:00
|
|
|
{
|
2024-08-06 17:42:36 +02:00
|
|
|
uint8_t i;
|
2024-08-12 20:52:02 +02:00
|
|
|
struct timeval t1;
|
|
|
|
uint8_t eaten;
|
2024-07-09 13:18:59 +02:00
|
|
|
|
2024-07-21 20:55:35 +02:00
|
|
|
i = 0;
|
2024-08-12 20:52:02 +02:00
|
|
|
eaten = 0;
|
2024-07-23 18:11:44 +02:00
|
|
|
while (i < data.philo_nbr)
|
2024-07-09 13:18:59 +02:00
|
|
|
{
|
2024-08-12 21:06:38 +02:00
|
|
|
|
2024-08-12 20:52:02 +02:00
|
|
|
gettimeofday(&t1, NULL);
|
2024-08-12 21:06:38 +02:00
|
|
|
pthread_mutex_lock(philo->check);
|
2024-08-19 23:19:48 +02:00
|
|
|
if (get_time_in_ms(philo[i].eat, t1) > data.die_time + 1)
|
2024-08-12 20:52:02 +02:00
|
|
|
{
|
2024-08-12 21:06:38 +02:00
|
|
|
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]));
|
|
|
|
}
|
2024-08-12 21:06:38 +02:00
|
|
|
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);
|
2024-07-22 15:06:51 +02:00
|
|
|
i++;
|
2024-07-09 13:18:59 +02:00
|
|
|
}
|
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);
|
2024-07-09 13:18:59 +02:00
|
|
|
}
|