From: Lukas Jiriste Date: Fri, 22 Mar 2024 08:46:28 +0000 (+0100) Subject: Add lock to the philos' state X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=HEAD;p=42%2Fphilosophers_old.git Add lock to the philos' state This should prevent data races between any philo thread and the main thread checking their status. --- diff --git a/philo/check_death.c b/philo/check_death.c index 9cca50a..e227520 100644 --- a/philo/check_death.c +++ b/philo/check_death.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/19 15:53:52 by ljiriste #+# #+# */ -/* Updated: 2024/03/22 09:22:19 by ljiriste ### ########.fr */ +/* Updated: 2024/03/22 09:44:44 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,11 +23,13 @@ static int philo_died(t_philo *philo, t_settings *set) suseconds_t now; i = 0; - now = cur_time_sus(); while (i < set->philo_count) { + pthread_mutex_lock(&philo->state_lock); + now = cur_time_sus(); if ((now - philo->last_eaten) > set->time_to_die) { + pthread_mutex_unlock(&philo->state_lock); pthread_mutex_lock(&set->terminal); printf("%li %lu %s\n", (cur_time_sus() - set->program_start) / 1000, i + 1, G_DIE_STR); @@ -35,6 +37,7 @@ static int philo_died(t_philo *philo, t_settings *set) pthread_mutex_unlock(&set->terminal); return (1); } + pthread_mutex_unlock(&philo->state_lock); ++i; } return (0); @@ -49,8 +52,13 @@ static int all_full(t_philo *philos, t_settings *set) return (0); while (i < set->philo_count) { + pthread_mutex_lock(&philos[i].state_lock); if (philos[i].times_eaten < set->min_eats_num) + { + pthread_mutex_unlock(&philos[i].state_lock); return (0); + } + pthread_mutex_unlock(&philos[i].state_lock); ++i; } return (1); @@ -65,8 +73,10 @@ static suseconds_t time_to_starve(t_philo *philos, t_settings *set) i = 1; while (i < set->philo_count) { + pthread_mutex_lock(&philos[i].state_lock); if (furthest_eaten > philos[i].last_eaten) furthest_eaten = philos[i].last_eaten; + pthread_mutex_unlock(&philos[i].state_lock); ++i; } return (set->time_to_die - (cur_time_sus() - furthest_eaten)); diff --git a/philo/init.c b/philo/init.c index e6bc43f..25dd6e2 100644 --- a/philo/init.c +++ b/philo/init.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/19 15:48:58 by ljiriste #+# #+# */ -/* Updated: 2024/03/21 10:04:35 by ljiriste ### ########.fr */ +/* Updated: 2024/03/22 09:39:54 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,6 +55,7 @@ t_philo *build_philos(pthread_mutex_t *forks, t_settings *set) philos[count].times_eaten = 0; philos[count].settings = set; philos[count].forks[0] = forks + count - count % 2; + pthread_mutex_init(&philos[count].state_lock, NULL); if (count > 0) philos[count].forks[1] = forks + count - (count + 1) % 2; } diff --git a/philo/main.c b/philo/main.c index c09afbf..13257ce 100644 --- a/philo/main.c +++ b/philo/main.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 12:49:40 by ljiriste #+# #+# */ -/* Updated: 2024/03/21 10:26:01 by ljiriste ### ########.fr */ +/* Updated: 2024/03/22 09:41:43 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,6 +31,7 @@ static void cleanup(pthread_mutex_t *forks, t_philo *philos, pthread_t *threads, { --count; pthread_mutex_destroy(&forks[count]); + pthread_mutex_destroy(&philos[count].state_lock); pthread_join(threads[count], NULL); } pthread_mutex_destroy(&set->terminal); diff --git a/philo/philo.h b/philo/philo.h index f7c99cd..412ea52 100644 --- a/philo/philo.h +++ b/philo/philo.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/19 14:25:18 by ljiriste #+# #+# */ -/* Updated: 2024/03/21 10:16:40 by ljiriste ### ########.fr */ +/* Updated: 2024/03/22 09:40:43 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,6 +39,7 @@ const char *g_die_str = "died"; // unsigned long would be more apropriate for min_eats_num // but for the ease of parsing I'm using size_t (they are the same) +// terminal mutex is also used to lock the end member typedef struct s_settings { int end; @@ -56,6 +57,7 @@ typedef struct s_philo size_t times_eaten; size_t id; suseconds_t last_eaten; + pthread_mutex_t state_lock; pthread_mutex_t *forks[2]; t_settings *settings; } t_philo; diff --git a/philo/philos.c b/philo/philos.c index 316ec5e..a334138 100644 --- a/philo/philos.c +++ b/philo/philos.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/21 09:16:23 by ljiriste #+# #+# */ -/* Updated: 2024/03/21 10:26:14 by ljiriste ### ########.fr */ +/* Updated: 2024/03/22 09:42:20 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,9 +34,11 @@ void philo_eat(t_philo *philo) pthread_mutex_lock(philo->forks[1]); thread_print(philo->settings, philo->id, G_FORK_STR); thread_print(philo->settings, philo->id, G_EAT_STR); + pthread_mutex_lock(&philo->state_lock); philo->last_eaten = cur_time_sus(); - usleep(philo->settings->time_to_eat); ++philo->times_eaten; + pthread_mutex_unlock(&philo->state_lock); + usleep(philo->settings->time_to_eat); pthread_mutex_unlock(philo->forks[0]); pthread_mutex_unlock(philo->forks[1]); return ;