reorganisation structs et timestamp global
This commit is contained in:
@@ -5,11 +5,14 @@ typedef pthread_mutex_t t_mtx;
|
||||
|
||||
typedef struct s_params
|
||||
{
|
||||
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]
|
||||
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]
|
||||
int dead;
|
||||
long int t_start_s;
|
||||
long int t_start_u;
|
||||
} t_params;
|
||||
|
||||
typedef struct s_philo
|
||||
@@ -18,11 +21,8 @@ typedef struct s_philo
|
||||
int p_nbr;
|
||||
t_mtx m_fork;
|
||||
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_u;
|
||||
int *death;
|
||||
struct s_philo *next;
|
||||
} t_philo;
|
||||
|
||||
|
||||
15
srcs/exec.c
15
srcs/exec.c
@@ -7,21 +7,24 @@ void go_sleep(t_philo *philo, int time)
|
||||
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);
|
||||
}
|
||||
|
||||
int take_forks(t_philo *philo)
|
||||
{
|
||||
pthread_mutex_lock(&(philo->m_fork));
|
||||
if (philo->death)
|
||||
return (ret_unlock(philo->m_fork, 1));
|
||||
if (philo->params->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->death)
|
||||
return (ret_unlock(philo->next->m_fork, 1));
|
||||
if (philo->params->dead != 0)
|
||||
return (ret_unlock(&(philo->m_fork), &(philo->next->m_fork), 1));
|
||||
print_message(philo, "has taken a fork");
|
||||
|
||||
print_message(philo, "is eating");
|
||||
|
||||
14
srcs/init.c
14
srcs/init.c
@@ -1,6 +1,6 @@
|
||||
#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;
|
||||
|
||||
@@ -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)
|
||||
return (NULL);
|
||||
new->m_print = m_print;
|
||||
new->death = death;
|
||||
new->next = NULL;
|
||||
return (new);
|
||||
}
|
||||
|
||||
// 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 *tmp;
|
||||
@@ -29,7 +28,7 @@ t_philo *init_chain_philo(t_params *params, t_mtx *m_print, int *death)
|
||||
philo = NULL;
|
||||
while (i < params->n_phi)
|
||||
{
|
||||
tmp = lst_add_philo(params, m_print, death, i);
|
||||
tmp = lst_add_philo(params, m_print, i);
|
||||
if (philo)
|
||||
philo->next = tmp;
|
||||
else
|
||||
@@ -54,6 +53,9 @@ t_params *init_params(int ac, char **av)
|
||||
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);
|
||||
@@ -65,7 +67,6 @@ 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)
|
||||
@@ -76,8 +77,7 @@ 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);
|
||||
death = 0;
|
||||
philo = init_chain_philo(params, m_print, &death);
|
||||
philo = init_chain_philo(params, m_print);
|
||||
if (philo == NULL)
|
||||
return (NULL);
|
||||
return (philo);
|
||||
|
||||
@@ -7,14 +7,14 @@ void print_message(t_philo *philo, char *msg)
|
||||
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;
|
||||
time_stamp = (stime.tv_sec - philo->params->t_start_s) * 1000;
|
||||
time_stamp += (stime.tv_usec - philo->params->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)))
|
||||
else if (ft_strnstr(msg, "sleeping", ft_strlen(msg)))
|
||||
color = B_BLUE;
|
||||
if (ft_strnstr(msg, "thinking", ft_strlen(msg)))
|
||||
else 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);
|
||||
|
||||
@@ -4,9 +4,12 @@ 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;
|
||||
if (philo->params->t_start_s == 0)
|
||||
{
|
||||
gettimeofday(&stime, NULL);
|
||||
philo->params->t_start_s = stime.tv_sec;
|
||||
philo->params->t_start_u = stime.tv_usec;
|
||||
}
|
||||
}
|
||||
|
||||
void update_time(t_philo *philo)
|
||||
|
||||
Reference in New Issue
Block a user