From dd686a4f85d82e67766b1914e3611287ece86d33 Mon Sep 17 00:00:00 2001 From: whaffman Date: Mon, 27 Jan 2025 14:14:43 +0100 Subject: [PATCH] let them think --- philo/Makefile | 2 +- philo/inc/philo.h | 8 ++- philo/src/check_alive.c | 18 ------ philo/src/create_mutexes.c | 5 +- philo/src/create_philos.c | 2 +- philo/src/create_threads.c | 2 +- philo/src/destroy_mutexes.c | 2 +- philo/src/free_philos.c | 10 +++- philo/src/join_threads.c | 2 +- philo/src/main.c | 2 +- philo/src/parse_arguments.c | 2 +- philo/src/philo_routine.c | 102 ++------------------------------- philo/src/utils/get_time.c | 2 +- philo/src/utils/ph_atoi.c | 2 +- philo/src/utils/print_rules.c | 2 +- philo/src/utils/print_status.c | 2 +- 16 files changed, 33 insertions(+), 132 deletions(-) delete mode 100644 philo/src/check_alive.c diff --git a/philo/Makefile b/philo/Makefile index a2a4c72..9c2511e 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -25,7 +25,7 @@ CC = cc RM = rm -rf INCLUDES = -I./$(INC_PATH) -CFLAGS = -Wall -Wextra -Werror -MMD -g +CFLAGS = -Wall -Wextra -Werror -MMD -g UNAME_S := $(shell uname -s) ifeq ($(UNAME_S),Linux) diff --git a/philo/inc/philo.h b/philo/inc/philo.h index 4b34017..f5ac269 100644 --- a/philo/inc/philo.h +++ b/philo/inc/philo.h @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2025/01/20 14:27:34 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2025/01/20 14:27:34 by whaffman ### ### ### ### / \ */ +/* Updated: 2025/01/27 14:13:50 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ @@ -81,10 +81,14 @@ int join_threads(t_rules *rules); void print_rules(t_rules *rules); int check_death(t_philo *philo); void philo_die(t_philo *philo); -int philo_died_while_sleeping(t_philo *philo, int time); int philo_eat(t_philo *philo); void *monitor_routine(void *arg); int check_philos(t_rules *rules); int ft_min(int a, int b); +void set_finished(t_rules *rules); +int get_finished(t_rules *rules); +void synchronize_philos(t_philo *philo); +int philo_sleep(t_philo *philo, int time); + #endif diff --git a/philo/src/check_alive.c b/philo/src/check_alive.c deleted file mode 100644 index 93884a0..0000000 --- a/philo/src/check_alive.c +++ /dev/null @@ -1,18 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: o_ :::::: ::: */ -/* check_alive.c :+: / :+::+: :+: */ -/* +:+ > +:++:+ +:+ */ -/* By: whaffman +#+ +:+ +#++#++:++#++ */ -/* +#+ +#+#+ +#++#+ +#+ \o/ */ -/* Created: 2025/01/20 14:26:43 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2025/01/20 14:27:35 by whaffman ### ### ### ### / \ */ -/* */ -/* ************************************************************************** */ - -#include "philo.h" - -int check_death(t_philo *philo) -{ - return (get_time() - philo->last_meal >= philo->rules->time_to_die); -} diff --git a/philo/src/create_mutexes.c b/philo/src/create_mutexes.c index 39cc35f..21ec089 100644 --- a/philo/src/create_mutexes.c +++ b/philo/src/create_mutexes.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2025/01/20 14:26:44 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2025/01/20 14:27:35 by whaffman ### ### ### ### / \ */ +/* Updated: 2025/01/27 14:13:51 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ @@ -21,9 +21,6 @@ int create_mutexes(t_rules *rules) return (FAILURE); if (pthread_mutex_init(rules->print_lock, NULL)) return (FAILURE); - rules->forks = malloc(rules->n_philos * sizeof(pthread_mutex_t)); - if (!rules->forks) - return (FAILURE); while (i < rules->n_philos) { if (pthread_mutex_init(&rules->forks[i], NULL)) diff --git a/philo/src/create_philos.c b/philo/src/create_philos.c index 4363184..0826722 100644 --- a/philo/src/create_philos.c +++ b/philo/src/create_philos.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2025/01/20 14:26:45 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2025/01/20 14:27:36 by whaffman ### ### ### ### / \ */ +/* Updated: 2025/01/27 14:13:52 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ diff --git a/philo/src/create_threads.c b/philo/src/create_threads.c index e20207b..aff733b 100644 --- a/philo/src/create_threads.c +++ b/philo/src/create_threads.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2025/01/20 14:26:45 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2025/01/20 14:27:37 by whaffman ### ### ### ### / \ */ +/* Updated: 2025/01/27 14:13:53 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ diff --git a/philo/src/destroy_mutexes.c b/philo/src/destroy_mutexes.c index f73d89a..a687857 100644 --- a/philo/src/destroy_mutexes.c +++ b/philo/src/destroy_mutexes.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2025/01/20 14:26:46 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2025/01/20 14:27:38 by whaffman ### ### ### ### / \ */ +/* Updated: 2025/01/27 14:13:53 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ diff --git a/philo/src/free_philos.c b/philo/src/free_philos.c index 6f1b3cd..8fb77c0 100644 --- a/philo/src/free_philos.c +++ b/philo/src/free_philos.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2025/01/20 14:26:46 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2025/01/20 14:27:39 by whaffman ### ### ### ### / \ */ +/* Updated: 2025/01/27 14:13:54 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ @@ -14,6 +14,14 @@ void free_philos(t_rules *rules) { + int i; + i = 0; + while (i < rules->n_philos) + { + free(rules->philos[i].pid); + i++; + } + free(rules->monitor); free(rules->philos); free(rules->forks); free(rules->print_lock); diff --git a/philo/src/join_threads.c b/philo/src/join_threads.c index 85b991c..cf6175e 100644 --- a/philo/src/join_threads.c +++ b/philo/src/join_threads.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2025/01/20 14:26:47 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2025/01/20 14:27:39 by whaffman ### ### ### ### / \ */ +/* Updated: 2025/01/27 14:13:57 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ diff --git a/philo/src/main.c b/philo/src/main.c index d43c6e6..eb9cca0 100644 --- a/philo/src/main.c +++ b/philo/src/main.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2025/01/20 14:26:48 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2025/01/20 14:27:40 by whaffman ### ### ### ### / \ */ +/* Updated: 2025/01/27 14:13:58 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ diff --git a/philo/src/parse_arguments.c b/philo/src/parse_arguments.c index aa431b5..51b94ad 100644 --- a/philo/src/parse_arguments.c +++ b/philo/src/parse_arguments.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2025/01/20 14:26:48 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2025/01/20 14:27:40 by whaffman ### ### ### ### / \ */ +/* Updated: 2025/01/27 14:13:59 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ diff --git a/philo/src/philo_routine.c b/philo/src/philo_routine.c index 599fed8..541688b 100644 --- a/philo/src/philo_routine.c +++ b/philo/src/philo_routine.c @@ -6,102 +6,12 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2025/01/20 14:26:49 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2025/01/20 14:27:41 by whaffman ### ### ### ### / \ */ +/* Updated: 2025/01/27 14:14:02 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ #include "philo.h" - - -void set_finished(t_rules *rules) -{ - pthread_mutex_lock(rules->finished_lock); - rules->finished = 1; - pthread_mutex_unlock(rules->finished_lock); -} - -int get_finished(t_rules *rules) -{ - int finished; - - pthread_mutex_lock(rules->finished_lock); - finished = rules->finished; - pthread_mutex_unlock(rules->finished_lock); - return (finished); -} - -void set_death(t_philo *philo) -{ - pthread_mutex_lock(philo->death_lock); - philo->death = 1; - pthread_mutex_unlock(philo->death_lock); -} - -int get_death(t_philo *philo) -{ - int death; - - pthread_mutex_lock(philo->death_lock); - death = philo->death; - pthread_mutex_unlock(philo->death_lock); - return (death); -} - -void philo_die(t_philo *philo) -{ - print_status(philo, "died"); - philo->death = 1; - set_finished(philo->rules); - pthread_mutex_unlock(philo->l_fork); - pthread_mutex_unlock(philo->r_fork); -} - -int philo_sleep(t_philo *philo, int time) -{ - const int start = get_time(); - - while (get_time() - start < time) - { - if (philo->death) - return (FAILURE); - usleep(50); - } - return (SUCCESS); -} -int philo_eat(t_philo *philo) -{ - if (philo->id % 2 == 1) - { - pthread_mutex_lock(philo->r_fork); - print_status(philo, "has taken a fork"); - } - pthread_mutex_lock(philo->l_fork); - print_status(philo, "has taken a fork"); - if (philo->id % 2 != 1) - { - pthread_mutex_lock(philo->r_fork); - print_status(philo, "has taken a fork"); - } - if (philo->death) - return (FAILURE); - print_status(philo, "is eating"); - philo->last_meal = get_time(); - philo->n_eat++; - return (SUCCESS); -} -void synchronize_philos(t_philo *philo) -{ - pthread_mutex_lock(philo->rules->finished_lock); - pthread_mutex_unlock(philo->rules->finished_lock); - philo->last_meal = get_time(); - if (philo->id % 2 == 1) - { - print_status(philo, "is thinking"); - usleep(philo->rules->time_to_eat * 10); - } -} - void *philo_routine(void *arg) { t_philo *philo; @@ -110,20 +20,20 @@ void *philo_routine(void *arg) synchronize_philos(philo); while (!philo->death && !get_finished(philo->rules)) { - if (philo->death) - break ; if (!philo_eat(philo)) break ; if (!philo_sleep(philo, philo->rules->time_to_eat)) break ; pthread_mutex_unlock(philo->l_fork); pthread_mutex_unlock(philo->r_fork); - if (!philo->death) + if (!philo->death && !get_finished(philo->rules)) print_status(philo, "is sleeping"); if (!philo_sleep(philo, philo->rules->time_to_sleep)) - break; - if(!philo->death) + break ; + if(!philo->death && !get_finished(philo->rules)) print_status(philo, "is thinking"); + if (!philo_sleep(philo, (philo->rules->time_to_die - philo->rules->time_to_eat - philo->rules->time_to_sleep) / 3)) + break ; } return (NULL); } diff --git a/philo/src/utils/get_time.c b/philo/src/utils/get_time.c index ad86129..83d0ce1 100644 --- a/philo/src/utils/get_time.c +++ b/philo/src/utils/get_time.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2025/01/20 14:27:08 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2025/01/20 14:27:42 by whaffman ### ### ### ### / \ */ +/* Updated: 2025/01/27 14:14:05 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ diff --git a/philo/src/utils/ph_atoi.c b/philo/src/utils/ph_atoi.c index b6df97c..9e2f33e 100644 --- a/philo/src/utils/ph_atoi.c +++ b/philo/src/utils/ph_atoi.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2025/01/20 14:27:09 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2025/01/20 14:27:42 by whaffman ### ### ### ### / \ */ +/* Updated: 2025/01/27 14:14:05 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ diff --git a/philo/src/utils/print_rules.c b/philo/src/utils/print_rules.c index c4ce46e..2f3d9ee 100644 --- a/philo/src/utils/print_rules.c +++ b/philo/src/utils/print_rules.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2025/01/20 14:27:10 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2025/01/20 14:27:43 by whaffman ### ### ### ### / \ */ +/* Updated: 2025/01/27 14:14:06 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ diff --git a/philo/src/utils/print_status.c b/philo/src/utils/print_status.c index 99e3115..286b56f 100644 --- a/philo/src/utils/print_status.c +++ b/philo/src/utils/print_status.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2025/01/20 14:27:10 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2025/01/20 14:27:44 by whaffman ### ### ### ### / \ */ +/* Updated: 2025/01/27 14:14:07 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */