philosophers/philo/src/check_philos.c
2025-01-28 17:24:01 +01:00

42 lines
1.6 KiB
C

/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* check_philos.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2025/01/27 14:13:50 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2025/01/27 14:13:51 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "philo.h"
int check_philos(t_rules *rules)
{
int i;
int min_n_eat;
min_n_eat = INT_MAX;
i = 0;
while (i < rules->n_philos)
{
pthread_mutex_lock(rules->philos[i].n_eat_lock);
min_n_eat = ft_min(rules->philos[i].n_eat, min_n_eat);
pthread_mutex_unlock(rules->philos[i].n_eat_lock);
pthread_mutex_lock(rules->philos[i].last_meal_lock);
if (get_time() - rules->philos[i].last_meal >= rules->time_to_die)
{
philo_die(&rules->philos[i]);
set_finished(rules);
pthread_mutex_unlock(rules->philos[i].last_meal_lock);
return (FAILURE);
}
pthread_mutex_unlock(rules->philos[i].last_meal_lock);
i++;
}
if (min_n_eat >= rules->n_must_eat)
return (set_finished(rules), FAILURE);
return (SUCCESS);
}