1 philo ok, deplacement diff_time dans generic, renomme go_sleep, ferme lock en fin
This commit is contained in:
11
srcs/exec.c
11
srcs/exec.c
@@ -1,13 +1,12 @@
|
||||
#include "philo.h"
|
||||
|
||||
void go_sleep(t_philo *philo, int action_time)
|
||||
void action_delay(t_philo *philo, int action_time)
|
||||
{
|
||||
struct timeval stime;
|
||||
int time_to_death;
|
||||
|
||||
gettimeofday(&stime, NULL);
|
||||
time_to_death = diff_time(philo->t_last_meal, stime);
|
||||
time_to_death = philo->params->t_die - time_to_death;
|
||||
time_to_death = philo->params->t_die - diff_time(philo->t_last_meal, stime);
|
||||
if (time_to_death > action_time)
|
||||
usleep(action_time * 1000);
|
||||
else if (time_to_death > 0)
|
||||
@@ -30,15 +29,13 @@ int eat(t_philo *philo)
|
||||
pthread_mutex_lock(&(philo->next->m_fork));
|
||||
if (print_message(philo, WHITE, "has taken a fork"))
|
||||
return (ret_err_unlock(philo, 2));
|
||||
|
||||
update_time(philo);
|
||||
if (print_message(philo, B_YELLOW, "is eating"))
|
||||
return (ret_err_unlock(philo, 2));
|
||||
philo->eat++;
|
||||
if (philo->eat == philo->params->n_eat)
|
||||
philo->global->n_eat++;
|
||||
go_sleep(philo, philo->params->t_eat);
|
||||
|
||||
action_delay(philo, philo->params->t_eat);
|
||||
pthread_mutex_unlock(&(philo->next->m_fork));
|
||||
pthread_mutex_unlock(&(philo->m_fork));
|
||||
return (0);
|
||||
@@ -58,7 +55,7 @@ void *philo_exec(void *arg)
|
||||
break ;
|
||||
if (print_message(philo, B_BLUE, "is sleeping"))
|
||||
break ;
|
||||
go_sleep(philo, philo->params->t_slp);
|
||||
action_delay(philo, philo->params->t_slp);
|
||||
if (print_message(philo, B_GREEN, "is thinking"))
|
||||
break ;
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ void init_time(t_philo *philo)
|
||||
philo->global->t_start.ts = stime.tv_sec;
|
||||
philo->global->t_start.tu = stime.tv_usec;
|
||||
}
|
||||
philo->t_last_meal.ts = philo->global->t_start.ts;
|
||||
philo->t_last_meal.tu = philo->global->t_start.tu;
|
||||
}
|
||||
|
||||
void update_time(t_philo *philo)
|
||||
@@ -21,6 +23,11 @@ void update_time(t_philo *philo)
|
||||
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 print_message(t_philo *philo, char *clr, char *msg)
|
||||
{
|
||||
long int time_stamp;
|
||||
|
||||
@@ -16,5 +16,12 @@ int main(int ac, char **av)
|
||||
pthread_join(id[i], NULL);
|
||||
i++;
|
||||
}
|
||||
i = 0;
|
||||
while (i < philo->params->n_phi)
|
||||
{
|
||||
pthread_create(&id[i], NULL, &philo_exec, philo);
|
||||
philo = philo->next;
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
#include "philo.h"
|
||||
|
||||
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;
|
||||
@@ -51,7 +46,7 @@ void launch_threads(t_philo *philo, pthread_t *id)
|
||||
i = 0;
|
||||
while (i < philo->params->n_phi)
|
||||
{
|
||||
pthread_create(&id[i], NULL, &philo_exec, philo);
|
||||
pthread_mutex_unlock(&(philo->m_fork));
|
||||
philo = philo->next;
|
||||
i++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user