debut implementation death tout est casse
This commit is contained in:
67
srcs/exec.c
67
srcs/exec.c
@@ -1,60 +1,35 @@
|
||||
#include "philo.h"
|
||||
|
||||
void print_message(t_philo *philo, char *msg)
|
||||
// TODO : compare time with time before dying
|
||||
void go_sleep(t_philo *philo, int time)
|
||||
{
|
||||
long int time_stamp;
|
||||
struct timeval stime;
|
||||
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;
|
||||
color = WHITE;
|
||||
if (ft_strnstr(msg, "eating", ft_strlen(msg)))
|
||||
color = B_YELLOW;
|
||||
if (ft_strnstr(msg, "sleeping", ft_strlen(msg)))
|
||||
color = B_BLUE;
|
||||
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);
|
||||
pthread_mutex_unlock(philo->m_print);
|
||||
(void)philo;
|
||||
usleep(time * 1000);
|
||||
}
|
||||
|
||||
// long int t_start_s;
|
||||
// long int t_start_u;
|
||||
// long int t_last_meal_s;
|
||||
// long int t_last_meal_u;
|
||||
void init_time(t_philo *philo)
|
||||
int ret_unlock(t_mtx mutex, int ret)
|
||||
{
|
||||
struct timeval stime;
|
||||
|
||||
gettimeofday(&stime, NULL);
|
||||
philo->t_start_s = stime.tv_sec;
|
||||
philo->t_start_u = stime.tv_usec;
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void take_forks(t_philo *philo)
|
||||
int take_forks(t_philo *philo)
|
||||
{
|
||||
pthread_mutex_lock(&(philo->m_fork));
|
||||
if (philo->death)
|
||||
return (ret_unlock(philo->m_fork, 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));
|
||||
print_message(philo, "has taken a fork");
|
||||
pthread_mutex_lock(&(philo->next->m_fork));
|
||||
print_message(philo, "has taken a fork");
|
||||
|
||||
print_message(philo, "is eating");
|
||||
usleep(philo->params->t_eat * 1000);
|
||||
print_message(philo, "is eating");
|
||||
go_sleep(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));
|
||||
return (0);
|
||||
}
|
||||
|
||||
// int n_phi; // number_of_philosophers
|
||||
@@ -72,10 +47,10 @@ void *philo_exec(void *arg)
|
||||
usleep(10 * 1000);
|
||||
while (1)
|
||||
{
|
||||
take_forks(philo);
|
||||
|
||||
if (take_forks(philo) != 0)
|
||||
break ;
|
||||
print_message(philo, "is sleeping");
|
||||
usleep(philo->params->t_slp * 1000);
|
||||
go_sleep(philo, philo->params->t_slp);
|
||||
print_message(philo, "is thinking");
|
||||
}
|
||||
return (NULL);
|
||||
|
||||
Reference in New Issue
Block a user