Matthos Denys 5f1937beec update
2021-05-18 21:36:56 +03:00

98 lines
2.1 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* logic.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/18 21:18:08 by mdenys #+# #+# */
/* Updated: 2021/05/18 21:18:08 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/philo_header.h"
int check_live(t_all *all)
{
int i;
i = 0;
while (i < all->nbr_phls)
{
if (all->data->philors[i].died)
{
return (1);
}
i++;
}
return (0);
}
void *run_trhead(t_philo *philo)
{
while (philo->all_struct->smert != 42)
{
if (philo->all_struct->opt)
{
if (philo->count_eat == philo->all_struct->iteration_eat)
{
philo->all_struct->smert = 42;
}
}
cycle(philo);
}
return (NULL);
}
int run_observer42(t_all *all, int i)
{
time_t time_eat;
pthread_mutex_lock(&all->data->philors[i].eat_mutex);
time_eat = (ft_get_time() - all->start_time) \
- all->data->philors[i].last_eat;
if (time_eat > all->time_to_die)
{
printf("%ld %d died\n", \
ft_get_time() - all->start_time, i + 1);
return (2);
}
pthread_mutex_unlock(&all->data->philors[i].eat_mutex);
return (0);
}
int run_observer(t_all *all)
{
int i;
i = 0;
while (1)
{
i = 0;
while (i < all->nbr_phls)
{
if (all->smert != 42)
{
if (run_observer42(all, i) == 2)
{
return (2);
}
}
else
return (ft_exit(all));
i++;
}
}
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);
}