From b998de9340aaa8d93081837188f8d1118f63c672 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sun, 23 Jan 2022 18:09:46 +0100 Subject: [PATCH] ajout fonction sleep uqi compare action vs death --- :w | 114 ---------------------------------------------------- srcs/exec.c | 16 +++++++- 2 files changed, 14 insertions(+), 116 deletions(-) delete mode 100644 :w diff --git a/:w b/:w deleted file mode 100644 index b9e380b..0000000 --- a/:w +++ /dev/null @@ -1,114 +0,0 @@ -#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/srcs/exec.c b/srcs/exec.c index 4aa6d48..d4bdfee 100644 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -3,8 +3,20 @@ // TODO : compare time with time before dying void go_sleep(t_philo *philo, int time) { - (void)philo; - usleep(time * 1000); + long int start_time; + long int death_time; + long int next_step_time; + + start_time = philo->t_last_meal_u; + if (start_time == 0) + start_time = philo->global->t_start_u; + + death_time = start_time + (philo->params->t_die * 1000); + next_step_time = start_time + (time * 1000); + if (death_time < next_step_time) + usleep(philo->params->t_die * 1000); + else + usleep(time * 1000); } int ret_unlock(t_mtx *mutex_1, t_mtx *mutex_2, int ret)