nimp dans timestamp
This commit is contained in:
@@ -10,6 +10,8 @@ void *philo_exec(void *arg);
|
|||||||
// generic.c
|
// generic.c
|
||||||
void init_time(t_philo *philo);
|
void init_time(t_philo *philo);
|
||||||
void update_time(t_philo *philo);
|
void update_time(t_philo *philo);
|
||||||
|
int diff_time(t_time old, struct timeval stime);
|
||||||
|
int is_dead(t_philo *philo);
|
||||||
void print_message(t_philo *philo, char *msg);
|
void print_message(t_philo *philo, char *msg);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -3,6 +3,12 @@
|
|||||||
|
|
||||||
typedef pthread_mutex_t t_mtx;
|
typedef pthread_mutex_t t_mtx;
|
||||||
|
|
||||||
|
typedef struct s_time
|
||||||
|
{
|
||||||
|
long int ts;
|
||||||
|
long int tu;
|
||||||
|
} t_time;
|
||||||
|
|
||||||
typedef struct s_params
|
typedef struct s_params
|
||||||
{
|
{
|
||||||
int n_phi; // number_of_philosophers
|
int n_phi; // number_of_philosophers
|
||||||
@@ -15,8 +21,7 @@ typedef struct s_params
|
|||||||
typedef struct s_global
|
typedef struct s_global
|
||||||
{
|
{
|
||||||
int dead;
|
int dead;
|
||||||
long int t_start_s;
|
t_time t_start;
|
||||||
long int t_start_u;
|
|
||||||
t_mtx m_print;
|
t_mtx m_print;
|
||||||
t_mtx m_dead;
|
t_mtx m_dead;
|
||||||
} t_global;
|
} t_global;
|
||||||
@@ -27,8 +32,8 @@ typedef struct s_philo
|
|||||||
t_global *global;
|
t_global *global;
|
||||||
int p_nbr;
|
int p_nbr;
|
||||||
t_mtx m_fork;
|
t_mtx m_fork;
|
||||||
long int t_last_meal_s;
|
t_time t_last_meal;
|
||||||
long int t_last_meal_u;
|
int eat;
|
||||||
struct s_philo *next;
|
struct s_philo *next;
|
||||||
} t_philo;
|
} t_philo;
|
||||||
|
|
||||||
|
|||||||
90
srcs/exec.c
90
srcs/exec.c
@@ -1,46 +1,88 @@
|
|||||||
#include "philo.h"
|
#include "philo.h"
|
||||||
|
|
||||||
// TODO : compare time with time before dying
|
void go_sleep(t_philo *philo, int action_time)
|
||||||
void go_sleep(t_philo *philo, int time)
|
|
||||||
{
|
{
|
||||||
long int start_time;
|
struct timeval stime;
|
||||||
long int death_time;
|
int time_to_death;
|
||||||
long int next_step_time;
|
|
||||||
|
|
||||||
start_time = philo->t_last_meal_u;
|
// now_s
|
||||||
if (start_time == 0)
|
// now_u
|
||||||
start_time = philo->global->t_start_u;
|
// last_meal_s
|
||||||
|
// last_meal_u
|
||||||
|
//
|
||||||
|
// last_meal now
|
||||||
|
// v v
|
||||||
|
// ------|-----------|------------------------------------------
|
||||||
|
// | |
|
||||||
|
// |_d_e_a_t_h__t_i_m_e__________________|
|
||||||
|
// |
|
||||||
|
// |_e_a_t__t_i_m_e_________________|->action_time
|
||||||
|
// |
|
||||||
|
// |_d_i_f_f___|
|
||||||
|
// |
|
||||||
|
// |_d_e_a_t_h_-_d_i_f_f_____|->time_to_death
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// last_meal now
|
||||||
|
// v v
|
||||||
|
// ------|-----------|------------------------------------------
|
||||||
|
// |
|
||||||
|
// |_d_e_a_t_h__t_i_m_e_______________________________|
|
||||||
|
// |
|
||||||
|
// |_e_a_t__t_i_m_e_________________|->action_time
|
||||||
|
// |
|
||||||
|
// |_d_i_f_f___|
|
||||||
|
// |
|
||||||
|
// |_d_e_a_t_h_-_d_i_f_f__________________|->time_to_death
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// last_meal now
|
||||||
|
// v v
|
||||||
|
// ------|-----------|------------------------------------------
|
||||||
|
// |
|
||||||
|
// |_death___|
|
||||||
|
// |
|
||||||
|
// |_e_a_t__t_i_m_e_________________|->action_time
|
||||||
|
// |
|
||||||
|
// |_d_i_f_f___|
|
||||||
|
// |
|
||||||
|
// |_|->time_to_death
|
||||||
|
//
|
||||||
|
//
|
||||||
|
|
||||||
death_time = start_time + (philo->params->t_die * 1000);
|
gettimeofday(&stime, NULL);
|
||||||
next_step_time = start_time + (time * 1000);
|
time_to_death = diff_time(philo->t_last_meal, stime);
|
||||||
if (death_time < next_step_time)
|
time_to_death = philo->params->t_die - time_to_death;
|
||||||
|
if (time_to_death > action_time)
|
||||||
|
usleep(action_time * 1000);
|
||||||
|
else if (time_to_death > 0)
|
||||||
usleep(philo->params->t_die * 1000);
|
usleep(philo->params->t_die * 1000);
|
||||||
else
|
|
||||||
usleep(time * 1000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int ret_unlock(t_mtx *mutex_1, t_mtx *mutex_2, int ret)
|
int ret_unlock(t_philo *philo, int nbr_fork, int ret)
|
||||||
{
|
{
|
||||||
if (mutex_1)
|
pthread_mutex_unlock(&philo->m_fork);
|
||||||
pthread_mutex_unlock(mutex_1);
|
if (nbr_fork == 2)
|
||||||
if (mutex_2)
|
pthread_mutex_unlock(&philo->next->m_fork);
|
||||||
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->global->dead != 0)
|
if (is_dead(philo))
|
||||||
return (ret_unlock(&(philo->m_fork), NULL, 1));
|
return (ret_unlock(philo, 1, 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->global->dead != 0)
|
if (is_dead(philo))
|
||||||
return (ret_unlock(&(philo->m_fork), &(philo->next->m_fork), 1));
|
return (ret_unlock(philo, 2, 1));
|
||||||
print_message(philo, "has taken a fork");
|
print_message(philo, "has taken a fork");
|
||||||
|
|
||||||
update_time(philo);
|
update_time(philo);
|
||||||
print_message(philo, "is eating");
|
print_message(philo, "is eating");
|
||||||
|
philo->eat++;
|
||||||
|
if (philo->params->n_eat && philo->eat >= philo->params->n_eat)
|
||||||
|
return (ret_unlock(philo, 2, 1));
|
||||||
go_sleep(philo, philo->params->t_eat);
|
go_sleep(philo, philo->params->t_eat);
|
||||||
|
|
||||||
pthread_mutex_unlock(&(philo->next->m_fork));
|
pthread_mutex_unlock(&(philo->next->m_fork));
|
||||||
@@ -60,8 +102,12 @@ void *philo_exec(void *arg)
|
|||||||
{
|
{
|
||||||
if (take_forks(philo) != 0)
|
if (take_forks(philo) != 0)
|
||||||
break ;
|
break ;
|
||||||
|
if (is_dead(philo))
|
||||||
|
break ;
|
||||||
print_message(philo, "is sleeping");
|
print_message(philo, "is sleeping");
|
||||||
go_sleep(philo, philo->params->t_slp);
|
go_sleep(philo, philo->params->t_slp);
|
||||||
|
if (is_dead(philo))
|
||||||
|
break ;
|
||||||
print_message(philo, "is thinking");
|
print_message(philo, "is thinking");
|
||||||
}
|
}
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
|||||||
@@ -4,11 +4,11 @@ void init_time(t_philo *philo)
|
|||||||
{
|
{
|
||||||
struct timeval stime;
|
struct timeval stime;
|
||||||
|
|
||||||
if (philo->global->t_start_s == 0)
|
if (philo->global->t_start.ts == 0)
|
||||||
{
|
{
|
||||||
gettimeofday(&stime, NULL);
|
gettimeofday(&stime, NULL);
|
||||||
philo->global->t_start_s = stime.tv_sec;
|
philo->global->t_start.ts = stime.tv_sec;
|
||||||
philo->global->t_start_u = stime.tv_usec;
|
philo->global->t_start.tu = stime.tv_usec;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,8 +17,31 @@ void update_time(t_philo *philo)
|
|||||||
struct timeval stime;
|
struct timeval stime;
|
||||||
|
|
||||||
gettimeofday(&stime, NULL);
|
gettimeofday(&stime, NULL);
|
||||||
philo->t_last_meal_s = stime.tv_sec;
|
philo->t_last_meal.ts = stime.tv_sec;
|
||||||
philo->t_last_meal_u = stime.tv_usec;
|
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 is_dead(t_philo *philo)
|
||||||
|
{
|
||||||
|
struct timeval stime;
|
||||||
|
int time;
|
||||||
|
|
||||||
|
if (philo->global->dead)
|
||||||
|
return (1);
|
||||||
|
gettimeofday(&stime, NULL);
|
||||||
|
time = diff_time(philo->t_last_meal, stime);
|
||||||
|
if (time >= philo->params->t_die)
|
||||||
|
{
|
||||||
|
philo->global->dead = 1;
|
||||||
|
print_message(philo, "died");
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_message(t_philo *philo, char *msg)
|
void print_message(t_philo *philo, char *msg)
|
||||||
@@ -28,8 +51,8 @@ void print_message(t_philo *philo, char *msg)
|
|||||||
char *color;
|
char *color;
|
||||||
|
|
||||||
gettimeofday(&stime, NULL);
|
gettimeofday(&stime, NULL);
|
||||||
time_stamp = (stime.tv_sec - philo->global->t_start_s) * 1000;
|
time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000;
|
||||||
time_stamp += (stime.tv_usec - philo->global->t_start_u) / 1000;
|
time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 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;
|
||||||
|
|||||||
10
srcs/init.c
10
srcs/init.c
@@ -12,13 +12,13 @@ t_philo *lst_add_philo(t_params *params, t_global *global, int i)
|
|||||||
new->p_nbr = i + 1;
|
new->p_nbr = i + 1;
|
||||||
if (pthread_mutex_init(&(new->m_fork), NULL) != 0)
|
if (pthread_mutex_init(&(new->m_fork), NULL) != 0)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
new->t_last_meal_s = 0;
|
new->t_last_meal.ts = 0;
|
||||||
new->t_last_meal_u = 0;
|
new->t_last_meal.tu = 0;
|
||||||
|
new->eat = 0;
|
||||||
new->next = NULL;
|
new->next = NULL;
|
||||||
return (new);
|
return (new);
|
||||||
}
|
}
|
||||||
|
|
||||||
// looping chained list
|
|
||||||
t_philo *init_chain_philo(t_params *params, t_global *global)
|
t_philo *init_chain_philo(t_params *params, t_global *global)
|
||||||
{
|
{
|
||||||
t_philo *philo;
|
t_philo *philo;
|
||||||
@@ -71,8 +71,8 @@ t_global *init_global(void)
|
|||||||
if (!global)
|
if (!global)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
global->dead = 0;
|
global->dead = 0;
|
||||||
global->t_start_s = 0;
|
global->t_start.ts = 0;
|
||||||
global->t_start_u = 0;
|
global->t_start.tu = 0;
|
||||||
if (pthread_mutex_init(&(global->m_print), NULL) != 0)
|
if (pthread_mutex_init(&(global->m_print), NULL) != 0)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
if (pthread_mutex_init(&(global->m_dead), NULL) != 0)
|
if (pthread_mutex_init(&(global->m_dead), NULL) != 0)
|
||||||
|
|||||||
@@ -13,15 +13,9 @@ void launch_threads(t_philo *philo, pthread_t *id)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int is_dead(t_philo *philo)
|
|
||||||
{
|
|
||||||
(void)philo;
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void watch_threads(t_philo *philo, pthread_t *id)
|
void watch_threads(t_philo *philo, pthread_t *id)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (1)
|
while (1)
|
||||||
@@ -46,6 +40,5 @@ int main(int ac, char **av)
|
|||||||
return (0);
|
return (0);
|
||||||
launch_threads(philo, id);
|
launch_threads(philo, id);
|
||||||
watch_threads(philo, id);
|
watch_threads(philo, id);
|
||||||
write(1, "main function\n", 14);
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user