diff options
| author | Dominik Kaiser | 2025-01-28 13:56:07 +0100 |
|---|---|---|
| committer | Dominik Kaiser | 2025-01-28 13:56:07 +0100 |
| commit | c33bbb4f66dbaca2f02b6f32c2d6bdb84be5f115 (patch) | |
| tree | a33a18e211b4fcae9e6d6dd8257ac56a5ae603f9 /philo | |
| parent | ec7a989cf45440e41d43e7fa2d2ce660fa499933 (diff) | |
| download | Philosophers-c33bbb4f66dbaca2f02b6f32c2d6bdb84be5f115.tar.gz Philosophers-c33bbb4f66dbaca2f02b6f32c2d6bdb84be5f115.zip | |
Protect simulation_running with mutex
Diffstat (limited to 'philo')
| -rw-r--r-- | philo/Makefile | 2 | ||||
| -rw-r--r-- | philo/include/philo.h | 10 | ||||
| -rw-r--r-- | philo/src/ft_utils.c | 10 | ||||
| -rw-r--r-- | philo/src/init.c | 10 | ||||
| -rw-r--r-- | philo/src/main.c | 3 | ||||
| -rw-r--r-- | philo/src/philo_eat.c | 10 | ||||
| -rw-r--r-- | philo/src/sim_controls.c | 30 | ||||
| -rw-r--r-- | philo/src/simulation.c | 25 |
8 files changed, 74 insertions, 26 deletions
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 <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/17 10:23:19 by dkaiser #+# #+# */ -/* Updated: 2025/01/28 13:16:12 by dkaiser ### ########.fr */ +/* Updated: 2025/01/28 13:34:48 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,6 +38,7 @@ typedef struct s_phdata int philos_must_eat; pthread_mutex_t pme_mutex; int simulation_running; + pthread_mutex_t sr_mutex; t_fork *forks; } t_phdata; @@ -57,6 +58,9 @@ int run_simulation(int nbr_of_philos, t_philo *philos, int philo_die(t_philo *philo); void philo_eat(t_philo *philo); +int is_simulation_running(t_phdata *data); +void stop_simulation(t_phdata *data); + /* ** Prints error message and returns EXIT_FAILURE */ @@ -73,8 +77,8 @@ int ft_atoi(const char *str); int ft_cur_time_in_ms(void); /* -** Prints "timestamp_in_ms id str" +** Prints "timestamp_in_ms philo->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 <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/17 11:57:11 by dkaiser #+# #+# */ -/* Updated: 2025/01/28 13:04:10 by dkaiser ### ########.fr */ +/* Updated: 2025/01/28 13:52:09 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" +#include <pthread.h> 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 <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/28 13:12:57 by dkaiser #+# #+# */ -/* Updated: 2025/01/28 13:15:30 by dkaiser ### ########.fr */ +/* Updated: 2025/01/28 13:25:15 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" +#include <pthread.h> 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 <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/14 17:13:30 by dkaiser #+# #+# */ -/* Updated: 2025/01/28 13:20:47 by dkaiser ### ########.fr */ +/* Updated: 2025/01/28 13:49:55 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,7 +52,6 @@ int main(int argc, char *argv[]) result = init(&philos, &data); if (result != EXIT_SUCCESS) return (result); - data.simulation_running = 1; result = run_simulation(data.nbr_of_philos, philos, &data); free(philos); free(data.forks); diff --git a/philo/src/philo_eat.c b/philo/src/philo_eat.c index 3a0208e..b689f1f 100644 --- a/philo/src/philo_eat.c +++ b/philo/src/philo_eat.c @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/21 18:15:40 by dkaiser #+# #+# */ -/* Updated: 2025/01/28 13:05:31 by dkaiser ### ########.fr */ +/* Updated: 2025/01/28 13:42:19 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ static void take_fork(t_philo *philo, t_fork *fork) if (fork->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 <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/28 13:32:28 by dkaiser #+# #+# */ +/* Updated: 2025/01/28 13:45:20 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo.h" + +int is_simulation_running(t_phdata *data) +{ + int result; + + pthread_mutex_lock(&data->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 <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/17 14:38:04 by dkaiser #+# #+# */ -/* Updated: 2025/01/27 15:01:42 by dkaiser ### ########.fr */ +/* Updated: 2025/01/28 13:41:46 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,8 +17,8 @@ int philo_die(t_philo *philo) if (ft_cur_time_in_ms() > 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); |
