gestion cas n_eat a zero

This commit is contained in:
Hugo LAMY
2022-01-26 18:58:39 +01:00
parent 232684b73a
commit e45ca1331f
4 changed files with 27 additions and 15 deletions

View File

@@ -6,7 +6,7 @@
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */ /* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:28 by hulamy #+# #+# */ /* Created: 2022/01/26 15:30:28 by hulamy #+# #+# */
/* Updated: 2022/01/26 16:29:49 by hulamy ### ########.fr */ /* Updated: 2022/01/26 18:51:44 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -44,10 +44,8 @@ static int eat(t_philo *philo)
update_time(philo); update_time(philo);
if (print_message(philo, B_YELLOW, "is eating")) if (print_message(philo, B_YELLOW, "is eating"))
return (ret_err_unlock(philo, 2)); return (ret_err_unlock(philo, 2));
philo->eat_count++;
if (philo->eat_count == philo->params->n_eat)
philo->global->satiated_count++;
action_delay(philo, philo->params->t_eat); action_delay(philo, philo->params->t_eat);
philo->eat_count++;
pthread_mutex_unlock(&(philo->next->m_fork)); pthread_mutex_unlock(&(philo->next->m_fork));
pthread_mutex_unlock(&(philo->m_fork)); pthread_mutex_unlock(&(philo->m_fork));
return (0); return (0);

View File

@@ -6,7 +6,7 @@
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */ /* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:34 by hulamy #+# #+# */ /* Created: 2022/01/26 15:30:34 by hulamy #+# #+# */
/* Updated: 2022/01/26 17:55:34 by hulamy ### ########.fr */ /* Updated: 2022/01/26 18:15:42 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */ /* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:49 by hulamy #+# #+# */ /* Created: 2022/01/26 15:30:49 by hulamy #+# #+# */
/* Updated: 2022/01/26 16:29:13 by hulamy ### ########.fr */ /* Updated: 2022/01/26 18:52:32 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -36,17 +36,31 @@ static int is_finished(t_philo *philo, struct timeval *stime)
return (0); return (0);
} }
int is_satiated(t_philo *philo)
{
if (philo->eat_count == philo->params->n_eat)
{
philo->global->satiated_count++;
return (1);
}
return (0);
}
static void pere_fouettard(t_philo *philo) static void pere_fouettard(t_philo *philo)
{ {
struct timeval stime; struct timeval stime;
int i; int i;
int satiated;
while (philo->global->stop == 0) while (philo->global->stop == 0)
{ {
i = 0; i = 0;
satiated = 1;
gettimeofday(&stime, NULL); gettimeofday(&stime, NULL);
while (i < philo->params->n_phi) while (i < philo->params->n_phi)
{ {
if (satiated == 1 && is_satiated(philo) == 0)
satiated = 0;
if (is_finished(philo, &stime)) if (is_finished(philo, &stime))
break ; break ;
philo = philo->next; philo = philo->next;

View File

@@ -6,7 +6,7 @@
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */ /* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:19 by hulamy #+# #+# */ /* Created: 2022/01/26 15:30:19 by hulamy #+# #+# */
/* Updated: 2022/01/26 17:50:31 by hulamy ### ########.fr */ /* Updated: 2022/01/26 18:04:08 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */