From: Lukas Jiriste Date: Tue, 19 Mar 2024 13:20:48 +0000 (+0100) Subject: Replace string literals with global string consts X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=9b38aec7f19c24ba2f63446e3e9d03f69655396a;p=42%2Fphilosophers.git Replace string literals with global string consts This is done to have a consistent output for special cases as changes have to only be made in one spot. --- diff --git a/philo/main.c b/philo/main.c index f24c5ca..e32081d 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/19 14:14:15 by ljiriste ### ########.fr */ +/* Updated: 2024/03/19 14:20:33 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,11 @@ #include static const char *g_fed_str = "All philosophers are well fed"; +static const char *g_think_str = "is thinking"; +static const char *g_fork_str = "has taken a fork"; +static const char *g_eat_str = "is eating"; +static const char *g_sleep_str = "is sleeping"; +static 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 typedef struct s_settings @@ -64,24 +69,24 @@ void thread_print(t_settings *set, size_t id, const char *message) void philo_sleep(t_settings *set, size_t id) { - thread_print(set, id, "is sleeping"); + thread_print(set, id, g_sleep_str); usleep(set->time_to_sleep); return ; } void philo_think(t_settings *set, size_t id) { - thread_print(set, id, "is thinking"); + thread_print(set, id, g_think_str); return ; } void philo_eat(t_philo *philo) { pthread_mutex_lock(philo->forks[0]); - thread_print(philo->settings, philo->id, "has taken a fork"); + thread_print(philo->settings, philo->id, g_fork_str); pthread_mutex_lock(philo->forks[1]); - thread_print(philo->settings, philo->id, "has taken a fork"); - thread_print(philo->settings, philo->id, "is eating"); + thread_print(philo->settings, philo->id, g_fork_str); + thread_print(philo->settings, philo->id, g_eat_str); philo->last_eaten = cur_time_sus(); usleep(philo->settings->time_to_eat); ++philo->times_eaten; @@ -321,7 +326,7 @@ void check_death(t_philo *philos, t_settings *set) if((now - philos[i].last_eaten) > set->time_to_die) { pthread_mutex_lock(&set->terminal); - printf("%li %lu %s\n", (cur_time_sus() - set->program_start) / 1000, i + 1, "died"); + printf("%li %lu %s\n", (cur_time_sus() - set->program_start) / 1000, i + 1, g_die_str); set->end = 1; pthread_mutex_unlock(&set->terminal); return ; @@ -347,10 +352,10 @@ void check_death(t_philo *philos, t_settings *set) int handle_single_philo(t_settings *set) { set->program_start = cur_time_sus(); - thread_print(set, 1, "is thinking"); - thread_print(set, 1, "has taken a fork"); + thread_print(set, 1, g_think_str); + thread_print(set, 1, g_fork_str); usleep(set->time_to_die); - thread_print(set, 1, "died"); + thread_print(set, 1, g_die_str); return (0); }