Skip to content

Commit

Permalink
Merge pull request #49 from biralavor/35-replacing-dinner-routine
Browse files Browse the repository at this point in the history
35 replacing dinner routine
  • Loading branch information
biralavor authored Oct 31, 2024
2 parents f6f3a67 + d2eb2f5 commit 131cf76
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 208 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/valgrind_for_Philo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: 50, time_to_die: 200, time_to_eat: 200, time_to_sleep: 350, total_meals: 15 }
defaults:
run:
working-directory: ./philo/
Expand Down
6 changes: 3 additions & 3 deletions philo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: umeneses <umeneses@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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 #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
30 changes: 18 additions & 12 deletions philo/headers/philo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: umeneses <umeneses@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 16:04:11 by umeneses #+# #+# */
/* Updated: 2024/10/30 16:40:57 by umeneses ### ########.fr */
/* Updated: 2024/10/31 18:44:09 by umeneses ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -62,7 +62,6 @@ typedef enum e_philo_status
DEAD,
} t_philo_status;

typedef struct s_table t_table;

typedef struct s_set
{
Expand All @@ -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;
Expand All @@ -97,11 +98,11 @@ struct s_table
long start_time;
bool this_is_the_end;
bool all_threads_ready_togo;
t_set *set;
t_philo *philo;
pthread_mutex_t *table_mtx;
pthread_mutex_t *printer_mtx;
t_chops *chopstick;
t_set set;
t_philo *philos;
pthread_mutex_t table_mtx;
pthread_mutex_t printer_mtx;
t_chops *chopsticks;
};

/* validation functions */
Expand All @@ -113,9 +114,8 @@ 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);

/* mutex handler functions */
Expand All @@ -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);
Expand All @@ -142,7 +143,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);

Expand All @@ -151,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);
Expand Down
31 changes: 7 additions & 24 deletions philo/src/01.main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,23 @@
/* By: umeneses <umeneses@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 16:03:55 by umeneses #+# #+# */
/* Updated: 2024/10/30 16:09:03 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;
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);
table_free(&table);
exit(EXIT_SUCCESS);
}
36 changes: 11 additions & 25 deletions philo/src/03.parsing.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,28 @@
/* By: umeneses <umeneses@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/22 16:16:16 by umeneses #+# #+# */
/* Updated: 2024/10/30 16:08:25 by umeneses ### ########.fr */
/* Updated: 2024/10/31 18:41:08 by umeneses ### ########.fr */
/* */
/* ************************************************************************** */

#include "philo.h"

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)
* 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));
return (table);
}

/**
* @todo: free table if error_manager is called
*/
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");
}
13 changes: 3 additions & 10 deletions philo/src/04.table_holder.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: umeneses <umeneses@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/28 15:40:26 by umeneses #+# #+# */
/* Updated: 2024/10/30 16:08:47 by umeneses ### ########.fr */
/* Updated: 2024/10/31 18:47:28 by umeneses ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -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->philo->th_id);
free(table_holder->philo);
free(table_holder->printer_mtx);
free(table_holder->table_mtx);
free(table_holder->chopstick);
free(table_holder);
free(table_holder->philos);
free(table_holder->chopsticks);
table_holder = NULL;
}
return (table_holder);
Expand Down
12 changes: 6 additions & 6 deletions philo/src/06.pthread_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: umeneses <umeneses@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/28 15:16:28 by umeneses #+# #+# */
/* Updated: 2024/10/28 16:51:30 by umeneses ### ########.fr */
/* Updated: 2024/10/30 23:36:07 by umeneses ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -25,14 +25,14 @@ 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 *), \
void safe_thread_handler(pthread_t *th_id, void *(*func_ptr)(void *),
void *data, t_mtx_opcode opcode)
{
if (CREATE == opcode)
Expand Down
Loading

0 comments on commit 131cf76

Please sign in to comment.