nimp dans timestamp
This commit is contained in:
90
srcs/exec.c
90
srcs/exec.c
@@ -1,46 +1,88 @@
|
||||
#include "philo.h"
|
||||
|
||||
// TODO : compare time with time before dying
|
||||
void go_sleep(t_philo *philo, int time)
|
||||
void go_sleep(t_philo *philo, int action_time)
|
||||
{
|
||||
long int start_time;
|
||||
long int death_time;
|
||||
long int next_step_time;
|
||||
struct timeval stime;
|
||||
int time_to_death;
|
||||
|
||||
start_time = philo->t_last_meal_u;
|
||||
if (start_time == 0)
|
||||
start_time = philo->global->t_start_u;
|
||||
// now_s
|
||||
// now_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);
|
||||
next_step_time = start_time + (time * 1000);
|
||||
if (death_time < next_step_time)
|
||||
gettimeofday(&stime, NULL);
|
||||
time_to_death = diff_time(philo->t_last_meal, stime);
|
||||
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);
|
||||
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(mutex_1);
|
||||
if (mutex_2)
|
||||
pthread_mutex_unlock(mutex_2);
|
||||
pthread_mutex_unlock(&philo->m_fork);
|
||||
if (nbr_fork == 2)
|
||||
pthread_mutex_unlock(&philo->next->m_fork);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
int take_forks(t_philo *philo)
|
||||
{
|
||||
pthread_mutex_lock(&(philo->m_fork));
|
||||
if (philo->global->dead != 0)
|
||||
return (ret_unlock(&(philo->m_fork), NULL, 1));
|
||||
if (is_dead(philo))
|
||||
return (ret_unlock(philo, 1, 1));
|
||||
print_message(philo, "has taken a fork");
|
||||
pthread_mutex_lock(&(philo->next->m_fork));
|
||||
if (philo->global->dead != 0)
|
||||
return (ret_unlock(&(philo->m_fork), &(philo->next->m_fork), 1));
|
||||
if (is_dead(philo))
|
||||
return (ret_unlock(philo, 2, 1));
|
||||
print_message(philo, "has taken a fork");
|
||||
|
||||
update_time(philo);
|
||||
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);
|
||||
|
||||
pthread_mutex_unlock(&(philo->next->m_fork));
|
||||
@@ -60,8 +102,12 @@ void *philo_exec(void *arg)
|
||||
{
|
||||
if (take_forks(philo) != 0)
|
||||
break ;
|
||||
if (is_dead(philo))
|
||||
break ;
|
||||
print_message(philo, "is sleeping");
|
||||
go_sleep(philo, philo->params->t_slp);
|
||||
if (is_dead(philo))
|
||||
break ;
|
||||
print_message(philo, "is thinking");
|
||||
}
|
||||
return (NULL);
|
||||
|
||||
@@ -4,11 +4,11 @@ void init_time(t_philo *philo)
|
||||
{
|
||||
struct timeval stime;
|
||||
|
||||
if (philo->global->t_start_s == 0)
|
||||
if (philo->global->t_start.ts == 0)
|
||||
{
|
||||
gettimeofday(&stime, NULL);
|
||||
philo->global->t_start_s = stime.tv_sec;
|
||||
philo->global->t_start_u = stime.tv_usec;
|
||||
philo->global->t_start.ts = stime.tv_sec;
|
||||
philo->global->t_start.tu = stime.tv_usec;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,8 +17,31 @@ 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;
|
||||
philo->t_last_meal.ts = stime.tv_sec;
|
||||
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)
|
||||
@@ -28,8 +51,8 @@ void print_message(t_philo *philo, char *msg)
|
||||
char *color;
|
||||
|
||||
gettimeofday(&stime, NULL);
|
||||
time_stamp = (stime.tv_sec - philo->global->t_start_s) * 1000;
|
||||
time_stamp += (stime.tv_usec - philo->global->t_start_u) / 1000;
|
||||
time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000;
|
||||
time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000;
|
||||
color = WHITE;
|
||||
if (ft_strnstr(msg, "eating", ft_strlen(msg)))
|
||||
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;
|
||||
if (pthread_mutex_init(&(new->m_fork), NULL) != 0)
|
||||
return (NULL);
|
||||
new->t_last_meal_s = 0;
|
||||
new->t_last_meal_u = 0;
|
||||
new->t_last_meal.ts = 0;
|
||||
new->t_last_meal.tu = 0;
|
||||
new->eat = 0;
|
||||
new->next = NULL;
|
||||
return (new);
|
||||
}
|
||||
|
||||
// looping chained list
|
||||
t_philo *init_chain_philo(t_params *params, t_global *global)
|
||||
{
|
||||
t_philo *philo;
|
||||
@@ -71,8 +71,8 @@ t_global *init_global(void)
|
||||
if (!global)
|
||||
return (NULL);
|
||||
global->dead = 0;
|
||||
global->t_start_s = 0;
|
||||
global->t_start_u = 0;
|
||||
global->t_start.ts = 0;
|
||||
global->t_start.tu = 0;
|
||||
if (pthread_mutex_init(&(global->m_print), NULL) != 0)
|
||||
return (NULL);
|
||||
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)
|
||||
{
|
||||
int i;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (1)
|
||||
@@ -46,6 +40,5 @@ int main(int ac, char **av)
|
||||
return (0);
|
||||
launch_threads(philo, id);
|
||||
watch_threads(philo, id);
|
||||
write(1, "main function\n", 14);
|
||||
return (0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user