From 4e45d4bace2475d0f0b0aa8153c90f6d84407001 Mon Sep 17 00:00:00 2001 From: whaffman Date: Mon, 20 Jan 2025 15:56:59 +0100 Subject: [PATCH] fixed stdheaders, odd philo delay, synchro start --- philo/inc/philo.h | 122 ++++++++++++++++++--------------- philo/src/check_alive.c | 13 +++- philo/src/create_mutexes.c | 26 ++++--- philo/src/create_philos.c | 16 ++++- philo/src/create_threads.c | 14 +++- philo/src/destroy_mutexes.c | 20 ++++-- philo/src/free_philos.c | 14 +++- philo/src/join_threads.c | 18 ++++- philo/src/main.c | 28 +++++--- philo/src/parse_arguments.c | 36 ++++++---- philo/src/philo_routine.c | 47 ++++++++----- philo/src/utils/get_time.c | 16 ++++- philo/src/utils/ph_atoi.c | 12 ++++ philo/src/utils/print_rules.c | 37 ++++------ philo/src/utils/print_status.c | 21 +++++- 15 files changed, 298 insertions(+), 142 deletions(-) diff --git a/philo/inc/philo.h b/philo/inc/philo.h index 949fe08..2423b61 100644 --- a/philo/inc/philo.h +++ b/philo/inc/philo.h @@ -1,52 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* philo.h :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2025/01/20 14:27:34 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2025/01/20 14:27:34 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + #ifndef PHILO_H -# define PHILO_H -# include -# include -# include -# include -# include -# include -# include +#define PHILO_H +#include +#include +#include +#include +#include +#include +#include +#define SUCCESS 1 +#define FAILURE 0 -# define SUCCESS 1 -# define FAILURE 0 +#define START_DELAY 0 -# define START_DELAY 500 - -# define ERROR_USAGE "Usage: ./philo number_of_philosophers time_to_die " \ - "time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]\n" -# define ERROR_MUTEX "Can not create mutex" -# define ERROR_MALLOC "Malloc Failure" -# define ERROR_THREADS "Can not create threads" +#define ERROR_USAGE "Usage: ./philo number_of_philosophers time_to_die " \ + "time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]\n" +#define ERROR_MUTEX "Can not create mutex" +#define ERROR_MALLOC "Malloc Failure" +#define ERROR_THREADS "Can not create threads" typedef struct s_rules { - int n_philos; - int time_to_die; - int time_to_eat; - int time_to_sleep; - int n_must_eat; - int finished; - int start_time; - pthread_mutex_t *print_lock; - pthread_mutex_t *finished_lock; - pthread_mutex_t *forks; - struct s_philo *philos; - -} t_rules; + int n_philos; + int time_to_die; + int time_to_eat; + int time_to_sleep; + int n_must_eat; + int finished; + int start_time; + pthread_mutex_t *print_lock; + pthread_mutex_t *finished_lock; + pthread_mutex_t *forks; + struct s_philo *philos; +} t_rules; typedef struct s_philo { - int id; - pthread_t *pid; - pthread_mutex_t *l_fork; - pthread_mutex_t *r_fork; - int death; - int last_meal; - t_rules *rules; - int n_eat; -} t_philo; + int id; + pthread_t *pid; + pthread_mutex_t *l_fork; + pthread_mutex_t *r_fork; + int death; + int last_meal; + t_rules *rules; + int n_eat; +} t_philo; // memset, printf, malloc, free, write, // usleep, gettimeofday, pthread_create, @@ -54,21 +64,21 @@ typedef struct s_philo // pthread_mutex_destroy, pthread_mutex_lock, // pthread_mutex_unlock -int ph_atoi(const char *nptr, int *res); -int get_time(void); -void free_philos(t_rules *rules); -int destroy_mutexes(t_rules *rules); -int create_philos(t_rules *rules); -int parse_arguments(int argc,char *argv[], t_rules *rules); -int create_mutexes(t_rules *rules); -int create_threads(t_rules *rules); -void print_status(t_philo *philo, char *status); -void *philo_routine(void *philo); -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); +int ph_atoi(const char *nptr, int *res); +int get_time(void); +void free_philos(t_rules *rules); +int destroy_mutexes(t_rules *rules); +int create_philos(t_rules *rules); +int parse_arguments(int argc, char *argv[], t_rules *rules); +int create_mutexes(t_rules *rules); +int create_threads(t_rules *rules); +void print_status(t_philo *philo, char *status); +void *philo_routine(void *philo); +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); -#endif \ No newline at end of file +#endif diff --git a/philo/src/check_alive.c b/philo/src/check_alive.c index d6d29f7..93884a0 100644 --- a/philo/src/check_alive.c +++ b/philo/src/check_alive.c @@ -1,7 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: 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 699e9ba..39cc35f 100644 --- a/philo/src/create_mutexes.c +++ b/philo/src/create_mutexes.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* create_mutexes.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2025/01/20 14:26:44 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2025/01/20 14:27:35 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + #include "philo.h" int create_mutexes(t_rules *rules) @@ -5,20 +17,18 @@ int create_mutexes(t_rules *rules) int i; i = 0; - printf("creating mutexes\n"); if (pthread_mutex_init(rules->finished_lock, NULL)) - return(printf("death mutex created"), FAILURE); + return (FAILURE); if (pthread_mutex_init(rules->print_lock, NULL)) - return(printf("print mutex created"), FAILURE); - printf("creating forks\n"); + return (FAILURE); rules->forks = malloc(rules->n_philos * sizeof(pthread_mutex_t)); - if (!rules->forks) - return(FAILURE); + if (!rules->forks) + return (FAILURE); while (i < rules->n_philos) { if (pthread_mutex_init(&rules->forks[i], NULL)) - return(FAILURE); + return (FAILURE); i++; } return (SUCCESS); -} \ No newline at end of file +} diff --git a/philo/src/create_philos.c b/philo/src/create_philos.c index be0059a..3781045 100644 --- a/philo/src/create_philos.c +++ b/philo/src/create_philos.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* create_philos.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2025/01/20 14:26:45 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2025/01/20 14:27:36 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + #include "philo.h" int create_philos(t_rules *rules) @@ -8,7 +20,7 @@ int create_philos(t_rules *rules) i = 0; rules->philos = malloc(rules->n_philos * sizeof(t_philo)); if (!rules->philos) - return(FAILURE); + return (FAILURE); while (i < rules->n_philos) { philo = &(rules->philos[i]); @@ -22,4 +34,4 @@ int create_philos(t_rules *rules) i++; } return (SUCCESS); -} \ No newline at end of file +} diff --git a/philo/src/create_threads.c b/philo/src/create_threads.c index 1042a01..0182f2c 100644 --- a/philo/src/create_threads.c +++ b/philo/src/create_threads.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* create_threads.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2025/01/20 14:26:45 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2025/01/20 14:27:37 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + #include "philo.h" int create_threads(t_rules *rules) @@ -17,4 +29,4 @@ int create_threads(t_rules *rules) i++; } return (SUCCESS); -} \ No newline at end of file +} diff --git a/philo/src/destroy_mutexes.c b/philo/src/destroy_mutexes.c index e446f94..f73d89a 100644 --- a/philo/src/destroy_mutexes.c +++ b/philo/src/destroy_mutexes.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* destroy_mutexes.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2025/01/20 14:26:46 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2025/01/20 14:27:38 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + #include "philo.h" int destroy_mutexes(t_rules *rules) @@ -6,14 +18,14 @@ int destroy_mutexes(t_rules *rules) i = 0; if (pthread_mutex_destroy(rules->finished_lock)) - return(FAILURE); + return (FAILURE); if (pthread_mutex_destroy(rules->print_lock)) - return(FAILURE); + return (FAILURE); while (i < rules->n_philos) { if (pthread_mutex_destroy(&rules->forks[i])) - return(FAILURE); + return (FAILURE); i++; } return (SUCCESS); -} \ No newline at end of file +} diff --git a/philo/src/free_philos.c b/philo/src/free_philos.c index cc493fb..6f1b3cd 100644 --- a/philo/src/free_philos.c +++ b/philo/src/free_philos.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* free_philos.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2025/01/20 14:26:46 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2025/01/20 14:27:39 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + #include "philo.h" void free_philos(t_rules *rules) @@ -6,4 +18,4 @@ void free_philos(t_rules *rules) free(rules->forks); free(rules->print_lock); free(rules->finished_lock); -} \ No newline at end of file +} diff --git a/philo/src/join_threads.c b/philo/src/join_threads.c index 9a2baa8..6e0ec6b 100644 --- a/philo/src/join_threads.c +++ b/philo/src/join_threads.c @@ -1,9 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* join_threads.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2025/01/20 14:26:47 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2025/01/20 14:27:39 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + #include "philo.h" int join_threads(t_rules *rules) { - int i; - t_philo *philo; + int i; + t_philo *philo; i = 0; while (i < rules->n_philos) @@ -14,4 +26,4 @@ int join_threads(t_rules *rules) i++; } return (SUCCESS); -} \ No newline at end of file +} diff --git a/philo/src/main.c b/philo/src/main.c index 7afb9dc..d43c6e6 100644 --- a/philo/src/main.c +++ b/philo/src/main.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* main.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2025/01/20 14:26:48 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2025/01/20 14:27:40 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + #include "philo.h" int main(int argc, char *argv[]) @@ -5,25 +17,21 @@ int main(int argc, char *argv[]) t_rules rules; if (FAILURE == parse_arguments(argc, argv, &rules)) - return (printf(ERROR_USAGE), EXIT_FAILURE); - printf("arguments parsed\n"); + return (printf(ERROR_USAGE), EXIT_FAILURE); if (FAILURE == create_mutexes(&rules)) return (printf(ERROR_MUTEX), EXIT_FAILURE); - printf("mutexes created\n"); if (FAILURE == create_philos(&rules)) return (printf(ERROR_MALLOC), EXIT_FAILURE); - printf("philos created\n"); - print_rules(&rules); rules.start_time = get_time() + START_DELAY; + pthread_mutex_lock(rules.finished_lock); if (FAILURE == create_threads(&rules)) - return (printf(ERROR_THREADS), EXIT_FAILURE); - printf("threads created\n"); + return (printf(ERROR_THREADS), EXIT_FAILURE); + pthread_mutex_unlock(rules.finished_lock); if (FAILURE == join_threads(&rules)) return (printf(ERROR_THREADS), EXIT_FAILURE); - printf("threads joined\n"); + if (FAILURE == destroy_mutexes(&rules)) return (printf(ERROR_MUTEX), EXIT_FAILURE); - printf("mutexes destroyed\n"); free_philos(&rules); - return(EXIT_SUCCESS); + return (EXIT_SUCCESS); } diff --git a/philo/src/parse_arguments.c b/philo/src/parse_arguments.c index 2a16a84..aa431b5 100644 --- a/philo/src/parse_arguments.c +++ b/philo/src/parse_arguments.c @@ -1,27 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* parse_arguments.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2025/01/20 14:26:48 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2025/01/20 14:27:40 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + #include "philo.h" -int parse_arguments(int argc,char *argv[], t_rules *rules) +int parse_arguments(int argc, char *argv[], t_rules *rules) { rules->n_must_eat = -1; - printf("argc: %d\n", argc); - if (argc != 5 && argc != 6) - return (FAILURE); - if (!ph_atoi(argv[1], &rules->n_philos)) - return (FAILURE); - if (!ph_atoi(argv[2], &rules->time_to_die)) + if (argc != 5 && argc != 6) return (FAILURE); - if (!ph_atoi(argv[3], &rules->time_to_eat)) + if (!ph_atoi(argv[1], &rules->n_philos)) + return (FAILURE); + if (!ph_atoi(argv[2], &rules->time_to_die)) + return (FAILURE); + if (!ph_atoi(argv[3], &rules->time_to_eat)) return (FAILURE); if (!ph_atoi(argv[4], &rules->time_to_sleep)) - return (FAILURE); - if (argc == 6 && !ph_atoi(argv[5], &rules->n_must_eat)) return (FAILURE); - rules->print_lock = malloc(sizeof(pthread_mutex_t)); + if (argc == 6 && !ph_atoi(argv[5], &rules->n_must_eat)) + return (FAILURE); + rules->print_lock = malloc(sizeof(pthread_mutex_t)); rules->finished_lock = malloc(sizeof(pthread_mutex_t)); + rules->finished = 0; if (!rules->print_lock || !rules->finished_lock) return (FAILURE); rules->forks = malloc(rules->n_philos * sizeof(pthread_mutex_t)); if (!rules->forks) return (FAILURE); return (SUCCESS); -} \ No newline at end of file +} diff --git a/philo/src/philo_routine.c b/philo/src/philo_routine.c index 4f11377..211e863 100644 --- a/philo/src/philo_routine.c +++ b/philo/src/philo_routine.c @@ -1,21 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* philo_routine.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2025/01/20 14:26:49 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2025/01/20 14:27:41 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + #include "philo.h" -void set_finished(t_rules *rules) +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 get_finished(t_rules *rules) { - int finished; + int finished; pthread_mutex_lock(rules->finished_lock); finished = rules->finished; pthread_mutex_unlock(rules->finished_lock); return (finished); } -void philo_die(t_philo *philo) + +void philo_die(t_philo *philo) { print_status(philo, "died"); philo->death = 1; @@ -24,10 +37,11 @@ void philo_die(t_philo *philo) pthread_mutex_unlock(philo->r_fork); } -int philo_died_while_sleeping(t_philo *philo, int time) +int philo_died_while_sleeping(t_philo *philo, int time) { const int start = get_time(); - while(get_time() - start < time) + + while (get_time() - start < time) { if (philo->last_meal - get_time() >= philo->rules->time_to_die) return (SUCCESS); @@ -35,7 +49,7 @@ int philo_died_while_sleeping(t_philo *philo, int time) } return (FAILURE); } -int philo_eat(t_philo *philo) +int philo_eat(t_philo *philo) { if (philo->id % 2 == 1) { @@ -44,11 +58,11 @@ int philo_eat(t_philo *philo) } pthread_mutex_lock(philo->l_fork); print_status(philo, "has taken a fork"); - if (philo->id % 2 == 0) + if (philo->id % 2 != 1) { pthread_mutex_lock(philo->r_fork); print_status(philo, "has taken a fork"); - } + } if (check_death(philo)) return (philo_die(philo), FAILURE); print_status(philo, "is eating"); @@ -62,13 +76,14 @@ void *philo_routine(void *arg) t_philo *philo; philo = (t_philo *)arg; - - while (get_time() < philo->rules->start_time) - usleep(50); - while (!philo->death && !philo->rules->finished - && (philo->n_eat < philo->rules->n_must_eat || philo->rules->n_must_eat == -1)) + pthread_mutex_lock(philo->rules->finished_lock); + pthread_mutex_unlock(philo->rules->finished_lock); + if (philo->id % 2 == 1) + usleep(philo->rules->time_to_eat * 100); + while (!philo->death && !philo->rules->finished + && (philo->n_eat < philo->rules->n_must_eat + || philo->rules->n_must_eat == -1)) { - if (check_death(philo)) return (philo_die(philo), NULL); if (!philo_eat(philo)) @@ -83,4 +98,4 @@ void *philo_routine(void *arg) print_status(philo, "is thinking"); } return (NULL); -} \ No newline at end of file +} diff --git a/philo/src/utils/get_time.c b/philo/src/utils/get_time.c index 641faf3..ad86129 100644 --- a/philo/src/utils/get_time.c +++ b/philo/src/utils/get_time.c @@ -1,9 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* get_time.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2025/01/20 14:27:08 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2025/01/20 14:27:42 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + #include "philo.h" -int get_time(void) +int get_time(void) { struct timeval time; gettimeofday(&time, NULL); return (time.tv_sec * 1000 + time.tv_usec / 1000); -} \ No newline at end of file +} diff --git a/philo/src/utils/ph_atoi.c b/philo/src/utils/ph_atoi.c index e47396c..b6df97c 100644 --- a/philo/src/utils/ph_atoi.c +++ b/philo/src/utils/ph_atoi.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ph_atoi.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2025/01/20 14:27:09 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2025/01/20 14:27:42 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + #include "philo.h" static int ph_isspace(int c) diff --git a/philo/src/utils/print_rules.c b/philo/src/utils/print_rules.c index 7845ff7..c4ce46e 100644 --- a/philo/src/utils/print_rules.c +++ b/philo/src/utils/print_rules.c @@ -1,31 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* print_rules.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2025/01/20 14:27:10 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2025/01/20 14:27:43 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + #include "philo.h" -void print_rules(t_rules *rules) +void print_rules(t_rules *rules) { - int i; - printf("n_philos: %d\n", rules->n_philos); printf("time_to_die: %d\n", rules->time_to_die); printf("time_to_eat: %d\n", rules->time_to_eat); printf("time_to_sleep: %d\n", rules->time_to_sleep); printf("n_must_eat: %d\n", rules->n_must_eat); - i = 0; - while (i < rules->n_philos) - { - printf("philo %d has id: %d\n", i, rules->philos[i].id); - i++; - } - i = 0; - while (i < rules->n_philos) - { - printf("fork %d has pointer:%p\n", i, &rules->forks[i]); - i++; - } - i = 0; - while (i < rules->n_philos) - { - printf("philo %d\n has l_fork %p and r_fork: %p\n", rules->philos[i].id, rules->philos[i].l_fork, rules->philos[i].r_fork); - i++; - } - -} \ No newline at end of file +} diff --git a/philo/src/utils/print_status.c b/philo/src/utils/print_status.c index 28140c0..7f0533e 100644 --- a/philo/src/utils/print_status.c +++ b/philo/src/utils/print_status.c @@ -1,8 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* print_status.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2025/01/20 14:27:10 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2025/01/20 14:27:44 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + #include "philo.h" -void print_status(t_philo *philo, char *status) +void print_status(t_philo *philo, char *status) { pthread_mutex_lock(philo->rules->print_lock); - printf("%d %d %s\n", get_time() - philo->rules->start_time, philo->id, status); + printf("%d %d %s\n", + get_time() - philo->rules->start_time, + philo->id, + status); pthread_mutex_unlock(philo->rules->print_lock); -} \ No newline at end of file +}