reorganisation structs et timestamp global

This commit is contained in:
Hugo LAMY
2022-01-22 12:41:30 +01:00
parent 6a4aad9503
commit 3ba76ccc4e
5 changed files with 34 additions and 28 deletions

View File

@@ -10,6 +10,9 @@ typedef struct s_params
int t_eat; // time_to_eat int t_eat; // time_to_eat
int t_slp; // time_to_sleep int t_slp; // time_to_sleep
int n_eat; // [number_of_times_each_philosopher_must_eat] int n_eat; // [number_of_times_each_philosopher_must_eat]
int dead;
long int t_start_s;
long int t_start_u;
} t_params; } t_params;
typedef struct s_philo typedef struct s_philo
@@ -18,11 +21,8 @@ typedef struct s_philo
int p_nbr; int p_nbr;
t_mtx m_fork; t_mtx m_fork;
t_mtx *m_print; t_mtx *m_print;
long int t_start_s;
long int t_start_u;
long int t_last_meal_s; long int t_last_meal_s;
long int t_last_meal_u; long int t_last_meal_u;
int *death;
struct s_philo *next; struct s_philo *next;
} t_philo; } t_philo;

View File

@@ -7,21 +7,24 @@ void go_sleep(t_philo *philo, int time)
usleep(time * 1000); usleep(time * 1000);
} }
int ret_unlock(t_mtx mutex, int ret) int ret_unlock(t_mtx *mutex_1, t_mtx *mutex_2, int ret)
{ {
pthread_mutex_unlock(&mutex); if (mutex_1)
pthread_mutex_unlock(mutex_1);
if (mutex_2)
pthread_mutex_unlock(mutex_2);
return (ret); return (ret);
} }
int take_forks(t_philo *philo) int take_forks(t_philo *philo)
{ {
pthread_mutex_lock(&(philo->m_fork)); pthread_mutex_lock(&(philo->m_fork));
if (philo->death) if (philo->params->dead != 0)
return (ret_unlock(philo->m_fork, 1)); return (ret_unlock(&(philo->m_fork), NULL, 1));
print_message(philo, "has taken a fork"); print_message(philo, "has taken a fork");
pthread_mutex_lock(&(philo->next->m_fork)); pthread_mutex_lock(&(philo->next->m_fork));
if (philo->death) if (philo->params->dead != 0)
return (ret_unlock(philo->next->m_fork, 1)); return (ret_unlock(&(philo->m_fork), &(philo->next->m_fork), 1));
print_message(philo, "has taken a fork"); print_message(philo, "has taken a fork");
print_message(philo, "is eating"); print_message(philo, "is eating");

View File

@@ -1,6 +1,6 @@
#include "philo.h" #include "philo.h"
t_philo *lst_add_philo(t_params *params, t_mtx *m_print, int *death, int i) t_philo *lst_add_philo(t_params *params, t_mtx *m_print, int i)
{ {
t_philo *new; t_philo *new;
@@ -12,13 +12,12 @@ t_philo *lst_add_philo(t_params *params, t_mtx *m_print, int *death, int i)
if (pthread_mutex_init(&(new->m_fork), NULL) != 0) if (pthread_mutex_init(&(new->m_fork), NULL) != 0)
return (NULL); return (NULL);
new->m_print = m_print; new->m_print = m_print;
new->death = death;
new->next = NULL; new->next = NULL;
return (new); return (new);
} }
// looping chained list // looping chained list
t_philo *init_chain_philo(t_params *params, t_mtx *m_print, int *death) t_philo *init_chain_philo(t_params *params, t_mtx *m_print)
{ {
t_philo *philo; t_philo *philo;
t_philo *tmp; t_philo *tmp;
@@ -29,7 +28,7 @@ t_philo *init_chain_philo(t_params *params, t_mtx *m_print, int *death)
philo = NULL; philo = NULL;
while (i < params->n_phi) while (i < params->n_phi)
{ {
tmp = lst_add_philo(params, m_print, death, i); tmp = lst_add_philo(params, m_print, i);
if (philo) if (philo)
philo->next = tmp; philo->next = tmp;
else else
@@ -54,6 +53,9 @@ t_params *init_params(int ac, char **av)
params->t_slp = ft_atoi(av[4]); params->t_slp = ft_atoi(av[4]);
if (ac == 6) if (ac == 6)
params->n_eat = ft_atoi(av[5]); params->n_eat = ft_atoi(av[5]);
params->dead = 0;
params->t_start_s = 0;
params->t_start_u = 0;
} }
else else
return (NULL); return (NULL);
@@ -65,7 +67,6 @@ t_philo *init(int ac, char **av, pthread_t **id)
t_philo *philo; t_philo *philo;
t_params *params; t_params *params;
t_mtx *m_print; t_mtx *m_print;
int death;
params = init_params(ac, av); params = init_params(ac, av);
if (params == NULL) if (params == NULL)
@@ -76,8 +77,7 @@ t_philo *init(int ac, char **av, pthread_t **id)
m_print = malloc(sizeof(t_mtx)); m_print = malloc(sizeof(t_mtx));
if (pthread_mutex_init(m_print, NULL) != 0) if (pthread_mutex_init(m_print, NULL) != 0)
return (NULL); return (NULL);
death = 0; philo = init_chain_philo(params, m_print);
philo = init_chain_philo(params, m_print, &death);
if (philo == NULL) if (philo == NULL)
return (NULL); return (NULL);
return (philo); return (philo);

View File

@@ -7,14 +7,14 @@ void print_message(t_philo *philo, char *msg)
char *color; char *color;
gettimeofday(&stime, NULL); gettimeofday(&stime, NULL);
time_stamp = (stime.tv_sec - philo->t_start_s) * 1000; time_stamp = (stime.tv_sec - philo->params->t_start_s) * 1000;
time_stamp += (stime.tv_usec - philo->t_start_u) / 1000; time_stamp += (stime.tv_usec - philo->params->t_start_u) / 1000;
color = WHITE; color = WHITE;
if (ft_strnstr(msg, "eating", ft_strlen(msg))) if (ft_strnstr(msg, "eating", ft_strlen(msg)))
color = B_YELLOW; color = B_YELLOW;
if (ft_strnstr(msg, "sleeping", ft_strlen(msg))) else if (ft_strnstr(msg, "sleeping", ft_strlen(msg)))
color = B_BLUE; color = B_BLUE;
if (ft_strnstr(msg, "thinking", ft_strlen(msg))) else if (ft_strnstr(msg, "thinking", ft_strlen(msg)))
color = B_GREEN; color = B_GREEN;
pthread_mutex_lock(philo->m_print); pthread_mutex_lock(philo->m_print);
ft_printf("%s%i %i %s%s\n", color, time_stamp, philo->p_nbr, msg, RESET); ft_printf("%s%i %i %s%s\n", color, time_stamp, philo->p_nbr, msg, RESET);

View File

@@ -4,9 +4,12 @@ void init_time(t_philo *philo)
{ {
struct timeval stime; struct timeval stime;
if (philo->params->t_start_s == 0)
{
gettimeofday(&stime, NULL); gettimeofday(&stime, NULL);
philo->t_start_s = stime.tv_sec; philo->params->t_start_s = stime.tv_sec;
philo->t_start_u = stime.tv_usec; philo->params->t_start_u = stime.tv_usec;
}
} }
void update_time(t_philo *philo) void update_time(t_philo *philo)