Compare commits

..

No commits in common. "b940b4692400a28d6734dd57accdfda849a85f74" and "796e186c3690df0e32f0fc7ddfd69d1f6d0ae5fc" have entirely different histories.

4 changed files with 13 additions and 29 deletions

3
data.c
View File

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

View File

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

26
time.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:35:26 by cchauvet #+# #+# */ /* Created: 2023/04/27 11:35:26 by cchauvet #+# #+# */
/* Updated: 2023/05/02 11:44:21 by cchauvet ### ########.fr */ /* Updated: 2023/04/27 11:54:04 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,27 +30,13 @@ size_t get_time(void)
return ((time - start_time) / 1000); return ((time - start_time) / 1000);
} }
size_t get_time_eat(t_philo *philo, t_data *data) size_t get_time_perfect(t_philo *philo, t_data *data)
{ {
size_t time; size_t time;
size_t value;
time = get_time(); time = get_time();
value = (data->life_expectency - philo->last_eat - time); if (time - philo->last_eat + data->eat_time > data->life_expectency)
if (value > data->eat_time) return ((time - philo->last_eat + data->eat_time));
value = data->eat_time; else
return (value); return (data->eat_time);
} }
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 +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:32:57 by cchauvet #+# #+# */ /* Created: 2023/04/27 11:32:57 by cchauvet #+# #+# */
/* Updated: 2023/05/02 11:27:53 by cchauvet ### ########.fr */ /* Updated: 2023/04/27 11:54:55 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,7 +17,6 @@
# include <stddef.h> # include <stddef.h>
size_t get_time(void); size_t get_time(void);
size_t get_time_eat(t_philo *philo, t_data *data); size_t get_time_perfect(t_philo *philo, t_data *data);
size_t get_time_sleep(t_philo *philo, t_data *data);
#endif #endif