Compare commits

...

2 Commits

Author SHA1 Message Date
Camille Chauvet
b940b46924 ?fix 2023-05-05 16:59:36 +00:00
Camille Chauvet
5c309f2f02 fix: sleeptime and eat time if time to die < time eat|sleep 2023-05-02 11:44:16 +00:00
4 changed files with 29 additions and 13 deletions

3
data.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:28:36 by cchauvet #+# #+# */
/* Updated: 2023/04/27 12:04:31 by cchauvet ### ########.fr */
/* Updated: 2023/05/05 16:58:45 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -67,6 +67,7 @@ void data_destroyer(t_data *data)
i++;
usleep(1000);
}
usleep(1000);
i = -1;
while (++i < (ssize_t) data->nb_philos)
pthread_mutex_destroy(&data->forks[i]);

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))

26
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:44:21 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -30,13 +30,27 @@ 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;
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;
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