diff --git a/Makefile b/Makefile index 67e872a..737f05a 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ LIBS = -lpthread SRCS = main.c \ init.c \ - pere_fouettard.c \ + launch.c \ exec.c \ utils.c \ generic.c diff --git a/headers/philo_proto.h b/headers/philo_proto.h index dab46f0..20f068d 100644 --- a/headers/philo_proto.h +++ b/headers/philo_proto.h @@ -16,20 +16,19 @@ // init.c t_philo *init(int ac, char **av, pthread_t **id); -// pere_fouettard.c -int is_dead(t_philo *philo); -void launch_threads(t_philo *philo, pthread_t *id); +// launch.c +void launch(t_philo *philo, pthread_t *id); // exec.c void *philo_exec(void *arg); -// utils.c +// generic.c void init_time(t_philo *philo); void update_time(t_philo *philo); -int diff_time(t_time old, struct timeval new); +int diff_time(t_time *old, struct timeval *new); int print_message(t_philo *philo, char *clr, char *msg); -// generic.c +// utils.c int ft_atoi(const char *str); #endif diff --git a/headers/philo_struct.h b/headers/philo_struct.h index 92340f0..bb76206 100644 --- a/headers/philo_struct.h +++ b/headers/philo_struct.h @@ -32,11 +32,10 @@ typedef struct s_params typedef struct s_global { - int dead; - int n_eat; + int stop; + int satiated_count; t_time t_start; t_mtx m_print; - t_mtx m_dead; } t_global; typedef struct s_philo @@ -46,7 +45,7 @@ typedef struct s_philo int p_nbr; t_mtx m_fork; t_time t_last_meal; - int eat; + int eat_count; struct s_philo *next; } t_philo; diff --git a/srcs/exec.c b/srcs/exec.c index f803d95..2360bdb 100644 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -3,29 +3,29 @@ /* ::: :::::::: */ /* exec.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: hulamy +#+ +:+ +#+ */ +/* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/26 15:30:28 by hulamy #+# #+# */ -/* Updated: 2022/01/26 15:32:57 by hulamy ### ########.fr */ +/* Updated: 2022/01/26 16:29:49 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" -void action_delay(t_philo *philo, int action_time) +static void action_delay(t_philo *philo, int action_time) { struct timeval stime; - int time_to_death; + int death_time; gettimeofday(&stime, NULL); - time_to_death = philo->params->t_die - diff_time(philo->t_last_meal, stime); - if (time_to_death > action_time) + death_time = philo->params->t_die - diff_time(&philo->t_last_meal, &stime); + if (death_time > action_time) usleep(action_time * 1000); - else if (time_to_death > 0) + else if (death_time > 0) usleep(philo->params->t_die * 1000); } -int ret_err_unlock(t_philo *philo, int nbr_fork) +static int ret_err_unlock(t_philo *philo, int nbr_fork) { pthread_mutex_unlock(&philo->m_fork); if (nbr_fork == 2) @@ -33,7 +33,7 @@ int ret_err_unlock(t_philo *philo, int nbr_fork) return (1); } -int eat(t_philo *philo) +static int eat(t_philo *philo) { pthread_mutex_lock(&(philo->m_fork)); if (print_message(philo, WHITE, "has taken a fork")) @@ -44,9 +44,9 @@ int eat(t_philo *philo) update_time(philo); if (print_message(philo, B_YELLOW, "is eating")) return (ret_err_unlock(philo, 2)); - philo->eat++; - if (philo->eat == philo->params->n_eat) - philo->global->n_eat++; + philo->eat_count++; + if (philo->eat_count == philo->params->n_eat) + philo->global->satiated_count++; action_delay(philo, philo->params->t_eat); pthread_mutex_unlock(&(philo->next->m_fork)); pthread_mutex_unlock(&(philo->m_fork)); diff --git a/srcs/generic.c b/srcs/generic.c index 48ef485..7f2552a 100644 --- a/srcs/generic.c +++ b/srcs/generic.c @@ -3,39 +3,58 @@ /* ::: :::::::: */ /* generic.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: hulamy +#+ +:+ +#+ */ +/* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2022/01/26 15:30:19 by hulamy #+# #+# */ -/* Updated: 2022/01/26 15:31:15 by hulamy ### ########.fr */ +/* Created: 2022/01/26 15:30:54 by hulamy #+# #+# */ +/* Updated: 2022/01/26 16:29:10 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" -int ft_atoi(const char *str) +void init_time(t_philo *philo) { - int i; - int result; - int is_negative; + struct timeval stime; - is_negative = 0; - result = 0; - i = 0; - while (str[i] == ' ' || (str[i] >= 9 && str[i] <= 13)) - i++; - if (str[i] == '+') - i++; - else if (str[i] == '-') + if (philo->global->t_start.ts == 0) { - is_negative = 1; - i++; + gettimeofday(&stime, NULL); + philo->global->t_start.ts = stime.tv_sec; + philo->global->t_start.tu = stime.tv_usec; } - while (str[i] >= '0' && str[i] <= '9') - { - result = (result * 10) + (str[i] - '0'); - i++; - } - if (is_negative) - result = result * -1; - return (result); + philo->t_last_meal.ts = philo->global->t_start.ts; + philo->t_last_meal.tu = philo->global->t_start.tu; +} + +void update_time(t_philo *philo) +{ + struct timeval stime; + + gettimeofday(&stime, NULL); + philo->t_last_meal.ts = stime.tv_sec; + philo->t_last_meal.tu = stime.tv_usec; +} + +int diff_time(t_time *old, struct timeval *new) +{ + return ((new->tv_sec - old->ts) * 1000 + (new->tv_usec - old->tu) / 1000); +} + +int print_message(t_philo *philo, char *clr, char *msg) +{ + long int time_stamp; + struct timeval stime; + + pthread_mutex_lock(&(philo->global->m_print)); + if (philo->global->stop) + { + pthread_mutex_unlock(&(philo->global->m_print)); + return (1); + } + gettimeofday(&stime, NULL); + time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000; + time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000; + printf("%s%li %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET); + pthread_mutex_unlock(&(philo->global->m_print)); + return (0); } diff --git a/srcs/init.c b/srcs/init.c index d500df6..f1d3040 100644 --- a/srcs/init.c +++ b/srcs/init.c @@ -3,16 +3,16 @@ /* ::: :::::::: */ /* init.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: hulamy +#+ +:+ +#+ */ +/* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/26 15:30:34 by hulamy #+# #+# */ -/* Updated: 2022/01/26 15:30:35 by hulamy ### ########.fr */ +/* Updated: 2022/01/26 16:29:38 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" -t_philo *lst_add_philo(t_params *params, t_global *global, int i) +static t_philo *lst_add_philo(t_params *params, t_global *global, int i) { t_philo *new; @@ -26,12 +26,12 @@ t_philo *lst_add_philo(t_params *params, t_global *global, int i) return (NULL); new->t_last_meal.ts = 0; new->t_last_meal.tu = 0; - new->eat = 0; + new->eat_count = 0; new->next = NULL; return (new); } -t_philo *init_chain_philo(t_params *params, t_global *global) +static t_philo *init_chain_philo(t_params *params, t_global *global) { t_philo *philo; t_philo *tmp; @@ -54,7 +54,7 @@ t_philo *init_chain_philo(t_params *params, t_global *global) return (philo); } -t_params *init_params(int ac, char **av) +static t_params *init_params(int ac, char **av) { t_params *params; @@ -76,21 +76,19 @@ t_params *init_params(int ac, char **av) return (params); } -t_global *init_global(void) +static t_global *init_global(void) { t_global *global; global = malloc(sizeof(t_global)); if (!global) return (NULL); - global->dead = 0; - global->n_eat = 0; + global->stop = 0; + global->satiated_count = 0; global->t_start.ts = 0; global->t_start.tu = 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); } diff --git a/srcs/pere_fouettard.c b/srcs/launch.c similarity index 59% rename from srcs/pere_fouettard.c rename to srcs/launch.c index 47d0cf0..33e9dcb 100644 --- a/srcs/pere_fouettard.c +++ b/srcs/launch.c @@ -1,36 +1,34 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* pere_fouettard.c :+: :+: :+: */ +/* launch.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: hulamy +#+ +:+ +#+ */ +/* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/26 15:30:49 by hulamy #+# #+# */ -/* Updated: 2022/01/26 15:31:40 by hulamy ### ########.fr */ +/* Updated: 2022/01/26 16:29:13 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" -int is_dead(t_philo *philo) +static int is_finished(t_philo *philo, struct timeval *stime) { - struct timeval stime; long int time_stamp; int time; - if (philo->global->n_eat == philo->params->n_phi) + if (philo->global->satiated_count == philo->params->n_phi) { - philo->global->dead = 1; + philo->global->stop = 1; return (1); } - gettimeofday(&stime, NULL); - time = diff_time(philo->t_last_meal, stime); + time = diff_time(&philo->t_last_meal, stime); if (time >= philo->params->t_die) { pthread_mutex_lock(&(philo->global->m_print)); - philo->global->dead = 1; - time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000; - time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000; + philo->global->stop = 1; + time_stamp = (stime->tv_sec - philo->global->t_start.ts) * 1000; + time_stamp += (stime->tv_usec - philo->global->t_start.tu) / 1000; printf("%s%li %i died%s\n", B_RED, time_stamp, philo->p_nbr, RESET); pthread_mutex_unlock(&(philo->global->m_print)); return (1); @@ -38,7 +36,27 @@ int is_dead(t_philo *philo) return (0); } -void launch_threads(t_philo *philo, pthread_t *id) +static void pere_fouettard(t_philo *philo) +{ + struct timeval stime; + int i; + + while (philo->global->stop == 0) + { + i = 0; + gettimeofday(&stime, NULL); + while (i < philo->params->n_phi) + { + if (is_finished(philo, &stime)) + break ; + philo = philo->next; + i++; + } + usleep(1 * 1000); + } +} + +void launch(t_philo *philo, pthread_t *id) { int i; @@ -49,11 +67,7 @@ void launch_threads(t_philo *philo, pthread_t *id) philo = philo->next; i++; } - while (philo->global->dead == 0) - { - is_dead(philo); - philo = philo->next; - } + pere_fouettard(philo); i = 0; while (i < philo->params->n_phi) { diff --git a/srcs/main.c b/srcs/main.c index 4a223d1..1a5742d 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -21,7 +21,7 @@ int main(int ac, char **av) philo = init(ac, av, &id); if (philo == NULL) return (0); - launch_threads(philo, id); + launch(philo, id); i = 0; while (i < philo->params->n_phi) { diff --git a/srcs/utils.c b/srcs/utils.c index d89c284..48ef485 100644 --- a/srcs/utils.c +++ b/srcs/utils.c @@ -1,60 +1,41 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* utils.c :+: :+: :+: */ +/* generic.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2022/01/26 15:30:54 by hulamy #+# #+# */ -/* Updated: 2022/01/26 15:30:55 by hulamy ### ########.fr */ +/* Created: 2022/01/26 15:30:19 by hulamy #+# #+# */ +/* Updated: 2022/01/26 15:31:15 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" -void init_time(t_philo *philo) +int ft_atoi(const char *str) { - struct timeval stime; + int i; + int result; + int is_negative; - if (philo->global->t_start.ts == 0) + is_negative = 0; + result = 0; + i = 0; + while (str[i] == ' ' || (str[i] >= 9 && str[i] <= 13)) + i++; + if (str[i] == '+') + i++; + else if (str[i] == '-') { - gettimeofday(&stime, NULL); - philo->global->t_start.ts = stime.tv_sec; - philo->global->t_start.tu = stime.tv_usec; + is_negative = 1; + i++; } - philo->t_last_meal.ts = philo->global->t_start.ts; - philo->t_last_meal.tu = philo->global->t_start.tu; -} - -void update_time(t_philo *philo) -{ - struct timeval stime; - - gettimeofday(&stime, NULL); - philo->t_last_meal.ts = stime.tv_sec; - philo->t_last_meal.tu = stime.tv_usec; -} - -int diff_time(t_time old, struct timeval new) -{ - return ((new.tv_sec - old.ts) * 1000 + (new.tv_usec - old.tu) / 1000); -} - -int print_message(t_philo *philo, char *clr, char *msg) -{ - long int time_stamp; - struct timeval stime; - - pthread_mutex_lock(&(philo->global->m_print)); - if (philo->global->dead) + while (str[i] >= '0' && str[i] <= '9') { - pthread_mutex_unlock(&(philo->global->m_print)); - return (1); + result = (result * 10) + (str[i] - '0'); + i++; } - gettimeofday(&stime, NULL); - time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000; - time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000; - printf("%s%li %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET); - pthread_mutex_unlock(&(philo->global->m_print)); - return (0); + if (is_negative) + result = result * -1; + return (result); }