fix: sleeptime and eat time if time to die < time eat|sleep

This commit is contained in:
Camille Chauvet 2023-05-02 11:44:16 +00:00
parent 796e186c36
commit 5c309f2f02
3 changed files with 29 additions and 12 deletions

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:44:14 by cchauvet #+# #+# */
/* Updated: 2023/04/27 12:04:03 by cchauvet ### ########.fr */
/* Updated: 2023/05/02 11:41:10 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -72,7 +72,7 @@ bool philo_eat(t_philo *philo, t_data *data)
if (philo_take_forks(philo, data))
return (1);
print_eating(philo);
usleep(get_time_perfect(philo, data) * 1000);
usleep(get_time_eat(philo, data) * 1000);
pthread_mutex_unlock(&data->forks[philo->id]);
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
if (check(philo, data))
@ -89,7 +89,7 @@ bool philo_eat(t_philo *philo, t_data *data)
void philo_sleep(t_data *data, t_philo *philo)
{
print_sleeping(philo);
usleep(data->eat_time * 1000);
usleep(get_time_sleep(philo, data) * 1000);
}
void *philo_routine(void *arg)
@ -100,7 +100,7 @@ void *philo_routine(void *arg)
philo = arg;
data = philo->data;
print_thinking(philo);
usleep((philo->id % 2) * (data->eat_time) * 1000);
usleep((philo->id % 2) * (get_time_eat(philo, data)) * 1000);
while (true)
{
if (check(philo, data))

28
time.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:35:26 by cchauvet #+# #+# */
/* Updated: 2023/04/27 11:54:04 by cchauvet ### ########.fr */
/* Updated: 2023/05/02 11:42:45 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -30,13 +30,29 @@ size_t get_time(void)
return ((time - start_time) / 1000);
}
size_t get_time_perfect(t_philo *philo, t_data *data)
size_t get_time_eat(t_philo *philo, t_data *data)
{
size_t time;
size_t value;
time = get_time();
if (time - philo->last_eat + data->eat_time > data->life_expectency)
return ((time - philo->last_eat + data->eat_time));
else
return (data->eat_time);
value = (data->life_expectency - philo->last_eat - time);
if (value > data->eat_time)
value = data->eat_time;
// printf("\n%zu: time: %zu, last: %zu, eat: %zu, expectencty: %zu, value: %zu\n", philo->id + 1, time, philo->last_eat, data->eat_time, data->life_expectency, value);
return (value);
}
size_t get_time_sleep(t_philo *philo, t_data *data)
{
size_t time;
size_t value;
time = get_time();
value = (data->life_expectency - (time - philo->last_eat));
if (value > data->sleep_time)
value = data->sleep_time;
// printf("\n%zu: time: %zu, last: %zu, sleep: %zu, expectencty: %zu, value: %zu\n", philo->id + 1, time, philo->last_eat, data->sleep_time, data->life_expectency, value);
return (value);
}

5
time.h
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:32:57 by cchauvet #+# #+# */
/* Updated: 2023/04/27 11:54:55 by cchauvet ### ########.fr */
/* Updated: 2023/05/02 11:27:53 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,6 +17,7 @@
# include <stddef.h>
size_t get_time(void);
size_t get_time_perfect(t_philo *philo, t_data *data);
size_t get_time_eat(t_philo *philo, t_data *data);
size_t get_time_sleep(t_philo *philo, t_data *data);
#endif