Matthos Denys 02ffa0e76a normed
2021-05-20 12:54:15 +03:00

59 lines
1.6 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* observer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:50 by mdenys #+# #+# */
/* Updated: 2021/05/20 12:22:45 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/philo_header.h"
int run_observer_return(t_all *all, int i)
{
printf("%ld %d died\n", \
ft_get_time() - all->start_time, i + 1);
all->smert = 42;
return (2);
}
int run_observer(t_all *all)
{
int i;
time_t time_eat;
i = 0;
while (1)
{
i = 0;
while (i < all->nbr_phils)
{
if (all->smert != 42)
{
time_eat = (ft_get_time() - \
all->start_time) - all->data_philo->philors[i].last_eat;
if (time_eat > all->time_to_die)
return (run_observer_return(all, i));
}
else
return (ft_exit(all, 2));
i++;
}
ft_msleep(1);
}
return (0);
}
int runner(t_all *all)
{
prepare_resources(all);
all->start_time = ft_get_time();
run_all_phils(all);
if (!run_observer(all))
return (0);
return (1);
}