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