check died dans un autre thread et reorganisation fichiers avec main thread
This commit is contained in:
1
Makefile
1
Makefile
@@ -23,6 +23,7 @@ LIBFT = $(LIBFT_D)/libft.a
|
|||||||
|
|
||||||
SRCS = main.c \
|
SRCS = main.c \
|
||||||
init.c \
|
init.c \
|
||||||
|
main_thread.c \
|
||||||
exec.c \
|
exec.c \
|
||||||
generic.c
|
generic.c
|
||||||
|
|
||||||
|
|||||||
@@ -4,15 +4,18 @@
|
|||||||
// init.c
|
// init.c
|
||||||
t_philo *init(int ac, char **av, pthread_t **id);
|
t_philo *init(int ac, char **av, pthread_t **id);
|
||||||
|
|
||||||
|
// main_thread.c
|
||||||
|
int diff_time(t_time old, struct timeval new);
|
||||||
|
int is_dead(t_philo *philo);
|
||||||
|
void launch_threads(t_philo *philo, pthread_t *id);
|
||||||
|
|
||||||
// exec.c
|
// exec.c
|
||||||
void *philo_exec(void *arg);
|
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 print_message(t_philo *philo, char *clr, char *msg, int key);
|
||||||
int is_dead(t_philo *philo, struct timeval stime);
|
|
||||||
int print_message(t_philo *philo, char *clr, char *msg);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ typedef struct s_params
|
|||||||
typedef struct s_global
|
typedef struct s_global
|
||||||
{
|
{
|
||||||
int dead;
|
int dead;
|
||||||
|
int n_eat;
|
||||||
t_time t_start;
|
t_time t_start;
|
||||||
t_mtx m_print;
|
t_mtx m_print;
|
||||||
t_mtx m_dead;
|
t_mtx m_dead;
|
||||||
|
|||||||
14
srcs/exec.c
14
srcs/exec.c
@@ -25,16 +25,18 @@ int ret_err_unlock(t_philo *philo, int nbr_fork)
|
|||||||
int eat(t_philo *philo)
|
int eat(t_philo *philo)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&(philo->m_fork));
|
pthread_mutex_lock(&(philo->m_fork));
|
||||||
if (print_message(philo, WHITE, "has taken a fork") == 1)
|
if (print_message(philo, WHITE, "has taken a fork", philo->global->dead))
|
||||||
return (ret_err_unlock(philo, 1));
|
return (ret_err_unlock(philo, 1));
|
||||||
pthread_mutex_lock(&(philo->next->m_fork));
|
pthread_mutex_lock(&(philo->next->m_fork));
|
||||||
if (print_message(philo, WHITE, "has taken a fork") == 1)
|
if (print_message(philo, WHITE, "has taken a fork", philo->global->dead))
|
||||||
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") == 1)
|
if (print_message(philo, B_YELLOW, "is eating", philo->global->dead))
|
||||||
return (ret_err_unlock(philo, 2));
|
return (ret_err_unlock(philo, 2));
|
||||||
philo->eat++;
|
philo->eat++;
|
||||||
|
if (philo->eat == philo->params->n_eat)
|
||||||
|
philo->global->n_eat++;
|
||||||
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));
|
||||||
@@ -49,15 +51,15 @@ void *philo_exec(void *arg)
|
|||||||
philo = (t_philo*)arg;
|
philo = (t_philo*)arg;
|
||||||
init_time(philo);
|
init_time(philo);
|
||||||
if (philo->p_nbr % 2 == 0)
|
if (philo->p_nbr % 2 == 0)
|
||||||
usleep(10 * 1000);
|
usleep(10);
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
if (eat(philo) != 0)
|
if (eat(philo) != 0)
|
||||||
break ;
|
break ;
|
||||||
if (print_message(philo, B_BLUE, "is sleeping") == 1)
|
if (print_message(philo, B_BLUE, "is sleeping", philo->global->dead))
|
||||||
break ;
|
break ;
|
||||||
go_sleep(philo, philo->params->t_slp);
|
go_sleep(philo, philo->params->t_slp);
|
||||||
if (print_message(philo, B_GREEN, "is thinking") == 1)
|
if (print_message(philo, B_GREEN, "is thinking", philo->global->dead))
|
||||||
break ;
|
break ;
|
||||||
}
|
}
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
|||||||
@@ -21,51 +21,20 @@ 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)
|
int print_message(t_philo *philo, char *clr, char *msg, int key)
|
||||||
{
|
|
||||||
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);
|
|
||||||
if (philo->params->n_eat != -1 && philo->eat >= philo->params->n_eat)
|
|
||||||
{
|
|
||||||
philo->global->dead = 1;
|
|
||||||
return (philo->p_nbr);
|
|
||||||
}
|
|
||||||
time = diff_time(philo->t_last_meal, stime);
|
|
||||||
if (time >= philo->params->t_die)
|
|
||||||
{
|
|
||||||
philo->global->dead = 1;
|
|
||||||
return (philo->p_nbr);
|
|
||||||
}
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int print_message(t_philo *philo, char *clr, char *msg)
|
|
||||||
{
|
{
|
||||||
long int time_stamp;
|
long int time_stamp;
|
||||||
struct timeval stime;
|
struct timeval stime;
|
||||||
char p_dead;
|
|
||||||
|
|
||||||
|
if (key)
|
||||||
|
return (1);
|
||||||
pthread_mutex_lock(&(philo->global->m_print));
|
pthread_mutex_lock(&(philo->global->m_print));
|
||||||
gettimeofday(&stime, NULL);
|
gettimeofday(&stime, NULL);
|
||||||
p_dead = is_dead(philo, stime);
|
|
||||||
time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000;
|
time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000;
|
||||||
time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000;
|
time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000;
|
||||||
if (p_dead != 0)
|
ft_printf("%s%i %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET);
|
||||||
{
|
|
||||||
if (p_dead > 0)
|
|
||||||
ft_printf("%s%i %i died%s\n", B_RED, time_stamp, p_dead, RESET);
|
|
||||||
pthread_mutex_unlock(&(philo->global->m_print));
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
ft_printf("%s%i %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET);
|
|
||||||
pthread_mutex_unlock(&(philo->global->m_print));
|
pthread_mutex_unlock(&(philo->global->m_print));
|
||||||
|
if (philo->global->dead)
|
||||||
|
return (1);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,6 +72,7 @@ t_global *init_global(void)
|
|||||||
if (!global)
|
if (!global)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
global->dead = 0;
|
global->dead = 0;
|
||||||
|
global->n_eat = 0;
|
||||||
global->t_start.ts = 0;
|
global->t_start.ts = 0;
|
||||||
global->t_start.tu = 0;
|
global->t_start.tu = 0;
|
||||||
if (pthread_mutex_init(&(global->m_print), NULL) != 0)
|
if (pthread_mutex_init(&(global->m_print), NULL) != 0)
|
||||||
|
|||||||
13
srcs/main.c
13
srcs/main.c
@@ -1,18 +1,5 @@
|
|||||||
#include "philo.h"
|
#include "philo.h"
|
||||||
|
|
||||||
void launch_threads(t_philo *philo, pthread_t *id)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while (i < philo->params->n_phi)
|
|
||||||
{
|
|
||||||
pthread_create(&id[i], NULL, &philo_exec, philo);
|
|
||||||
philo = philo->next;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int ac, char **av)
|
int main(int ac, char **av)
|
||||||
{
|
{
|
||||||
pthread_t *id;
|
pthread_t *id;
|
||||||
|
|||||||
52
srcs/main_thread.c
Normal file
52
srcs/main_thread.c
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#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;
|
||||||
|
int time;
|
||||||
|
|
||||||
|
if (philo->global->n_eat == philo->params->n_phi)
|
||||||
|
{
|
||||||
|
philo->global->dead = 1;
|
||||||
|
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, B_RED, "died", 0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void launch_threads(t_philo *philo, pthread_t *id)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i < philo->params->n_phi)
|
||||||
|
{
|
||||||
|
pthread_create(&id[i], NULL, &philo_exec, philo);
|
||||||
|
philo = philo->next;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while (philo->global->dead == 0)
|
||||||
|
{
|
||||||
|
is_dead(philo);
|
||||||
|
philo = philo->next;
|
||||||
|
}
|
||||||
|
i = 0;
|
||||||
|
while (i < philo->params->n_phi)
|
||||||
|
{
|
||||||
|
pthread_create(&id[i], NULL, &philo_exec, philo);
|
||||||
|
philo = philo->next;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user