From c33bbb4f66dbaca2f02b6f32c2d6bdb84be5f115 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 28 Jan 2025 13:56:07 +0100 Subject: [PATCH] Protect simulation_running with mutex --- philo/Makefile | 2 +- philo/include/philo.h | 10 +++++++--- philo/src/ft_utils.c | 10 +++++++--- philo/src/init.c | 10 +++++++++- philo/src/main.c | 3 +-- philo/src/philo_eat.c | 10 +++++----- philo/src/sim_controls.c | 30 ++++++++++++++++++++++++++++++ philo/src/simulation.c | 25 ++++++++++++++----------- 8 files changed, 74 insertions(+), 26 deletions(-) create mode 100644 philo/src/sim_controls.c diff --git a/philo/Makefile b/philo/Makefile index 9cd7e26..0f33d41 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -9,7 +9,7 @@ CFLAGS = -Wall -Wextra -Werror HEADERS = -Iinclude VPATH := src -SRC := main.c init.c ft_utils.c simulation.c philo_eat.c +SRC := main.c init.c ft_utils.c simulation.c philo_eat.c sim_controls.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/philo/include/philo.h b/philo/include/philo.h index 07e4e96..ef5565e 100644 --- a/philo/include/philo.h +++ b/philo/include/philo.h @@ -6,7 +6,7 @@ /* By: dkaiser id str" */ -void ft_log(int id, const char *str); +void ft_log(t_philo *philo, const char *str); #endif diff --git a/philo/src/ft_utils.c b/philo/src/ft_utils.c index d77ee3f..0bcd17c 100644 --- a/philo/src/ft_utils.c +++ b/philo/src/ft_utils.c @@ -6,11 +6,12 @@ /* By: dkaiser int ft_err(const char *str) { @@ -28,9 +29,12 @@ int ft_cur_time_in_ms(void) return (time_in_ms); } -void ft_log(int id, const char *str) +void ft_log(t_philo *philo, const char *str) { - printf("%u %d %s\n", ft_cur_time_in_ms(), id, str); + pthread_mutex_lock(&philo->data->sr_mutex); + if (philo->data->simulation_running) + printf("%u %d %s\n", ft_cur_time_in_ms(), philo->id, str); + pthread_mutex_unlock(&philo->data->sr_mutex); } int ft_atoi(const char *str) diff --git a/philo/src/init.c b/philo/src/init.c index 6f91f7e..1afa104 100644 --- a/philo/src/init.c +++ b/philo/src/init.c @@ -6,11 +6,12 @@ /* By: dkaiser static void init_philo(t_philo *philo, t_phdata *data, int id); static int init_philos(t_philo **philos, t_phdata *data); @@ -35,6 +36,13 @@ int init(t_philo **philos, t_phdata *data) free(data->forks); return (result); } + result = pthread_mutex_init(&data->sr_mutex, NULL); + if (result != 0) + { + free(*philos); + free(data->forks); + return (result); + } result = init_philos(philos, data); return (result); } diff --git a/philo/src/main.c b/philo/src/main.c index ae0e54b..c928795 100644 --- a/philo/src/main.c +++ b/philo/src/main.c @@ -6,7 +6,7 @@ /* By: dkaiser owner == 0) { fork->owner = philo->id; - ft_log(philo->id, "has taken a fork"); + ft_log(philo, "has taken a fork"); } pthread_mutex_unlock(&fork->mutex); } @@ -35,10 +35,10 @@ static void eat(t_philo *philo, t_fork *left_fork, t_fork *right_fork) int started_eating; started_eating = ft_cur_time_in_ms(); - ft_log(philo->id, "is eating"); + ft_log(philo, "is eating"); while (ft_cur_time_in_ms() < started_eating + philo->data->time_to_eat) { - if (!philo->data->simulation_running) + if (!is_simulation_running(philo->data)) return ; if (philo_die(philo)) return ; @@ -65,7 +65,7 @@ void philo_eat(t_philo *philo) usleep(250); take_fork(philo, left_fork); take_fork(philo, right_fork); - if (!philo->data->simulation_running) + if (!is_simulation_running(philo->data)) return ; if (philo_die(philo)) return ; diff --git a/philo/src/sim_controls.c b/philo/src/sim_controls.c new file mode 100644 index 0000000..6489211 --- /dev/null +++ b/philo/src/sim_controls.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sim_controls.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser sr_mutex); + result = data->simulation_running; + pthread_mutex_unlock(&data->sr_mutex); + return (result); +} + +void stop_simulation(t_phdata *data) +{ + pthread_mutex_lock(&data->sr_mutex); + data->simulation_running = 0; + pthread_mutex_unlock(&data->sr_mutex); +} diff --git a/philo/src/simulation.c b/philo/src/simulation.c index 53bb6f8..4ce5178 100644 --- a/philo/src/simulation.c +++ b/philo/src/simulation.c @@ -6,7 +6,7 @@ /* By: dkaiser philo->last_time_eaten + philo->data->time_to_die) { philo->is_alive = 0; - ft_log(philo->id, "has died"); - philo->data->simulation_running = 0; + ft_log(philo, "has died"); + stop_simulation(philo->data); return (1); } return (0); @@ -31,11 +31,13 @@ void philo_sleep(t_philo *philo) started_sleeping = ft_cur_time_in_ms(); tts = philo->data->time_to_sleep; - ft_log(philo->id, "is sleeping"); + ft_log(philo, "is sleeping"); while (ft_cur_time_in_ms() < started_sleeping + tts) { if (philo_die(philo)) return ; + if (!is_simulation_running(philo->data)) + return ; usleep(250); } } @@ -45,7 +47,7 @@ void process_philo(void *arg) t_philo *philo; philo = (t_philo *)arg; - while (philo->data->simulation_running) + while (is_simulation_running(philo->data)) { philo_eat(philo); if (philo->times_must_eat == 0) @@ -54,7 +56,7 @@ void process_philo(void *arg) philo->data->philos_must_eat -= 1; pthread_mutex_unlock(&philo->data->pme_mutex); } - if (!philo->data->simulation_running) + if (!is_simulation_running(philo->data)) break ; if (philo->data->nbr_of_philos < 2) { @@ -63,9 +65,9 @@ void process_philo(void *arg) continue ; } philo_sleep(philo); - if (!philo->data->simulation_running) + if (!is_simulation_running(philo->data)) break ; - ft_log(philo->id, "is thinking"); + ft_log(philo, "is thinking"); } } @@ -76,6 +78,7 @@ int run_simulation(int nbr_of_philos, t_philo *philos, t_phdata *data) i = 0; result = EXIT_SUCCESS; + data->simulation_running = 1; while (i < nbr_of_philos) { result = pthread_create(&(philos[i].thread), NULL, @@ -85,11 +88,11 @@ int run_simulation(int nbr_of_philos, t_philo *philos, t_phdata *data) i++; } if (result != EXIT_SUCCESS) - data->simulation_running = 0; - while (data->simulation_running) + stop_simulation(data); + while (is_simulation_running(data)) { if (data->philos_must_eat <= 0) - data->simulation_running = 0; + stop_simulation(data); } while (i--) pthread_join(philos[i].thread, NULL); -- 2.47.2