Compare commits

...

4 Commits

Author SHA1 Message Date
Camille Chauvet
e4b5e6ea0d fix: update last_eat time after eat 2023-05-25 19:25:47 +02:00
Camille Chauvet
f88abe9631 dsfsd 2023-05-25 17:31:02 +02:00
Camille Chauvet
984f4c59bf norm 2023-05-25 15:24:35 +02:00
Camille Chauvet
61b599b9f2 opti 2023-05-25 15:24:17 +02:00
5 changed files with 35 additions and 49 deletions

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:33:06 by cchauvet #+# #+# */ /* Created: 2023/04/27 11:33:06 by cchauvet #+# #+# */
/* Updated: 2023/05/25 14:49:38 by cchauvet ### ########.fr */ /* Updated: 2023/05/25 15:26:38 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,7 +22,7 @@ typedef struct s_data
size_t sleep_time; size_t sleep_time;
size_t life_expectency; size_t life_expectency;
size_t nb_philos; size_t nb_philos;
size_t nb_meals; long nb_meals;
void **philos; void **philos;
pthread_t *threads; pthread_t *threads;
pthread_mutex_t *forks; pthread_mutex_t *forks;

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:33:25 by cchauvet #+# #+# */ /* Created: 2023/04/27 11:33:25 by cchauvet #+# #+# */
/* Updated: 2023/04/27 11:49:33 by cchauvet ### ########.fr */ /* Updated: 2023/05/25 16:34:22 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -75,11 +75,6 @@ int routine(t_data *data)
print_died(philo); print_died(philo);
return (1); return (1);
} }
if (get_min_meal(data) >= data->nb_meals)
{
stop(data);
return (1);
}
i++; i++;
} }
return (0); return (0);
@ -87,13 +82,14 @@ int routine(t_data *data)
void *check_routine(t_data *data) void *check_routine(t_data *data)
{ {
while (true) while (data->stop == 0)
{ {
if (routine(data)) if (routine(data))
{ {
return (NULL); return (NULL);
} }
} }
return (NULL);
} }
int main(int ac, char **av) int main(int ac, char **av)

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/24 14:03:42 by cchauvet ### ########.fr */ /* Updated: 2023/05/25 19:22:55 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,23 +16,15 @@
#include "./print.h" #include "./print.h"
#include "data.h" #include "data.h"
void philo_stop(t_philo *philo) bool check(t_data *data)
{
pthread_mutex_lock(&philo->stop_mutex);
philo->stop = 1;
pthread_mutex_unlock(&philo->stop_mutex);
}
bool check(t_philo *philo, t_data *data)
{ {
bool stop; bool stop;
if (get_min_meal(data) == data->nb_meals) if (data->nb_meals != -1 && get_min_meal(data) == (size_t) data->nb_meals)
{ {
pthread_mutex_lock(&philo->stop_mutex); pthread_mutex_lock(&data->stop_mutex);
philo->stop = 1; data->stop = 1;
pthread_mutex_unlock(&philo->stop_mutex); pthread_mutex_unlock(&data->stop_mutex);
return (1);
} }
pthread_mutex_lock(&data->stop_mutex); pthread_mutex_lock(&data->stop_mutex);
stop = data->stop; stop = data->stop;
@ -44,14 +36,14 @@ int philo_take_forks(t_philo *philo, t_data *data)
{ {
pthread_mutex_lock(&data->forks[philo->id]); pthread_mutex_lock(&data->forks[philo->id]);
print_take_a_fork(philo); print_take_a_fork(philo);
if (check(philo, data)) if (check(data))
{ {
pthread_mutex_unlock(&data->forks[philo->id]); pthread_mutex_unlock(&data->forks[philo->id]);
return (1); return (1);
} }
while ((philo->id + 1) % data->nb_philos == philo->id) while ((philo->id + 1) % data->nb_philos == philo->id)
{ {
if (check(philo, data)) if (check(data))
{ {
pthread_mutex_unlock(&data->forks[philo->id]); pthread_mutex_unlock(&data->forks[philo->id]);
return (1); return (1);
@ -59,7 +51,7 @@ int philo_take_forks(t_philo *philo, t_data *data)
} }
pthread_mutex_lock(&data->forks[(philo->id + 1) % data->nb_philos]); pthread_mutex_lock(&data->forks[(philo->id + 1) % data->nb_philos]);
print_take_a_fork(philo); print_take_a_fork(philo);
if (check(philo, data)) if (check(data))
{ {
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]); pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
pthread_mutex_unlock(&data->forks[philo->id]); pthread_mutex_unlock(&data->forks[philo->id]);
@ -72,18 +64,18 @@ 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);
pthread_mutex_lock(&philo->last_eat_mutex);
philo->last_eat = get_time();
pthread_mutex_unlock(&philo->last_eat_mutex);
print_eating(philo); print_eating(philo);
usleep(get_time_eat(philo, data) * 1000); usleep(get_time_eat(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(data))
return (1); return (1);
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);
pthread_mutex_lock(&philo->last_eat_mutex);
philo->last_eat = get_time();
pthread_mutex_unlock(&philo->last_eat_mutex);
return (0); return (0);
} }
@ -105,13 +97,15 @@ void *philo_routine(void *arg)
usleep((philo->id % 2) * (get_time_eat(philo, data)) * 1000); usleep((philo->id % 2) * (get_time_eat(philo, data)) * 1000);
while (true) while (true)
{ {
if (check(philo, data) if (check(data)
|| philo_eat(philo, data) || philo_eat(philo, data)
|| check(philo, data) || check(data)
|| philo_sleep(data, philo) || philo_sleep(data, philo)
|| check(philo, data)) || check(data))
{ {
philo_stop(philo); pthread_mutex_lock(&philo->stop_mutex);
philo->stop = 1;
pthread_mutex_unlock(&philo->stop_mutex);
return (NULL); return (NULL);
} }
print_thinking(philo); print_thinking(philo);

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:33:33 by cchauvet #+# #+# */ /* Created: 2023/04/27 11:33:33 by cchauvet #+# #+# */
/* Updated: 2023/05/17 13:33:17 by cchauvet ### ########.fr */ /* Updated: 2023/05/25 16:47:54 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,7 +30,7 @@ static void print(t_data *data, size_t id, char *str)
if (stop == false) if (stop == false)
{ {
time = get_time(); time = get_time();
printf("%07zu %07zu %s\n", time, id + 1, str); printf("%07zu %03zu %s\n", time, id + 1, str);
} }
pthread_mutex_unlock(&data->print_mutex); pthread_mutex_unlock(&data->print_mutex);
} }

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/16 13:24:30 by cchauvet ### ########.fr */ /* Updated: 2023/05/25 17:28:19 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,23 +33,19 @@ size_t get_time(void)
size_t get_time_eat(t_philo *philo, t_data *data) size_t get_time_eat(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 + data->eat_time > data->life_expectency + philo->last_eat)
if (value > data->eat_time) return (data->life_expectency + philo->last_eat - time);
value = data->eat_time; return (data->eat_time);
return (value);
} }
size_t get_time_sleep(t_philo *philo, t_data *data) size_t get_time_sleep(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 - (time - philo->last_eat)); if (time + data->sleep_time > data->life_expectency + philo->last_eat)
if (value > data->sleep_time) return (data->life_expectency + philo->last_eat - time);
value = data->sleep_time; return (data->sleep_time);
return (value);
} }