diff --git a/:w b/:w new file mode 100644 index 0000000..b9e380b --- /dev/null +++ b/:w @@ -0,0 +1,114 @@ +#include "philo.h" + +/* + t_params *params; + t_params *global; + int p_nbr; + t_mtx m_fork; + long int t_last_meal_s; + long int t_last_meal_u; + */ +t_philo *lst_add_philo(t_params *params, t_mtx *m_print, t_mtx *m_dead, int i) +{ + t_philo *new; + + new = malloc(sizeof(t_philo) * 1); + if (new == NULL) + return (NULL); + new->params = params; + new->global = global; + new->p_nbr = i + 1; + if (pthread_mutex_init(&(new->m_fork), NULL) != 0) + return (NULL); + new->next = NULL; + return (new); +} + +// looping chained list +t_philo *init_chain_philo(t_params *params, t_global *global) +{ + t_philo *philo; + t_philo *tmp; + t_philo *start; + int i; + + i = 0; + philo = NULL; + while (i < params->n_phi) + { + tmp = lst_add_philo(params, global, i); + if (philo) + philo->next = tmp; + else + start = tmp; + philo = tmp; + i++; + } + philo->next = start; + return (philo); +} + +t_params *init_params(int ac, char **av) +{ + t_params *params; + + if (ac == 5 || ac == 6) + { + params = malloc(sizeof(t_params)); + if (!params) + return (NULL); + params->n_phi = ft_atoi(av[1]); + params->t_die = ft_atoi(av[2]); + params->t_eat = ft_atoi(av[3]); + params->t_slp = ft_atoi(av[4]); + if (ac == 6) + params->n_eat = ft_atoi(av[5]); + } + else + return (NULL); + return (params); +} + +t_global *init_global(t_mtx *m_print, t_mtx *m_dead) +{ + t_global *global; + + global = malloc(sizeof(t_global)); + if (!global) + return (NULL); + global->dead = 0; + global->t_start_s = 0; + global->t_start_u = 0; + global->m_print = m_print; + global->m_dead = m_dead; + return (global); +} + +t_philo *init(int ac, char **av, pthread_t **id) +{ + t_philo *philo; + t_params *params; + t_global *global; + t_mtx *m_print; + t_mtx *m_dead; + + m_print = malloc(sizeof(t_mtx)); + if (pthread_mutex_init(m_print, NULL) != 0) + return (NULL); + m_dead = malloc(sizeof(t_mtx)); + if (pthread_mutex_init(m_dead, NULL) != 0) + return (NULL); + params = init_params(ac, av); + if (params == NULL) + return (NULL); + global = init_global(m_print, m_dead); + if (params == NULL) + return (NULL); + *id = malloc(sizeof(pthread_t) * params->n_phi); + if (*id == NULL) + return (NULL); + philo = init_chain_philo(params, global); + if (philo == NULL) + return (NULL); + return (philo); +} diff --git a/headers/philo_struct.h b/headers/philo_struct.h index 20ce219..afa245c 100644 --- a/headers/philo_struct.h +++ b/headers/philo_struct.h @@ -10,17 +10,23 @@ typedef struct s_params int t_eat; // time_to_eat int t_slp; // time_to_sleep int n_eat; // [number_of_times_each_philosopher_must_eat] +} t_params; + +typedef struct s_global +{ int dead; long int t_start_s; long int t_start_u; -} t_params; + t_mtx m_print; + t_mtx m_dead; +} t_global; typedef struct s_philo { t_params *params; + t_global *global; int p_nbr; t_mtx m_fork; - t_mtx *m_print; long int t_last_meal_s; long int t_last_meal_u; struct s_philo *next; diff --git a/srcs/exec.c b/srcs/exec.c index 59c5e02..4aa6d48 100644 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -19,14 +19,15 @@ int ret_unlock(t_mtx *mutex_1, t_mtx *mutex_2, int ret) int take_forks(t_philo *philo) { pthread_mutex_lock(&(philo->m_fork)); - if (philo->params->dead != 0) + if (philo->global->dead != 0) return (ret_unlock(&(philo->m_fork), NULL, 1)); print_message(philo, "has taken a fork"); pthread_mutex_lock(&(philo->next->m_fork)); - if (philo->params->dead != 0) + if (philo->global->dead != 0) return (ret_unlock(&(philo->m_fork), &(philo->next->m_fork), 1)); print_message(philo, "has taken a fork"); + update_time(philo); print_message(philo, "is eating"); go_sleep(philo, philo->params->t_eat); @@ -35,11 +36,6 @@ int take_forks(t_philo *philo) return (0); } -// int n_phi; // number_of_philosophers -// int t_die; // time_to_die -// int t_eat; // time_to_eat -// int t_slp; // time_to_sleep -// int n_eat; // [number_of_times_each_philosopher_must_eat] void *philo_exec(void *arg) { t_philo *philo; diff --git a/srcs/init.c b/srcs/init.c index c6e42af..af61d88 100644 --- a/srcs/init.c +++ b/srcs/init.c @@ -1,6 +1,6 @@ #include "philo.h" -t_philo *lst_add_philo(t_params *params, t_mtx *m_print, int i) +t_philo *lst_add_philo(t_params *params, t_global *global, int i) { t_philo *new; @@ -8,16 +8,18 @@ t_philo *lst_add_philo(t_params *params, t_mtx *m_print, int i) if (new == NULL) return (NULL); new->params = params; + new->global = global; new->p_nbr = i + 1; if (pthread_mutex_init(&(new->m_fork), NULL) != 0) return (NULL); - new->m_print = m_print; + new->t_last_meal_s = 0; + new->t_last_meal_u = 0; new->next = NULL; return (new); } // looping chained list -t_philo *init_chain_philo(t_params *params, t_mtx *m_print) +t_philo *init_chain_philo(t_params *params, t_global *global) { t_philo *philo; t_philo *tmp; @@ -28,7 +30,7 @@ t_philo *init_chain_philo(t_params *params, t_mtx *m_print) philo = NULL; while (i < params->n_phi) { - tmp = lst_add_philo(params, m_print, i); + tmp = lst_add_philo(params, global, i); if (philo) philo->next = tmp; else @@ -47,37 +49,53 @@ t_params *init_params(int ac, char **av) if (ac == 5 || ac == 6) { params = malloc(sizeof(t_params)); + if (!params) + return (NULL); params->n_phi = ft_atoi(av[1]); params->t_die = ft_atoi(av[2]); params->t_eat = ft_atoi(av[3]); params->t_slp = ft_atoi(av[4]); if (ac == 6) params->n_eat = ft_atoi(av[5]); - params->dead = 0; - params->t_start_s = 0; - params->t_start_u = 0; } else return (NULL); return (params); } +t_global *init_global(void) +{ + t_global *global; + + global = malloc(sizeof(t_global)); + if (!global) + return (NULL); + global->dead = 0; + global->t_start_s = 0; + global->t_start_u = 0; + if (pthread_mutex_init(&(global->m_print), NULL) != 0) + return (NULL); + if (pthread_mutex_init(&(global->m_dead), NULL) != 0) + return (NULL); + return (global); +} + t_philo *init(int ac, char **av, pthread_t **id) { t_philo *philo; t_params *params; - t_mtx *m_print; + t_global *global; params = init_params(ac, av); + if (params == NULL) + return (NULL); + global = init_global(); if (params == NULL) return (NULL); *id = malloc(sizeof(pthread_t) * params->n_phi); if (*id == NULL) return (NULL); - m_print = malloc(sizeof(t_mtx)); - if (pthread_mutex_init(m_print, NULL) != 0) - return (NULL); - philo = init_chain_philo(params, m_print); + philo = init_chain_philo(params, global); if (philo == NULL) return (NULL); return (philo); diff --git a/srcs/main.c b/srcs/main.c index 50b25d8..ebc2ff6 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -13,11 +13,22 @@ void launch_threads(t_philo *philo, pthread_t *id) } } +int is_dead(t_philo *philo) +{ + (void)philo; + return (0); +} + void watch_threads(t_philo *philo, pthread_t *id) { int i; i = 0; + while (1) + { + if (is_dead(philo) == 1) + break ; + } while (i < philo->params->n_phi) { pthread_join(id[i], NULL); diff --git a/srcs/message.c b/srcs/message.c index 1737da5..52e8b31 100644 --- a/srcs/message.c +++ b/srcs/message.c @@ -7,8 +7,8 @@ void print_message(t_philo *philo, char *msg) char *color; gettimeofday(&stime, NULL); - time_stamp = (stime.tv_sec - philo->params->t_start_s) * 1000; - time_stamp += (stime.tv_usec - philo->params->t_start_u) / 1000; + time_stamp = (stime.tv_sec - philo->global->t_start_s) * 1000; + time_stamp += (stime.tv_usec - philo->global->t_start_u) / 1000; color = WHITE; if (ft_strnstr(msg, "eating", ft_strlen(msg))) color = B_YELLOW; @@ -16,7 +16,7 @@ void print_message(t_philo *philo, char *msg) color = B_BLUE; else if (ft_strnstr(msg, "thinking", ft_strlen(msg))) color = B_GREEN; - pthread_mutex_lock(philo->m_print); + pthread_mutex_lock(&(philo->global->m_print)); ft_printf("%s%i %i %s%s\n", color, time_stamp, philo->p_nbr, msg, RESET); - pthread_mutex_unlock(philo->m_print); + pthread_mutex_unlock(&(philo->global->m_print)); } diff --git a/srcs/time.c b/srcs/time.c index 193ffac..92a42de 100644 --- a/srcs/time.c +++ b/srcs/time.c @@ -4,11 +4,11 @@ void init_time(t_philo *philo) { struct timeval stime; - if (philo->params->t_start_s == 0) + if (philo->global->t_start_s == 0) { gettimeofday(&stime, NULL); - philo->params->t_start_s = stime.tv_sec; - philo->params->t_start_u = stime.tv_usec; + philo->global->t_start_s = stime.tv_sec; + philo->global->t_start_u = stime.tv_usec; } }