From 6a4aad95037f76ffbd9c45dc85b907b9e42c4c3e Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Fri, 21 Jan 2022 18:32:37 +0100 Subject: [PATCH] debut implementation death tout est casse --- Makefile | 4 ++- headers/philo_proto.h | 7 +++++ headers/philo_struct.h | 2 +- srcs/exec.c | 67 +++++++++++++--------------------------- srcs/init.c | 29 +++++++++-------- srcs/main.c | 32 ++++++++++--------- srcs/main0.c | 23 -------------- srcs/main1_join.c | 25 --------------- srcs/main2_multithread.c | 43 -------------------------- srcs/main3_mutex.c | 48 ---------------------------- srcs/message.c | 22 +++++++++++++ srcs/time.c | 19 ++++++++++++ 12 files changed, 107 insertions(+), 214 deletions(-) delete mode 100644 srcs/main0.c delete mode 100644 srcs/main1_join.c delete mode 100644 srcs/main2_multithread.c delete mode 100644 srcs/main3_mutex.c create mode 100644 srcs/message.c create mode 100644 srcs/time.c diff --git a/Makefile b/Makefile index 10dd4c3..c4f0d23 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,9 @@ LIBFT = $(LIBFT_D)/libft.a SRCS = main.c \ init.c \ - exec.c + exec.c \ + time.c \ + message.c DIR_OBJS = builds OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o) diff --git a/headers/philo_proto.h b/headers/philo_proto.h index ca44930..dfef26e 100644 --- a/headers/philo_proto.h +++ b/headers/philo_proto.h @@ -7,5 +7,12 @@ t_philo *init(int ac, char **av, pthread_t **id); // exec.c void *philo_exec(void *arg); +// time.c +void init_time(t_philo *philo); +void update_time(t_philo *philo); + +// message.c +void print_message(t_philo *philo, char *msg); + #endif diff --git a/headers/philo_struct.h b/headers/philo_struct.h index 71b1421..6228e24 100644 --- a/headers/philo_struct.h +++ b/headers/philo_struct.h @@ -22,7 +22,7 @@ typedef struct s_philo long int t_start_u; long int t_last_meal_s; long int t_last_meal_u; - struct s_philo *prev; + int *death; struct s_philo *next; } t_philo; diff --git a/srcs/exec.c b/srcs/exec.c index 8b5da1c..0a10715 100644 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -1,60 +1,35 @@ #include "philo.h" -void print_message(t_philo *philo, char *msg) +// TODO : compare time with time before dying +void go_sleep(t_philo *philo, int time) { - long int time_stamp; - struct timeval stime; - char *color; - - gettimeofday(&stime, NULL); - time_stamp = (stime.tv_sec - philo->t_start_s) * 1000; - time_stamp += (stime.tv_usec - philo->t_start_u) / 1000; - color = WHITE; - if (ft_strnstr(msg, "eating", ft_strlen(msg))) - color = B_YELLOW; - if (ft_strnstr(msg, "sleeping", ft_strlen(msg))) - color = B_BLUE; - if (ft_strnstr(msg, "thinking", ft_strlen(msg))) - color = B_GREEN; - pthread_mutex_lock(philo->m_print); - ft_printf("%s%i %i %s%s\n", color, time_stamp, philo->p_nbr, msg, RESET); - pthread_mutex_unlock(philo->m_print); + (void)philo; + usleep(time * 1000); } -// long int t_start_s; -// long int t_start_u; -// long int t_last_meal_s; -// long int t_last_meal_u; -void init_time(t_philo *philo) +int ret_unlock(t_mtx mutex, int ret) { - struct timeval stime; - - gettimeofday(&stime, NULL); - philo->t_start_s = stime.tv_sec; - philo->t_start_u = stime.tv_usec; + pthread_mutex_unlock(&mutex); + return (ret); } -void update_time(t_philo *philo) -{ - struct timeval stime; - - gettimeofday(&stime, NULL); - philo->t_last_meal_s = stime.tv_sec; - philo->t_last_meal_u = stime.tv_usec; -} - -void take_forks(t_philo *philo) +int take_forks(t_philo *philo) { pthread_mutex_lock(&(philo->m_fork)); + if (philo->death) + return (ret_unlock(philo->m_fork, 1)); + print_message(philo, "has taken a fork"); + pthread_mutex_lock(&(philo->next->m_fork)); + if (philo->death) + return (ret_unlock(philo->next->m_fork, 1)); print_message(philo, "has taken a fork"); - pthread_mutex_lock(&(philo->next->m_fork)); - print_message(philo, "has taken a fork"); - print_message(philo, "is eating"); - usleep(philo->params->t_eat * 1000); + print_message(philo, "is eating"); + go_sleep(philo, philo->params->t_eat); - pthread_mutex_unlock(&(philo->next->m_fork)); + pthread_mutex_unlock(&(philo->next->m_fork)); pthread_mutex_unlock(&(philo->m_fork)); + return (0); } // int n_phi; // number_of_philosophers @@ -72,10 +47,10 @@ void *philo_exec(void *arg) usleep(10 * 1000); while (1) { - take_forks(philo); - + if (take_forks(philo) != 0) + break ; print_message(philo, "is sleeping"); - usleep(philo->params->t_slp * 1000); + go_sleep(philo, philo->params->t_slp); print_message(philo, "is thinking"); } return (NULL); diff --git a/srcs/init.c b/srcs/init.c index 48dee44..35d053f 100644 --- a/srcs/init.c +++ b/srcs/init.c @@ -1,6 +1,6 @@ #include "philo.h" -t_philo *lst_add_philo(t_philo *philo, t_params *params, t_mtx *m_print, int i) +t_philo *lst_add_philo(t_params *params, t_mtx *m_print, int *death, int i) { t_philo *new; @@ -12,31 +12,32 @@ t_philo *lst_add_philo(t_philo *philo, t_params *params, t_mtx *m_print, int i) if (pthread_mutex_init(&(new->m_fork), NULL) != 0) return (NULL); new->m_print = m_print; - if (philo) - philo->next = new; - new->prev = philo; + new->death = death; + 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_mtx *m_print, int *death) { - t_philo *end; t_philo *philo; + t_philo *tmp; + t_philo *start; int i; i = 0; philo = NULL; while (i < params->n_phi) { - philo = lst_add_philo(philo, params, m_print, i); + tmp = lst_add_philo(params, m_print, death, i); + if (philo) + philo->next = tmp; + else + start = tmp; + philo = tmp; i++; } - end = philo; - while (philo->prev != NULL) - philo = philo->prev; - philo->prev = end; - end->next = philo; + philo->next = start; return (philo); } @@ -64,6 +65,7 @@ t_philo *init(int ac, char **av, pthread_t **id) t_philo *philo; t_params *params; t_mtx *m_print; + int death; params = init_params(ac, av); if (params == NULL) @@ -74,7 +76,8 @@ t_philo *init(int ac, char **av, pthread_t **id) m_print = malloc(sizeof(t_mtx)); if (pthread_mutex_init(m_print, NULL) != 0) return (NULL); - philo = init_chain_philo(params, m_print); + death = 0; + philo = init_chain_philo(params, m_print, &death); if (philo == NULL) return (NULL); return (philo); diff --git a/srcs/main.c b/srcs/main.c index a0ae8df..50b25d8 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -1,14 +1,26 @@ #include "philo.h" -void launch_threads(t_philo *philo, pthread_t *id, int n) +void launch_threads(t_philo *philo, pthread_t *id) +{ + int i; + + i = 0; + while (i < philo->params->n_phi) + { + pthread_create(&id[i], NULL, &philo_exec, philo); + philo = philo->next; + i++; + } +} + +void watch_threads(t_philo *philo, pthread_t *id) { int i; i = 0; - while (i < n) + while (i < philo->params->n_phi) { - pthread_create(&id[i], NULL, &philo_exec, philo); - philo = philo->next; + pthread_join(id[i], NULL); i++; } } @@ -17,20 +29,12 @@ int main(int ac, char **av) { pthread_t *id; t_philo *philo; - int i; - int n; philo = init(ac, av, &id); if (philo == NULL) return (0); - n = philo->params->n_phi; - launch_threads(philo, id, n); - i = 0; - while (i < n) - { - pthread_join(id[i], NULL); - i++; - } + launch_threads(philo, id); + watch_threads(philo, id); write(1, "main function\n", 14); return (0); } diff --git a/srcs/main0.c b/srcs/main0.c deleted file mode 100644 index c9064db..0000000 --- a/srcs/main0.c +++ /dev/null @@ -1,23 +0,0 @@ -#include "philo.h" - -void *philo_exec(void *arg) -{ - t_philo *philo; - - philo = (t_philo*)arg; - while (1) - write(1, philo->str, ft_strlen(philo->str)); -} - -int main(void) -{ - pthread_t id; - t_philo *philo; - - philo = malloc(sizeof(philo)); - philo->str = "i'm philosopher\n"; - pthread_create(&id, NULL, &philo_exec, philo); - while (1) - write(1, "main function\n", 14); - return 0; -} diff --git a/srcs/main1_join.c b/srcs/main1_join.c deleted file mode 100644 index c8cdaf7..0000000 --- a/srcs/main1_join.c +++ /dev/null @@ -1,25 +0,0 @@ -#include "philo.h" - -void *philo_exec(void *arg) -{ - t_philo *philo; - - philo = (t_philo*)arg; - while (1) - write(1, philo->str, ft_strlen(philo->str)); -} - -int main(void) -{ - pthread_t id; - t_philo *philo; - - philo = malloc(sizeof(philo)); - philo->str = "i'm philosopher\n"; - pthread_create(&id, NULL, &philo_exec, philo); - pthread_join(id, NULL); - while (1) - write(1, "main function\n", 14); - return 0; -} - diff --git a/srcs/main2_multithread.c b/srcs/main2_multithread.c deleted file mode 100644 index 17e556c..0000000 --- a/srcs/main2_multithread.c +++ /dev/null @@ -1,43 +0,0 @@ -#include "philo.h" - -void *philo_exec(void *arg) -{ - t_philo *philo; - - philo = (t_philo*)arg; - while (1) - { - write(1, philo->str, ft_strlen(philo->str)); - write(1, " nb ", 4); - ft_putnbr_fd(philo->nbr, 1); - write(1, "\n", 1); - sleep (1); - } -} - -int main(void) -{ - pthread_t *tid; - int n_philo; - int i; - t_philo *philo; - - n_philo = 3; - philo = malloc(sizeof(philo) * n_philo); - tid = malloc(sizeof(pthread_t) * n_philo); - i = 0; - while (i < n_philo) - { - philo[i].str = "i'm philosopher"; - philo[i].nbr = i; - pthread_create(&tid[i], NULL, &philo_exec, &philo[i]); - i++; - } - while (1) - { - write(1, "main function\n", 14); - sleep (1); - } - return 0; -} - diff --git a/srcs/main3_mutex.c b/srcs/main3_mutex.c deleted file mode 100644 index 810a6c6..0000000 --- a/srcs/main3_mutex.c +++ /dev/null @@ -1,48 +0,0 @@ -#include "philo.h" - -pthread_mutex_t mutex; - -void *philo_exec(void *arg) -{ - t_philo *philo; - - pthread_mutex_lock(&mutex); - philo = (t_philo*)arg; - while (1) - { - write(1, philo->str, ft_strlen(philo->str)); - write(1, " nb ", 4); - ft_putnbr_fd(philo->nbr, 1); - write(1, "\n", 1); - sleep (1); - } - pthread_mutex_unlock(&mutex); -} - -int main(void) -{ - pthread_t *tid; - int n_philo; - int i; - t_philo *philo; - - n_philo = 3; - philo = malloc(sizeof(philo) * n_philo); - tid = malloc(sizeof(pthread_t) * n_philo); - i = 0; - pthread_mutex_init(&mutex, NULL); - while (i < n_philo) - { - philo[i].str = "i'm philosopher"; - philo[i].nbr = i; - pthread_create(&tid[i], NULL, &philo_exec, &philo[i]); - i++; - } - while (1) - { - write(1, "main function\n", 14); - sleep (1); - } - return 0; -} - diff --git a/srcs/message.c b/srcs/message.c new file mode 100644 index 0000000..e84f283 --- /dev/null +++ b/srcs/message.c @@ -0,0 +1,22 @@ +#include "philo.h" + +void print_message(t_philo *philo, char *msg) +{ + long int time_stamp; + struct timeval stime; + char *color; + + gettimeofday(&stime, NULL); + time_stamp = (stime.tv_sec - philo->t_start_s) * 1000; + time_stamp += (stime.tv_usec - philo->t_start_u) / 1000; + color = WHITE; + if (ft_strnstr(msg, "eating", ft_strlen(msg))) + color = B_YELLOW; + if (ft_strnstr(msg, "sleeping", ft_strlen(msg))) + color = B_BLUE; + if (ft_strnstr(msg, "thinking", ft_strlen(msg))) + color = B_GREEN; + pthread_mutex_lock(philo->m_print); + ft_printf("%s%i %i %s%s\n", color, time_stamp, philo->p_nbr, msg, RESET); + pthread_mutex_unlock(philo->m_print); +} diff --git a/srcs/time.c b/srcs/time.c new file mode 100644 index 0000000..8f83526 --- /dev/null +++ b/srcs/time.c @@ -0,0 +1,19 @@ +#include "philo.h" + +void init_time(t_philo *philo) +{ + struct timeval stime; + + gettimeofday(&stime, NULL); + philo->t_start_s = stime.tv_sec; + philo->t_start_u = stime.tv_usec; +} + +void update_time(t_philo *philo) +{ + struct timeval stime; + + gettimeofday(&stime, NULL); + philo->t_last_meal_s = stime.tv_sec; + philo->t_last_meal_u = stime.tv_usec; +}