From 5ae2e1ac5de24f7042adfed4871b6eb442450884 Mon Sep 17 00:00:00 2001 From: biralavor Date: Wed, 30 Oct 2024 19:04:50 -0300 Subject: [PATCH 01/16] updating variable names to plural --- philo/headers/philo.h | 9 +++---- philo/src/03.parsing.c | 8 +++---- philo/src/04.table_holder.c | 8 +++---- philo/src/07.table_philo_init.c | 42 ++++++++++++++++----------------- philo/src/08.dinner_manager.c | 29 +++++++++++------------ 5 files changed, 48 insertions(+), 48 deletions(-) diff --git a/philo/headers/philo.h b/philo/headers/philo.h index 5a695ae..45014c6 100644 --- a/philo/headers/philo.h +++ b/philo/headers/philo.h @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 16:04:11 by umeneses #+# #+# */ -/* Updated: 2024/10/30 16:40:57 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 19:02:36 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -98,10 +98,10 @@ struct s_table bool this_is_the_end; bool all_threads_ready_togo; t_set *set; - t_philo *philo; + t_philo *philos; pthread_mutex_t *table_mtx; pthread_mutex_t *printer_mtx; - t_chops *chopstick; + t_chops *chopsticks; }; /* validation functions */ @@ -142,7 +142,8 @@ bool this_is_the_end_of_dinner(t_table *table); /* dinner manager functions */ void semaphore_like_for_threads(t_table *table); void dinner_manager(t_table *table); -void *dinner_routine(void *arg); +void *dinner_runner(void *data); +// void *dinner_routine(void *arg); void printer_with_mutex(t_philo_status status, t_philo *philo, bool debug); void printer_with_mutex_debug(t_philo_status status, t_philo *philo, bool debbug); diff --git a/philo/src/03.parsing.c b/philo/src/03.parsing.c index ed079ce..a202250 100644 --- a/philo/src/03.parsing.c +++ b/philo/src/03.parsing.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/22 16:16:16 by umeneses #+# #+# */ -/* Updated: 2024/10/30 16:08:25 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 19:02:59 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,9 +16,9 @@ t_table *table_alloc(t_table *table, char **av) { table = ft_safe_malloc(sizeof(t_table)); table->set = ft_safe_malloc(sizeof(t_set)); - table->philo = ft_safe_malloc(sizeof(t_philo) * ft_atoi_long_int(av[1])); - table->chopstick = ft_safe_malloc(sizeof(t_philo) * ft_atoi_long_int(av[1])); - table->philo->th_id = ft_safe_malloc(sizeof(pthread_t) + table->philos = ft_safe_malloc(sizeof(t_philo) * ft_atoi_long_int(av[1])); + table->chopsticks = ft_safe_malloc(sizeof(t_philo) * ft_atoi_long_int(av[1])); + table->philos->th_id = ft_safe_malloc(sizeof(pthread_t) * ft_atoi_long_int(av[1])); table->table_mtx = ft_safe_malloc(sizeof(pthread_mutex_t)); table->printer_mtx = ft_safe_malloc(sizeof(pthread_mutex_t)); diff --git a/philo/src/04.table_holder.c b/philo/src/04.table_holder.c index 8be2292..fd1f2bb 100644 --- a/philo/src/04.table_holder.c +++ b/philo/src/04.table_holder.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/28 15:40:26 by umeneses #+# #+# */ -/* Updated: 2024/10/30 16:08:47 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 19:02:47 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,11 +23,11 @@ t_table *table_holder(t_table *table, bool destroy) pthread_mutex_destroy(table_holder->table_mtx); pthread_mutex_destroy(table_holder->printer_mtx); free(table_holder->set); - free(table_holder->philo->th_id); - free(table_holder->philo); + free(table_holder->philos->th_id); + free(table_holder->philos); free(table_holder->printer_mtx); free(table_holder->table_mtx); - free(table_holder->chopstick); + free(table_holder->chopsticks); free(table_holder); table_holder = NULL; } diff --git a/philo/src/07.table_philo_init.c b/philo/src/07.table_philo_init.c index 11ce046..0216eae 100644 --- a/philo/src/07.table_philo_init.c +++ b/philo/src/07.table_philo_init.c @@ -6,25 +6,25 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/22 16:17:52 by umeneses #+# #+# */ -/* Updated: 2024/10/30 16:24:44 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 19:03:18 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" -void get_chopsticks(t_philo *philo, t_chops *chopstick, int philo_pos) +void get_chopsticks(t_philo *philo, t_chops *chopsticks, int philo_pos) { if (philo->id % 2 == 0) { - philo->first_chops = &chopstick[philo_pos]; - philo->second_chops = &chopstick[(philo_pos + 1) + philo->first_chops = &chopsticks[philo_pos]; + philo->second_chops = &chopsticks[(philo_pos + 1) % philo->table->set->total_philos]; } else { - philo->first_chops = &chopstick[(philo_pos + 1) + philo->first_chops = &chopsticks[(philo_pos + 1) % philo->table->set->total_philos]; - philo->second_chops = &chopstick[philo_pos]; + philo->second_chops = &chopsticks[philo_pos]; } } @@ -36,53 +36,53 @@ void philo_init_runner(t_table *table) idx = -1; while (++idx < table->set->total_philos) { - philo = table->philo + idx; + philo = table->philos + idx; philo->id = idx + 1; philo->got_meals = 0; philo->full = false; philo->table = table; safe_mutex_handler(philo->philo_mtx, INIT); - get_chopsticks(philo, &table->chopstick[idx], idx); + get_chopsticks(philo, &table->chopsticks[idx], idx); } } void table_init(t_table *table) { int idx; - int *res; - int *arg_to_routine; + // int *res; + // int *arg_to_routine; + // res = 0; idx = -1; - res = 0; table->this_is_the_end = false; safe_mutex_handler(table->table_mtx, INIT); safe_mutex_handler(table->printer_mtx, INIT); while (++idx < table->set->total_philos) { - safe_mutex_handler(&table->chopstick[idx].chops_mtx, INIT); - table->chopstick[idx].chops_id = idx; + safe_mutex_handler(&table->chopsticks[idx].chops_mtx, INIT); + table->chopsticks[idx].chops_id = idx; - arg_to_routine = malloc(sizeof(int)); // it will be freed inside routine called by pthread_create() - *arg_to_routine = idx; - safe_thread_handler(&table->philo->th_id[idx], &dinner_routine, arg_to_routine, CREATE); + // arg_to_routine = malloc(sizeof(int)); // it will be freed inside routine called by pthread_create() + // *arg_to_routine = idx; + // safe_thread_handler(&table->philo->th_id[idx], &dinner_routine, arg_to_routine, CREATE); // if (pthread_create(&table->philo->th_id[idx], NULL, &routine, arg_to_routine) != 0) // { // perror("pthread_create error"); // return (1); // } - printf("Thread %d has started\n", idx); + // printf("Thread %d has started\n", idx); } idx = -1; while (++idx < table->set->total_philos) { - safe_thread_handler(&table->philo->th_id[idx], NULL, (void **) &res, JOIN); + safe_thread_handler(&table->philos->th_id[idx], NULL, NULL, JOIN); // if (pthread_join(table->philo->th_id[idx], (void **) &res) != 0) // { // perror("pthread_join error"); // return (3); // } - printf("Thread %d has finished\n", idx); - printf(">>> total mails from res = %d !\n", *res); - free(res); + // printf("Thread %d has finished\n", idx); + // printf(">>> total mails from res = %d !\n", *res); + // free(res); } } diff --git a/philo/src/08.dinner_manager.c b/philo/src/08.dinner_manager.c index a459596..be3826b 100644 --- a/philo/src/08.dinner_manager.c +++ b/philo/src/08.dinner_manager.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/29 10:01:45 by umeneses #+# #+# */ -/* Updated: 2024/10/30 16:39:45 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 19:03:58 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,33 +32,32 @@ void dinner_manager(t_table *table) // CREAT all threads while(++idx < table->set->total_philos) { - safe_thread_handler(table->philo[idx].th_id, dinner_routine, &table->philo[idx], CREATE); + safe_thread_handler(table->philos[idx].th_id, dinner_runner, &table->philos[idx], CREATE); } } - // starting the simulation table->start_time = ft_gettime(MILISSECOND); - // All threads are ready set_bool(table->table_mtx, &table->all_threads_ready_togo, true); idx = -1; - // JOIN all threads while (++idx < table->set->total_philos) { - safe_thread_handler(table->philo[idx].th_id, NULL, NULL, JOIN); + safe_thread_handler(table->philos[idx].th_id, NULL, NULL, JOIN); } } -void dinner_startup_runner(t_table *table) +void *dinner_runner(void *data) { - //avoid race condition - semaphore_like_for_threads(table); + t_philo *philo; - // set meals time - while (!this_is_the_end_of_dinner(table)) + philo = (t_philo *)data; + //avoid race condition + semaphore_like_for_threads(philo->table); + while (!this_is_the_end_of_dinner(philo->table)) { - if (table->philo->full) // TODO: thread safe + if (philo->full) // TODO: thread safe break ; - let_philo_eat_routine(table->philo); - let_philo_think_routine(table->philo); - let_philo_sleep_routine(table->philo); + let_philo_eat_routine(philo); + let_philo_think_routine(philo); + let_philo_sleep_routine(philo); } + return (NULL); } From 9ce57cfc4fc5bc9238a5a83fca0182fd9dd1f314 Mon Sep 17 00:00:00 2001 From: biralavor Date: Wed, 30 Oct 2024 19:09:12 -0300 Subject: [PATCH 02/16] adding philo_init_runner intop table_init --- philo/src/07.table_philo_init.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/philo/src/07.table_philo_init.c b/philo/src/07.table_philo_init.c index 0216eae..dd541b8 100644 --- a/philo/src/07.table_philo_init.c +++ b/philo/src/07.table_philo_init.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/22 16:17:52 by umeneses #+# #+# */ -/* Updated: 2024/10/30 19:03:18 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 19:07:50 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -72,6 +72,7 @@ void table_init(t_table *table) // } // printf("Thread %d has started\n", idx); } + philo_init_runner(table); idx = -1; while (++idx < table->set->total_philos) { From 6778a413f9b1ca3d2d766683452a696eee22b562 Mon Sep 17 00:00:00 2001 From: biralavor Date: Wed, 30 Oct 2024 19:12:24 -0300 Subject: [PATCH 03/16] fixing get_chopsticks parameters --- philo/src/07.table_philo_init.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/philo/src/07.table_philo_init.c b/philo/src/07.table_philo_init.c index dd541b8..c7dc031 100644 --- a/philo/src/07.table_philo_init.c +++ b/philo/src/07.table_philo_init.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/22 16:17:52 by umeneses #+# #+# */ -/* Updated: 2024/10/30 19:07:50 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 19:11:42 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,7 +30,7 @@ void get_chopsticks(t_philo *philo, t_chops *chopsticks, int philo_pos) void philo_init_runner(t_table *table) { - int idx; + int idx; t_philo *philo; idx = -1; @@ -42,7 +42,7 @@ void philo_init_runner(t_table *table) philo->full = false; philo->table = table; safe_mutex_handler(philo->philo_mtx, INIT); - get_chopsticks(philo, &table->chopsticks[idx], idx); + get_chopsticks(philo, table->chopsticks, idx); } } From 45ef3271850ac28409df6bdc56dc4ff5dc3b054d Mon Sep 17 00:00:00 2001 From: biralavor Date: Wed, 30 Oct 2024 19:24:08 -0300 Subject: [PATCH 04/16] clearing comments --- philo/src/07.table_philo_init.c | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/philo/src/07.table_philo_init.c b/philo/src/07.table_philo_init.c index c7dc031..2482745 100644 --- a/philo/src/07.table_philo_init.c +++ b/philo/src/07.table_philo_init.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/22 16:17:52 by umeneses #+# #+# */ -/* Updated: 2024/10/30 19:11:42 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 19:15:42 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,10 +49,7 @@ void philo_init_runner(t_table *table) void table_init(t_table *table) { int idx; - // int *res; - // int *arg_to_routine; - // res = 0; idx = -1; table->this_is_the_end = false; safe_mutex_handler(table->table_mtx, INIT); @@ -61,29 +58,12 @@ void table_init(t_table *table) { safe_mutex_handler(&table->chopsticks[idx].chops_mtx, INIT); table->chopsticks[idx].chops_id = idx; - - // arg_to_routine = malloc(sizeof(int)); // it will be freed inside routine called by pthread_create() - // *arg_to_routine = idx; - // safe_thread_handler(&table->philo->th_id[idx], &dinner_routine, arg_to_routine, CREATE); - // if (pthread_create(&table->philo->th_id[idx], NULL, &routine, arg_to_routine) != 0) - // { - // perror("pthread_create error"); - // return (1); - // } - // printf("Thread %d has started\n", idx); } philo_init_runner(table); idx = -1; while (++idx < table->set->total_philos) { safe_thread_handler(&table->philos->th_id[idx], NULL, NULL, JOIN); - // if (pthread_join(table->philo->th_id[idx], (void **) &res) != 0) - // { - // perror("pthread_join error"); - // return (3); - // } - // printf("Thread %d has finished\n", idx); - // printf(">>> total mails from res = %d !\n", *res); - // free(res); + } } From 62b861def6b6ad7a691382765e4d0c67df2347d1 Mon Sep 17 00:00:00 2001 From: biralavor Date: Wed, 30 Oct 2024 19:24:35 -0300 Subject: [PATCH 05/16] fixing forgotten prototype --- philo/headers/philo.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/philo/headers/philo.h b/philo/headers/philo.h index 45014c6..21a6cfa 100644 --- a/philo/headers/philo.h +++ b/philo/headers/philo.h @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 16:04:11 by umeneses #+# #+# */ -/* Updated: 2024/10/30 19:02:36 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 19:23:32 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -125,6 +125,7 @@ void error_mutex_handler(int status, int opcode); /* thread handler functions*/ void safe_thread_handler(pthread_t *th_id, void *(*func_ptr)(void *), \ void *data, t_mtx_opcode opcode); +void error_pthread_handler(int status, int opcode); /* table initialization */ void table_parsing(t_table *table, char **av); From 5977978a3c49b3bb573c5d0676d72adeb3d7aec5 Mon Sep 17 00:00:00 2001 From: biralavor Date: Wed, 30 Oct 2024 20:33:05 -0300 Subject: [PATCH 06/16] testing dinner_manager --- philo/src/01.main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/philo/src/01.main.c b/philo/src/01.main.c index 1e5a6c1..3d9c669 100644 --- a/philo/src/01.main.c +++ b/philo/src/01.main.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 16:03:55 by umeneses #+# #+# */ -/* Updated: 2024/10/30 16:09:03 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 16:43:29 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,7 +39,7 @@ int main(int ac, char **av) table_parsing(table, av); table = table_holder(NULL, false); table_init(table); - // dinner_manager(table); + dinner_manager(table); table_holder(NULL, true); exit(EXIT_SUCCESS); } From 127823aa1b5c8f5622bb515c1244dbb3f09705c5 Mon Sep 17 00:00:00 2001 From: biralavor Date: Wed, 30 Oct 2024 20:33:40 -0300 Subject: [PATCH 07/16] fixing logic sequence --- philo/src/08.dinner_manager.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/philo/src/08.dinner_manager.c b/philo/src/08.dinner_manager.c index be3826b..e324800 100644 --- a/philo/src/08.dinner_manager.c +++ b/philo/src/08.dinner_manager.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/29 10:01:45 by umeneses #+# #+# */ -/* Updated: 2024/10/30 19:03:58 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 20:28:36 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,15 +49,15 @@ void *dinner_runner(void *data) t_philo *philo; philo = (t_philo *)data; - //avoid race condition + //semaphore for syncronization semaphore_like_for_threads(philo->table); while (!this_is_the_end_of_dinner(philo->table)) { if (philo->full) // TODO: thread safe break ; let_philo_eat_routine(philo); - let_philo_think_routine(philo); let_philo_sleep_routine(philo); + let_philo_think_routine(philo); } return (NULL); } From ca2fd3b0975a2ca9b285c899915c14316a08a0be Mon Sep 17 00:00:00 2001 From: biralavor Date: Wed, 30 Oct 2024 23:29:53 -0300 Subject: [PATCH 08/16] fixing pointer initalization --- philo/headers/philo.h | 17 +++++++------ philo/src/01.main.c | 13 +++++----- philo/src/03.parsing.c | 42 +++++++++++++++---------------- philo/src/04.table_holder.c | 16 ++++++------ philo/src/06.pthread_handlers.c | 4 +-- philo/src/07.table_philo_init.c | 26 +++++++++---------- philo/src/08.dinner_manager.c | 23 +++++++++-------- philo/src/09.getters_setters.c | 4 +-- philo/src/10.synchronizers.c | 4 +-- philo/src/11.printer_with_mutex.c | 8 +++--- philo/src/12.dinner_routines.c | 14 +++++------ philo/src/error_manager.c | 4 +-- 12 files changed, 87 insertions(+), 88 deletions(-) diff --git a/philo/headers/philo.h b/philo/headers/philo.h index 21a6cfa..55db088 100644 --- a/philo/headers/philo.h +++ b/philo/headers/philo.h @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 16:04:11 by umeneses #+# #+# */ -/* Updated: 2024/10/30 19:23:32 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 22:56:36 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,7 +62,6 @@ typedef enum e_philo_status DEAD, } t_philo_status; -typedef struct s_table t_table; typedef struct s_set { @@ -79,14 +78,16 @@ typedef struct s_chopstick pthread_mutex_t chops_mtx; } t_chops; +typedef struct s_table t_table; + typedef struct s_philo { int id; long got_meals; long time_of_last_meal; bool full; - pthread_mutex_t *philo_mtx; - pthread_t *th_id; + pthread_mutex_t philo_mtx; + pthread_t th_id; t_chops *first_chops; t_chops *second_chops; t_table *table; @@ -97,10 +98,10 @@ struct s_table long start_time; bool this_is_the_end; bool all_threads_ready_togo; - t_set *set; + t_set set; t_philo *philos; - pthread_mutex_t *table_mtx; - pthread_mutex_t *printer_mtx; + pthread_mutex_t table_mtx; + pthread_mutex_t printer_mtx; t_chops *chopsticks; }; @@ -113,7 +114,7 @@ bool is_valid_length_number(char **av); bool is_number_under_intmax(int ac, char **av); /* memory allocation functions */ -t_table *table_alloc(t_table *table, char **av); +t_table *table_alloc(t_table *table); void *ft_safe_malloc(size_t size); void error_manager(const char *error_msg); t_table *table_holder(t_table *table, bool destroy); diff --git a/philo/src/01.main.c b/philo/src/01.main.c index 3d9c669..f5a083b 100644 --- a/philo/src/01.main.c +++ b/philo/src/01.main.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 16:03:55 by umeneses #+# #+# */ -/* Updated: 2024/10/30 16:43:29 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 22:55:34 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,15 +31,14 @@ void *dinner_routine(void *arg) int main(int ac, char **av) { - t_table *table; + t_table table; - table = NULL; if (!arguments_validation_manager(ac, av)) exit(EXIT_FAILURE); - table_parsing(table, av); - table = table_holder(NULL, false); - table_init(table); - dinner_manager(table); + table_parsing(&table, av); + // table = *table_holder(NULL, false); + table_init(&table); + dinner_manager(&table); table_holder(NULL, true); exit(EXIT_SUCCESS); } diff --git a/philo/src/03.parsing.c b/philo/src/03.parsing.c index a202250..5c6f738 100644 --- a/philo/src/03.parsing.c +++ b/philo/src/03.parsing.c @@ -6,22 +6,23 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/22 16:16:16 by umeneses #+# #+# */ -/* Updated: 2024/10/30 19:02:59 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 23:28:53 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" -t_table *table_alloc(t_table *table, char **av) +t_table *table_alloc(t_table *table) { - table = ft_safe_malloc(sizeof(t_table)); - table->set = ft_safe_malloc(sizeof(t_set)); - table->philos = ft_safe_malloc(sizeof(t_philo) * ft_atoi_long_int(av[1])); - table->chopsticks = ft_safe_malloc(sizeof(t_philo) * ft_atoi_long_int(av[1])); - table->philos->th_id = ft_safe_malloc(sizeof(pthread_t) - * ft_atoi_long_int(av[1])); - table->table_mtx = ft_safe_malloc(sizeof(pthread_mutex_t)); - table->printer_mtx = ft_safe_malloc(sizeof(pthread_mutex_t)); + // table = ft_safe_malloc(sizeof(t_table)); + // table->set = ft_safe_malloc(sizeof(t_set)); + table->philos = ft_safe_malloc(sizeof(t_philo) * table->set.total_philos); + table->chopsticks = ft_safe_malloc(sizeof(t_chops) * table->set.total_philos); + + // table->philos->th_id = ft_safe_malloc(sizeof(pthread_t)); + // table->philos->philo_mtx = ft_safe_malloc(sizeof(pthread_mutex_t)); + // table->table_mtx = ft_safe_malloc(sizeof(pthread_mutex_t)); + // table->printer_mtx = ft_safe_malloc(sizeof(pthread_mutex_t)); return (table); } @@ -30,18 +31,17 @@ t_table *table_alloc(t_table *table, char **av) */ void table_parsing(t_table *table, char **av) { - table = table_alloc(table, av); - table->set->total_philos = ft_atoi_long_int(av[1]); - table->set->time_to_die = ft_atoi_long_int(av[2]) * 1e3; - table->set->time_to_eat = ft_atoi_long_int(av[3]) * 1e3; - table->set->time_to_sleep = ft_atoi_long_int(av[4]) * 1e3; + table->set.total_philos = ft_atoi_long_int(av[1]); + table->set.time_to_die = ft_atoi_long_int(av[2]) * 1e3; + table->set.time_to_eat = ft_atoi_long_int(av[3]) * 1e3; + table->set.time_to_sleep = ft_atoi_long_int(av[4]) * 1e3; if (av[5]) - table->set->total_meals = ft_atoi_long_int(av[5]); + table->set.total_meals = ft_atoi_long_int(av[5]); else - table->set->total_meals = -1; - table = table_holder(table, false); - if (table->set->time_to_die < 6e4 - || table->set->time_to_eat < 6e4 - || table->set->time_to_sleep < 6e4) + table->set.total_meals = -1; + // table = table_holder(table, false); + if (table->set.time_to_die < 6e4 + || table->set.time_to_eat < 6e4 + || table->set.time_to_sleep < 6e4) error_manager("Time must be at least 60ms.\n"); } diff --git a/philo/src/04.table_holder.c b/philo/src/04.table_holder.c index fd1f2bb..45169e4 100644 --- a/philo/src/04.table_holder.c +++ b/philo/src/04.table_holder.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/28 15:40:26 by umeneses #+# #+# */ -/* Updated: 2024/10/30 19:02:47 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 22:47:03 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,15 +20,15 @@ t_table *table_holder(t_table *table, bool destroy) table_holder = table; else if (table_holder && destroy) { - pthread_mutex_destroy(table_holder->table_mtx); - pthread_mutex_destroy(table_holder->printer_mtx); - free(table_holder->set); - free(table_holder->philos->th_id); + // pthread_mutex_destroy(table_holder->table_mtx); + // pthread_mutex_destroy(table_holder->printer_mtx); + // free(table_holder->set); + // free(table_holder->philos->th_id); free(table_holder->philos); - free(table_holder->printer_mtx); - free(table_holder->table_mtx); + // free(table_holder->printer_mtx); + // free(table_holder->table_mtx); free(table_holder->chopsticks); - free(table_holder); + // free(table_holder); table_holder = NULL; } return (table_holder); diff --git a/philo/src/06.pthread_handlers.c b/philo/src/06.pthread_handlers.c index 3fc6e79..e140310 100644 --- a/philo/src/06.pthread_handlers.c +++ b/philo/src/06.pthread_handlers.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/28 15:16:28 by umeneses #+# #+# */ -/* Updated: 2024/10/28 16:51:30 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 23:22:01 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ void error_pthread_handler(int status, int opcode) specifies the calling thread.\n"); } -void safe_thread_handler(pthread_t *th_id, void *(*func_ptr)(void *), \ +void safe_thread_handler(pthread_t *th_id, void *(*func_ptr)(void *), void *data, t_mtx_opcode opcode) { if (CREATE == opcode) diff --git a/philo/src/07.table_philo_init.c b/philo/src/07.table_philo_init.c index 2482745..c820d84 100644 --- a/philo/src/07.table_philo_init.c +++ b/philo/src/07.table_philo_init.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/22 16:17:52 by umeneses #+# #+# */ -/* Updated: 2024/10/30 19:15:42 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 23:28:45 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,12 +18,12 @@ void get_chopsticks(t_philo *philo, t_chops *chopsticks, int philo_pos) { philo->first_chops = &chopsticks[philo_pos]; philo->second_chops = &chopsticks[(philo_pos + 1) - % philo->table->set->total_philos]; + % philo->table->set.total_philos]; } else { philo->first_chops = &chopsticks[(philo_pos + 1) - % philo->table->set->total_philos]; + % philo->table->set.total_philos]; philo->second_chops = &chopsticks[philo_pos]; } } @@ -34,14 +34,15 @@ void philo_init_runner(t_table *table) t_philo *philo; idx = -1; - while (++idx < table->set->total_philos) + while (++idx < table->set.total_philos) { philo = table->philos + idx; philo->id = idx + 1; philo->got_meals = 0; + philo->th_id = 0; philo->full = false; philo->table = table; - safe_mutex_handler(philo->philo_mtx, INIT); + safe_mutex_handler(&philo->philo_mtx, INIT); get_chopsticks(philo, table->chopsticks, idx); } } @@ -51,19 +52,16 @@ void table_init(t_table *table) int idx; idx = -1; + table->all_threads_ready_togo = false; table->this_is_the_end = false; - safe_mutex_handler(table->table_mtx, INIT); - safe_mutex_handler(table->printer_mtx, INIT); - while (++idx < table->set->total_philos) + table->start_time = 0; + table_alloc(table); + safe_mutex_handler(&table->table_mtx, INIT); + safe_mutex_handler(&table->printer_mtx, INIT); + while (++idx < table->set.total_philos) { safe_mutex_handler(&table->chopsticks[idx].chops_mtx, INIT); table->chopsticks[idx].chops_id = idx; } philo_init_runner(table); - idx = -1; - while (++idx < table->set->total_philos) - { - safe_thread_handler(&table->philos->th_id[idx], NULL, NULL, JOIN); - - } } diff --git a/philo/src/08.dinner_manager.c b/philo/src/08.dinner_manager.c index e324800..d56fa67 100644 --- a/philo/src/08.dinner_manager.c +++ b/philo/src/08.dinner_manager.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/29 10:01:45 by umeneses #+# #+# */ -/* Updated: 2024/10/30 20:28:36 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 23:23:10 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,31 +17,32 @@ void dinner_manager(t_table *table) int idx; idx = -1; - if (table->set->total_meals == -1) + if (table->set.total_meals == -1) { // TODO } - else if (table->set->total_meals > 0) + else if (table->set.total_meals > 0) return ; // back to main and clean - else if (table->set->total_meals == 1) + else if (table->set.total_philos == 1) { - // TODO ad hoc specific function for one meal + // TODO ad hoc specific function for one meal (or philo) } else { - // CREAT all threads - while(++idx < table->set->total_philos) + // CREATE all threads + while(++idx < table->set.total_philos) { - safe_thread_handler(table->philos[idx].th_id, dinner_runner, &table->philos[idx], CREATE); + safe_thread_handler(&table->philos[idx].th_id, dinner_runner, &table->philos[idx], CREATE); } } table->start_time = ft_gettime(MILISSECOND); - set_bool(table->table_mtx, &table->all_threads_ready_togo, true); + set_bool(&table->table_mtx, &table->all_threads_ready_togo, true); idx = -1; - while (++idx < table->set->total_philos) + while (++idx < table->set.total_philos) { - safe_thread_handler(table->philos[idx].th_id, NULL, NULL, JOIN); + safe_thread_handler(&table->philos[idx].th_id, NULL, NULL, JOIN); } + set_bool(&table->table_mtx, &table->this_is_the_end, true); } void *dinner_runner(void *data) diff --git a/philo/src/09.getters_setters.c b/philo/src/09.getters_setters.c index 49490e8..a699251 100644 --- a/philo/src/09.getters_setters.c +++ b/philo/src/09.getters_setters.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/29 15:16:51 by umeneses #+# #+# */ -/* Updated: 2024/10/29 15:34:57 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 22:48:50 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,6 +52,6 @@ bool this_is_the_end_of_dinner(t_table *table) { bool result; - result = get_bool(table->table_mtx, &table->this_is_the_end); + result = get_bool(&table->table_mtx, &table->this_is_the_end); return (result); } diff --git a/philo/src/10.synchronizers.c b/philo/src/10.synchronizers.c index 4152d85..f9c25a4 100644 --- a/philo/src/10.synchronizers.c +++ b/philo/src/10.synchronizers.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/29 15:42:40 by umeneses #+# #+# */ -/* Updated: 2024/10/29 15:53:57 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 22:49:02 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,6 @@ void semaphore_like_for_threads(t_table *table) { - while (!get_bool(table->table_mtx, &table->all_threads_ready_togo)) + while (!get_bool(&table->table_mtx, &table->all_threads_ready_togo)) ; } diff --git a/philo/src/11.printer_with_mutex.c b/philo/src/11.printer_with_mutex.c index 62267d8..a2ecda9 100644 --- a/philo/src/11.printer_with_mutex.c +++ b/philo/src/11.printer_with_mutex.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/30 11:12:00 by umeneses #+# #+# */ -/* Updated: 2024/10/30 13:36:27 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 22:49:15 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ void printer_with_mutex(t_philo_status status, t_philo *philo, bool debug) elapsed = ft_gettime(MILISSECOND) - philo->table->start_time; if (philo->full) return ; - safe_mutex_handler(philo->table->printer_mtx, LOCK); + safe_mutex_handler(&philo->table->printer_mtx, LOCK); if (debug) return ; // printer_with_mutex_debug(status, philo, elapsed); else if (!this_is_the_end_of_dinner(philo->table)) @@ -31,7 +31,7 @@ void printer_with_mutex(t_philo_status status, t_philo *philo, bool debug) printf(GREEN"%-6ld"RESET" philo %d is eating\n", elapsed, philo->id); else if (SLEEPING == status) - printf(YELLOW"%-6ld"RESET" philo %d is sleeping\n", + printf(BLUE"%-6ld"RESET" philo %d is sleeping\n", elapsed, philo->id); else if (THINKING == status) printf(YELLOW"%-6ld"RESET" philo %d is thinking\n", @@ -40,7 +40,7 @@ void printer_with_mutex(t_philo_status status, t_philo *philo, bool debug) printf(RED"%-6ld"RESET" philo %d is dead\n", elapsed, philo->id); } - safe_mutex_handler(philo->table->printer_mtx, UNLOCK); + safe_mutex_handler(&philo->table->printer_mtx, UNLOCK); } void printer_with_mutex_debug(t_philo_status status, t_philo *philo, bool debbug) diff --git a/philo/src/12.dinner_routines.c b/philo/src/12.dinner_routines.c index e4d5ed0..4c6a1b4 100644 --- a/philo/src/12.dinner_routines.c +++ b/philo/src/12.dinner_routines.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/30 16:19:23 by umeneses #+# #+# */ -/* Updated: 2024/10/30 16:41:36 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 22:49:25 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,13 +19,13 @@ void let_philo_eat_routine(t_philo *philo) safe_mutex_handler(&philo->second_chops->chops_mtx, LOCK); printer_with_mutex(GOT_2ND_CHOPSTICK, philo, false); - set_long(philo->philo_mtx, &philo->time_of_last_meal, ft_gettime(MILISSECOND)); + set_long(&philo->philo_mtx, &philo->time_of_last_meal, ft_gettime(MILISSECOND)); philo->got_meals++; printer_with_mutex(EATING, philo, false); - precise_usleep(philo->table->set->time_to_eat, philo->table); - if (philo->table->set->total_meals > 0 - && philo->got_meals == philo->table->set->total_meals) - set_bool(philo->philo_mtx, &philo->full, true); + precise_usleep(philo->table->set.time_to_eat, philo->table); + if (philo->table->set.total_meals > 0 + && philo->got_meals == philo->table->set.total_meals) + set_bool(&philo->philo_mtx, &philo->full, true); safe_mutex_handler(&philo->first_chops->chops_mtx, UNLOCK); safe_mutex_handler(&philo->second_chops->chops_mtx, UNLOCK); } @@ -40,5 +40,5 @@ void let_philo_think_routine(t_philo *philo) void let_philo_sleep_routine(t_philo *philo) { printer_with_mutex(SLEEPING, philo, false); - precise_usleep(philo->table->set->time_to_sleep, philo->table); + precise_usleep(philo->table->set.time_to_sleep, philo->table); } diff --git a/philo/src/error_manager.c b/philo/src/error_manager.c index 6deebdf..4a5d0da 100644 --- a/philo/src/error_manager.c +++ b/philo/src/error_manager.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/25 20:33:35 by umeneses #+# #+# */ -/* Updated: 2024/10/28 14:01:39 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 22:36:06 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,6 @@ void error_manager(const char *error_msg) write(STDERR_FILENO, "Error.\n", 8); write(STDERR_FILENO, error_msg, ft_strlen(error_msg)); write(STDERR_FILENO, RESET, ft_strlen(RESET)); - table_holder(NULL, true); + // table_holder(NULL, true); exit(EXIT_FAILURE); } From 46b315711612708ffe680bebef47b3748c3df0bf Mon Sep 17 00:00:00 2001 From: biralavor Date: Thu, 31 Oct 2024 18:39:35 -0300 Subject: [PATCH 09/16] fixing errror messages --- philo/src/06.pthread_handlers.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/philo/src/06.pthread_handlers.c b/philo/src/06.pthread_handlers.c index e140310..45f1e32 100644 --- a/philo/src/06.pthread_handlers.c +++ b/philo/src/06.pthread_handlers.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/28 15:16:28 by umeneses #+# #+# */ -/* Updated: 2024/10/30 23:22:01 by umeneses ### ########.fr */ +/* Updated: 2024/10/30 23:36:07 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,11 +25,11 @@ void error_pthread_handler(int status, int opcode) else if (EINVAL == status && (JOIN == opcode || DETACH == opcode)) error_manager("The value specified by thread is not joinable.\n"); else if (ESRCH == status) - error_manager("No thread could be found corresponding to that \ - specified by the given thread ID.\n"); + error_manager("No thread could be found corresponding to that" + " specified by the given thread ID.\n"); else if (EDEADLK == status) - error_manager("A deadlock was detected or thye value of thread \ - specifies the calling thread.\n"); + error_manager("A deadlock was detected or thye value of thread" + " specifies the calling thread.\n"); } void safe_thread_handler(pthread_t *th_id, void *(*func_ptr)(void *), From 8a546578929eb553b3427d5a0b10db733cc39767 Mon Sep 17 00:00:00 2001 From: biralavor Date: Thu, 31 Oct 2024 18:40:17 -0300 Subject: [PATCH 10/16] fixing logic for low total meals --- philo/src/08.dinner_manager.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/philo/src/08.dinner_manager.c b/philo/src/08.dinner_manager.c index d56fa67..f21178f 100644 --- a/philo/src/08.dinner_manager.c +++ b/philo/src/08.dinner_manager.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/29 10:01:45 by umeneses #+# #+# */ -/* Updated: 2024/10/30 23:23:10 by umeneses ### ########.fr */ +/* Updated: 2024/10/31 16:47:44 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,11 +17,11 @@ void dinner_manager(t_table *table) int idx; idx = -1; - if (table->set.total_meals == -1) - { - // TODO - } - else if (table->set.total_meals > 0) + // if (table->set.total_meals == -1) + // { + // // TODO + // } + if (table->set.total_meals == 0) return ; // back to main and clean else if (table->set.total_philos == 1) { From 6f9f3ee8bf6d6e662b496e9a36f06dba237f4298 Mon Sep 17 00:00:00 2001 From: biralavor Date: Thu, 31 Oct 2024 18:41:32 -0300 Subject: [PATCH 11/16] moving alloc to table init --- philo/src/03.parsing.c | 16 +--------------- philo/src/07.table_philo_init.c | 9 ++++++++- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/philo/src/03.parsing.c b/philo/src/03.parsing.c index 5c6f738..06195ae 100644 --- a/philo/src/03.parsing.c +++ b/philo/src/03.parsing.c @@ -6,26 +6,12 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/22 16:16:16 by umeneses #+# #+# */ -/* Updated: 2024/10/30 23:28:53 by umeneses ### ########.fr */ +/* Updated: 2024/10/31 18:41:08 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" -t_table *table_alloc(t_table *table) -{ - // table = ft_safe_malloc(sizeof(t_table)); - // table->set = ft_safe_malloc(sizeof(t_set)); - table->philos = ft_safe_malloc(sizeof(t_philo) * table->set.total_philos); - table->chopsticks = ft_safe_malloc(sizeof(t_chops) * table->set.total_philos); - - // table->philos->th_id = ft_safe_malloc(sizeof(pthread_t)); - // table->philos->philo_mtx = ft_safe_malloc(sizeof(pthread_mutex_t)); - // table->table_mtx = ft_safe_malloc(sizeof(pthread_mutex_t)); - // table->printer_mtx = ft_safe_malloc(sizeof(pthread_mutex_t)); - return (table); -} - /** * @todo: free table if error_manager is called */ diff --git a/philo/src/07.table_philo_init.c b/philo/src/07.table_philo_init.c index c820d84..3a7deef 100644 --- a/philo/src/07.table_philo_init.c +++ b/philo/src/07.table_philo_init.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/22 16:17:52 by umeneses #+# #+# */ -/* Updated: 2024/10/30 23:28:45 by umeneses ### ########.fr */ +/* Updated: 2024/10/31 18:41:11 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,6 +47,13 @@ void philo_init_runner(t_table *table) } } +t_table *table_alloc(t_table *table) +{ + table->philos = ft_safe_malloc(sizeof(t_philo) * table->set.total_philos); + table->chopsticks = ft_safe_malloc(sizeof(t_chops) * table->set.total_philos); + return (table); +} + void table_init(t_table *table) { int idx; From 6a51ed65753fa2ff999182ead8a300c2924b0e54 Mon Sep 17 00:00:00 2001 From: biralavor Date: Thu, 31 Oct 2024 18:43:01 -0300 Subject: [PATCH 12/16] updating file name --- philo/Makefile | 6 +++--- philo/src/{error_manager.c => clear_and_error_manager.c} | 0 2 files changed, 3 insertions(+), 3 deletions(-) rename philo/src/{error_manager.c => clear_and_error_manager.c} (100%) diff --git a/philo/Makefile b/philo/Makefile index abeb1f3..2805cf2 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -6,7 +6,7 @@ # By: umeneses +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/17 15:36:56 by umeneses #+# #+# # -# Updated: 2024/10/30 16:25:01 by umeneses ### ########.fr # +# Updated: 2024/10/31 18:42:24 by umeneses ### ########.fr # # # # **************************************************************************** # @@ -50,7 +50,7 @@ SRC_FILES += 09.getters_setters.c SRC_FILES += 10.synchronizers.c SRC_FILES += 11.printer_with_mutex.c SRC_FILES += 12.dinner_routines.c -SRC_FILES += error_manager.c +SRC_FILES += clear_and_error_manager.c SRC_FILES += ft_atoi.c SRC_FILES += ft_gettime.c SRC_FILES += ft_is_space.c @@ -144,7 +144,7 @@ define bonus endef define args_to_test - 7 60 60 60 + 7 60 60 60 10 endef ifdef WITH_SANITIZE_THREAD diff --git a/philo/src/error_manager.c b/philo/src/clear_and_error_manager.c similarity index 100% rename from philo/src/error_manager.c rename to philo/src/clear_and_error_manager.c From fc4d88cd6ce32746274471d61b8081ddf183ba32 Mon Sep 17 00:00:00 2001 From: biralavor Date: Thu, 31 Oct 2024 18:46:13 -0300 Subject: [PATCH 13/16] building table_free() --- philo/headers/philo.h | 7 +++++-- philo/src/01.main.c | 3 ++- philo/src/clear_and_error_manager.c | 10 ++++++++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/philo/headers/philo.h b/philo/headers/philo.h index 55db088..c16c5ab 100644 --- a/philo/headers/philo.h +++ b/philo/headers/philo.h @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 16:04:11 by umeneses #+# #+# */ -/* Updated: 2024/10/30 22:56:36 by umeneses ### ########.fr */ +/* Updated: 2024/10/31 18:44:09 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -116,7 +116,6 @@ bool is_number_under_intmax(int ac, char **av); /* memory allocation functions */ t_table *table_alloc(t_table *table); void *ft_safe_malloc(size_t size); -void error_manager(const char *error_msg); t_table *table_holder(t_table *table, bool destroy); /* mutex handler functions */ @@ -154,6 +153,10 @@ void let_philo_eat_routine(t_philo *philo); void let_philo_think_routine(t_philo *philo); void let_philo_sleep_routine(t_philo *philo); +/* clear and error manager functions */ +void error_manager(const char *error_msg); +void table_free(t_table *table); + /* LIBFT utility functions */ int ft_strlen(const char *str); long ft_atoi_long_int(const char *string); diff --git a/philo/src/01.main.c b/philo/src/01.main.c index f5a083b..a007732 100644 --- a/philo/src/01.main.c +++ b/philo/src/01.main.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 16:03:55 by umeneses #+# #+# */ -/* Updated: 2024/10/30 22:55:34 by umeneses ### ########.fr */ +/* Updated: 2024/10/31 18:43:31 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,5 +40,6 @@ int main(int ac, char **av) table_init(&table); dinner_manager(&table); table_holder(NULL, true); + table_free(&table); exit(EXIT_SUCCESS); } diff --git a/philo/src/clear_and_error_manager.c b/philo/src/clear_and_error_manager.c index 4a5d0da..50e1366 100644 --- a/philo/src/clear_and_error_manager.c +++ b/philo/src/clear_and_error_manager.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* error_manager.c :+: :+: :+: */ +/* clear_and_error_manager.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/25 20:33:35 by umeneses #+# #+# */ -/* Updated: 2024/10/30 22:36:06 by umeneses ### ########.fr */ +/* Updated: 2024/10/31 18:44:18 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,3 +21,9 @@ void error_manager(const char *error_msg) // table_holder(NULL, true); exit(EXIT_FAILURE); } + +void table_free(t_table *table) +{ + free(table->philos); + free(table->chopsticks); +} From bb6c12064ca79eac69217e532d07227436628ed8 Mon Sep 17 00:00:00 2001 From: biralavor Date: Thu, 31 Oct 2024 18:48:44 -0300 Subject: [PATCH 14/16] clearing unused functions and variables --- philo/src/01.main.c | 19 +---------------- philo/src/04.table_holder.c | 9 +------- philo/src/ft_precise_usleep.c | 40 ++--------------------------------- 3 files changed, 4 insertions(+), 64 deletions(-) diff --git a/philo/src/01.main.c b/philo/src/01.main.c index a007732..178e47e 100644 --- a/philo/src/01.main.c +++ b/philo/src/01.main.c @@ -6,29 +6,12 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 16:03:55 by umeneses #+# #+# */ -/* Updated: 2024/10/31 18:43:31 by umeneses ### ########.fr */ +/* Updated: 2024/10/31 18:46:53 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" -void *dinner_routine(void *arg) -{ - int mails = 0; - int *result = malloc(sizeof(int)); // it will be freed after pthread_join() - int idx = 0; - printf("Thread %d is running\n", *(int *)arg); - while (idx < 2) - { - mails++; - mails = mails * (*(int *)arg); - idx++; - } - *result = mails; - free(arg); - return ((void *) result); -} - int main(int ac, char **av) { t_table table; diff --git a/philo/src/04.table_holder.c b/philo/src/04.table_holder.c index 45169e4..cc288f2 100644 --- a/philo/src/04.table_holder.c +++ b/philo/src/04.table_holder.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/28 15:40:26 by umeneses #+# #+# */ -/* Updated: 2024/10/30 22:47:03 by umeneses ### ########.fr */ +/* Updated: 2024/10/31 18:47:28 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,15 +20,8 @@ t_table *table_holder(t_table *table, bool destroy) table_holder = table; else if (table_holder && destroy) { - // pthread_mutex_destroy(table_holder->table_mtx); - // pthread_mutex_destroy(table_holder->printer_mtx); - // free(table_holder->set); - // free(table_holder->philos->th_id); free(table_holder->philos); - // free(table_holder->printer_mtx); - // free(table_holder->table_mtx); free(table_holder->chopsticks); - // free(table_holder); table_holder = NULL; } return (table_holder); diff --git a/philo/src/ft_precise_usleep.c b/philo/src/ft_precise_usleep.c index f738657..49ef0f9 100644 --- a/philo/src/ft_precise_usleep.c +++ b/philo/src/ft_precise_usleep.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* precise_usleep.c :+: :+: :+: */ +/* ft_precise_usleep.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/24 16:34:23 by umeneses #+# #+# */ -/* Updated: 2024/10/30 10:37:23 by umeneses ### ########.fr */ +/* Updated: 2024/10/31 18:48:26 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,21 +23,6 @@ long get_elapsed_time_microsec(struct timeval start, struct timeval end) void precise_usleep(long microsec, t_table *table) { - // struct timeval start; - // struct timeval current; - // long elapsed_time; - // long remaining_time; - - // gettimeofday(&start, NULL); - // elapsed_time = get_elapsed_time_microsec(start, current); - // while (elapsed_time < microsec) - // { - // gettimeofday(¤t, NULL); - // elapsed_time = get_elapsed_time_microsec(start, current); - // remaining_time = microsec - elapsed_time; - // if (remaining_time > 1000) - // usleep(remaining_time / 2); - // } long start; long elapsed_time; long remaining_time; @@ -60,24 +45,3 @@ void precise_usleep(long microsec, t_table *table) } } } - - -// int main(void) -// { -// struct timeval start; -// struct timeval end; -// long seconds; -// long microseconds; -// double elapsed; - -// gettimeofday(&start, NULL); -// precise_usleep(500000); -// gettimeofday(&end, NULL); -// seconds = end.tv_sec - start.tv_sec; -// microseconds = end.tv_usec - start.tv_usec; -// elapsed = seconds + microseconds * 1e-6; - -// printf("Expected sleep duration: "BOLD_RED"\t0.5"RESET" seconds\n"); -// printf("Actual sleep duration: "BOLD_RED"\t\t%f"RESET" seconds\n\n", elapsed); -// return (0); -// } From c1ed0d09c3c3a04acee32ff6d568a413335472c8 Mon Sep 17 00:00:00 2001 From: biralavor Date: Thu, 31 Oct 2024 18:58:01 -0300 Subject: [PATCH 15/16] increasing timeout for actions --- .github/workflows/valgrind_for_Philo.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/valgrind_for_Philo.yml b/.github/workflows/valgrind_for_Philo.yml index fbc6a43..75d9921 100644 --- a/.github/workflows/valgrind_for_Philo.yml +++ b/.github/workflows/valgrind_for_Philo.yml @@ -21,13 +21,13 @@ on: jobs: valgrind_test: runs-on: ubuntu-latest - timeout-minutes: 7 + timeout-minutes: 10 strategy: matrix: test_case: - { index: 1, total_philos: 7, time_to_die: 60, time_to_eat: 100, time_to_sleep: 250, total_meals: 5 } - { index: 2, total_philos: 14, time_to_die: 100, time_to_eat: 150, time_to_sleep: 300, total_meals: 10 } - - { index: 3, total_philos: 100, time_to_die: 200, time_to_eat: 200, time_to_sleep: 350, total_meals: 15 } + - { index: 3, total_philos: 80, time_to_die: 200, time_to_eat: 200, time_to_sleep: 350, total_meals: 15 } defaults: run: working-directory: ./philo/ From d2eb2f583b5d8354f849eb5018b31f6ce411522c Mon Sep 17 00:00:00 2001 From: biralavor Date: Thu, 31 Oct 2024 19:10:18 -0300 Subject: [PATCH 16/16] reducing philos number for actions --- .github/workflows/valgrind_for_Philo.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/valgrind_for_Philo.yml b/.github/workflows/valgrind_for_Philo.yml index 75d9921..73ccbe7 100644 --- a/.github/workflows/valgrind_for_Philo.yml +++ b/.github/workflows/valgrind_for_Philo.yml @@ -27,7 +27,7 @@ jobs: test_case: - { index: 1, total_philos: 7, time_to_die: 60, time_to_eat: 100, time_to_sleep: 250, total_meals: 5 } - { index: 2, total_philos: 14, time_to_die: 100, time_to_eat: 150, time_to_sleep: 300, total_meals: 10 } - - { index: 3, total_philos: 80, time_to_die: 200, time_to_eat: 200, time_to_sleep: 350, total_meals: 15 } + - { index: 3, total_philos: 50, time_to_die: 200, time_to_eat: 200, time_to_sleep: 350, total_meals: 15 } defaults: run: working-directory: ./philo/