fix: end when philo died and not when philo stop eatind or sleep example 'philo 2 100 5 10000'

This commit is contained in:
Camille Chauvet 2023-04-20 12:20:22 +00:00
parent cb2e29d18b
commit 19c5a53ba9

25
philo.c
View File

@ -38,7 +38,17 @@ void eat(t_philo *philo, t_data *data)
print_take_a_fork(philo); print_take_a_fork(philo);
print_take_a_fork(philo); print_take_a_fork(philo);
print_eating(philo); print_eating(philo);
pthread_mutex_lock(&philo->last_eat_mutex);
if (get_time() - philo->last_eat + data->eat_time > data->life_expectency)
{
pthread_mutex_unlock(&philo->last_eat_mutex);
usleep((data->life_expectency -
(get_time() - philo->last_eat)) * 1000);
return ;
}
else
usleep(data->eat_time * 1000); usleep(data->eat_time * 1000);
pthread_mutex_unlock(&philo->last_eat_mutex);
pthread_mutex_lock(&philo->nb_meal_mutex); pthread_mutex_lock(&philo->nb_meal_mutex);
philo->nb_meal++; philo->nb_meal++;
pthread_mutex_unlock(&philo->nb_meal_mutex); pthread_mutex_unlock(&philo->nb_meal_mutex);
@ -81,7 +91,22 @@ bool philo_eat(t_philo *philo, t_data *data)
void philo_sleep(t_data *data, t_philo *philo) void philo_sleep(t_data *data, t_philo *philo)
{ {
print_sleeping(philo); print_sleeping(philo);
pthread_mutex_lock(&philo->last_sleep_mutex);
// printf("time=%zu, last_sleep=%zu, sleep_time=%zu, life_expectency=%zu\n", get_time(), philo->last_sleep, data->sleep_time, data->life_expectency);
if (get_time() - philo->last_sleep + data->sleep_time > data->life_expectency)
{
pthread_mutex_unlock(&philo->last_sleep_mutex);
// printf("rompiche=%zu\n", (data->life_expectency -
// (get_time() - philo->last_sleep)));
usleep((data->life_expectency -
(get_time() - philo->last_sleep)) * 1000);
return ;
}
else
usleep(data->sleep_time * 1000); usleep(data->sleep_time * 1000);
philo->last_sleep = get_time();
pthread_mutex_unlock(&philo->last_sleep_mutex);
} }