debut implementation death tout est casse
This commit is contained in:
4
Makefile
4
Makefile
@@ -23,7 +23,9 @@ LIBFT = $(LIBFT_D)/libft.a
|
|||||||
|
|
||||||
SRCS = main.c \
|
SRCS = main.c \
|
||||||
init.c \
|
init.c \
|
||||||
exec.c
|
exec.c \
|
||||||
|
time.c \
|
||||||
|
message.c
|
||||||
|
|
||||||
DIR_OBJS = builds
|
DIR_OBJS = builds
|
||||||
OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o)
|
OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o)
|
||||||
|
|||||||
@@ -7,5 +7,12 @@ t_philo *init(int ac, char **av, pthread_t **id);
|
|||||||
// exec.c
|
// exec.c
|
||||||
void *philo_exec(void *arg);
|
void *philo_exec(void *arg);
|
||||||
|
|
||||||
|
// time.c
|
||||||
|
void init_time(t_philo *philo);
|
||||||
|
void update_time(t_philo *philo);
|
||||||
|
|
||||||
|
// message.c
|
||||||
|
void print_message(t_philo *philo, char *msg);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ typedef struct s_philo
|
|||||||
long int t_start_u;
|
long int t_start_u;
|
||||||
long int t_last_meal_s;
|
long int t_last_meal_s;
|
||||||
long int t_last_meal_u;
|
long int t_last_meal_u;
|
||||||
struct s_philo *prev;
|
int *death;
|
||||||
struct s_philo *next;
|
struct s_philo *next;
|
||||||
} t_philo;
|
} t_philo;
|
||||||
|
|
||||||
|
|||||||
67
srcs/exec.c
67
srcs/exec.c
@@ -1,60 +1,35 @@
|
|||||||
#include "philo.h"
|
#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;
|
(void)philo;
|
||||||
struct timeval stime;
|
usleep(time * 1000);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// long int t_start_s;
|
int ret_unlock(t_mtx mutex, int ret)
|
||||||
// long int t_start_u;
|
|
||||||
// long int t_last_meal_s;
|
|
||||||
// long int t_last_meal_u;
|
|
||||||
void init_time(t_philo *philo)
|
|
||||||
{
|
{
|
||||||
struct timeval stime;
|
pthread_mutex_unlock(&mutex);
|
||||||
|
return (ret);
|
||||||
gettimeofday(&stime, NULL);
|
|
||||||
philo->t_start_s = stime.tv_sec;
|
|
||||||
philo->t_start_u = stime.tv_usec;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_time(t_philo *philo)
|
int take_forks(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)
|
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&(philo->m_fork));
|
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");
|
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");
|
print_message(philo, "is eating");
|
||||||
usleep(philo->params->t_eat * 1000);
|
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));
|
pthread_mutex_unlock(&(philo->m_fork));
|
||||||
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// int n_phi; // number_of_philosophers
|
// int n_phi; // number_of_philosophers
|
||||||
@@ -72,10 +47,10 @@ void *philo_exec(void *arg)
|
|||||||
usleep(10 * 1000);
|
usleep(10 * 1000);
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
take_forks(philo);
|
if (take_forks(philo) != 0)
|
||||||
|
break ;
|
||||||
print_message(philo, "is sleeping");
|
print_message(philo, "is sleeping");
|
||||||
usleep(philo->params->t_slp * 1000);
|
go_sleep(philo, philo->params->t_slp);
|
||||||
print_message(philo, "is thinking");
|
print_message(philo, "is thinking");
|
||||||
}
|
}
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
|||||||
29
srcs/init.c
29
srcs/init.c
@@ -1,6 +1,6 @@
|
|||||||
#include "philo.h"
|
#include "philo.h"
|
||||||
|
|
||||||
t_philo *lst_add_philo(t_philo *philo, t_params *params, t_mtx *m_print, int i)
|
t_philo *lst_add_philo(t_params *params, t_mtx *m_print, int *death, int i)
|
||||||
{
|
{
|
||||||
t_philo *new;
|
t_philo *new;
|
||||||
|
|
||||||
@@ -12,31 +12,32 @@ t_philo *lst_add_philo(t_philo *philo, t_params *params, t_mtx *m_print, int i)
|
|||||||
if (pthread_mutex_init(&(new->m_fork), NULL) != 0)
|
if (pthread_mutex_init(&(new->m_fork), NULL) != 0)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
new->m_print = m_print;
|
new->m_print = m_print;
|
||||||
if (philo)
|
new->death = death;
|
||||||
philo->next = new;
|
new->next = NULL;
|
||||||
new->prev = philo;
|
|
||||||
return (new);
|
return (new);
|
||||||
}
|
}
|
||||||
|
|
||||||
// looping chained list
|
// looping chained list
|
||||||
t_philo *init_chain_philo(t_params *params, t_mtx *m_print)
|
t_philo *init_chain_philo(t_params *params, t_mtx *m_print, int *death)
|
||||||
{
|
{
|
||||||
t_philo *end;
|
|
||||||
t_philo *philo;
|
t_philo *philo;
|
||||||
|
t_philo *tmp;
|
||||||
|
t_philo *start;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
philo = NULL;
|
philo = NULL;
|
||||||
while (i < params->n_phi)
|
while (i < params->n_phi)
|
||||||
{
|
{
|
||||||
philo = lst_add_philo(philo, params, m_print, i);
|
tmp = lst_add_philo(params, m_print, death, i);
|
||||||
|
if (philo)
|
||||||
|
philo->next = tmp;
|
||||||
|
else
|
||||||
|
start = tmp;
|
||||||
|
philo = tmp;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
end = philo;
|
philo->next = start;
|
||||||
while (philo->prev != NULL)
|
|
||||||
philo = philo->prev;
|
|
||||||
philo->prev = end;
|
|
||||||
end->next = philo;
|
|
||||||
return (philo);
|
return (philo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,6 +65,7 @@ t_philo *init(int ac, char **av, pthread_t **id)
|
|||||||
t_philo *philo;
|
t_philo *philo;
|
||||||
t_params *params;
|
t_params *params;
|
||||||
t_mtx *m_print;
|
t_mtx *m_print;
|
||||||
|
int death;
|
||||||
|
|
||||||
params = init_params(ac, av);
|
params = init_params(ac, av);
|
||||||
if (params == NULL)
|
if (params == NULL)
|
||||||
@@ -74,7 +76,8 @@ t_philo *init(int ac, char **av, pthread_t **id)
|
|||||||
m_print = malloc(sizeof(t_mtx));
|
m_print = malloc(sizeof(t_mtx));
|
||||||
if (pthread_mutex_init(m_print, NULL) != 0)
|
if (pthread_mutex_init(m_print, NULL) != 0)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
philo = init_chain_philo(params, m_print);
|
death = 0;
|
||||||
|
philo = init_chain_philo(params, m_print, &death);
|
||||||
if (philo == NULL)
|
if (philo == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
return (philo);
|
return (philo);
|
||||||
|
|||||||
32
srcs/main.c
32
srcs/main.c
@@ -1,14 +1,26 @@
|
|||||||
#include "philo.h"
|
#include "philo.h"
|
||||||
|
|
||||||
void launch_threads(t_philo *philo, pthread_t *id, int n)
|
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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void watch_threads(t_philo *philo, pthread_t *id)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < n)
|
while (i < philo->params->n_phi)
|
||||||
{
|
{
|
||||||
pthread_create(&id[i], NULL, &philo_exec, philo);
|
pthread_join(id[i], NULL);
|
||||||
philo = philo->next;
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -17,20 +29,12 @@ int main(int ac, char **av)
|
|||||||
{
|
{
|
||||||
pthread_t *id;
|
pthread_t *id;
|
||||||
t_philo *philo;
|
t_philo *philo;
|
||||||
int i;
|
|
||||||
int n;
|
|
||||||
|
|
||||||
philo = init(ac, av, &id);
|
philo = init(ac, av, &id);
|
||||||
if (philo == NULL)
|
if (philo == NULL)
|
||||||
return (0);
|
return (0);
|
||||||
n = philo->params->n_phi;
|
launch_threads(philo, id);
|
||||||
launch_threads(philo, id, n);
|
watch_threads(philo, id);
|
||||||
i = 0;
|
|
||||||
while (i < n)
|
|
||||||
{
|
|
||||||
pthread_join(id[i], NULL);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
write(1, "main function\n", 14);
|
write(1, "main function\n", 14);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
23
srcs/main0.c
23
srcs/main0.c
@@ -1,23 +0,0 @@
|
|||||||
#include "philo.h"
|
|
||||||
|
|
||||||
void *philo_exec(void *arg)
|
|
||||||
{
|
|
||||||
t_philo *philo;
|
|
||||||
|
|
||||||
philo = (t_philo*)arg;
|
|
||||||
while (1)
|
|
||||||
write(1, philo->str, ft_strlen(philo->str));
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
pthread_t id;
|
|
||||||
t_philo *philo;
|
|
||||||
|
|
||||||
philo = malloc(sizeof(philo));
|
|
||||||
philo->str = "i'm philosopher\n";
|
|
||||||
pthread_create(&id, NULL, &philo_exec, philo);
|
|
||||||
while (1)
|
|
||||||
write(1, "main function\n", 14);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
#include "philo.h"
|
|
||||||
|
|
||||||
void *philo_exec(void *arg)
|
|
||||||
{
|
|
||||||
t_philo *philo;
|
|
||||||
|
|
||||||
philo = (t_philo*)arg;
|
|
||||||
while (1)
|
|
||||||
write(1, philo->str, ft_strlen(philo->str));
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
pthread_t id;
|
|
||||||
t_philo *philo;
|
|
||||||
|
|
||||||
philo = malloc(sizeof(philo));
|
|
||||||
philo->str = "i'm philosopher\n";
|
|
||||||
pthread_create(&id, NULL, &philo_exec, philo);
|
|
||||||
pthread_join(id, NULL);
|
|
||||||
while (1)
|
|
||||||
write(1, "main function\n", 14);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
#include "philo.h"
|
|
||||||
|
|
||||||
void *philo_exec(void *arg)
|
|
||||||
{
|
|
||||||
t_philo *philo;
|
|
||||||
|
|
||||||
philo = (t_philo*)arg;
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
write(1, philo->str, ft_strlen(philo->str));
|
|
||||||
write(1, " nb ", 4);
|
|
||||||
ft_putnbr_fd(philo->nbr, 1);
|
|
||||||
write(1, "\n", 1);
|
|
||||||
sleep (1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
pthread_t *tid;
|
|
||||||
int n_philo;
|
|
||||||
int i;
|
|
||||||
t_philo *philo;
|
|
||||||
|
|
||||||
n_philo = 3;
|
|
||||||
philo = malloc(sizeof(philo) * n_philo);
|
|
||||||
tid = malloc(sizeof(pthread_t) * n_philo);
|
|
||||||
i = 0;
|
|
||||||
while (i < n_philo)
|
|
||||||
{
|
|
||||||
philo[i].str = "i'm philosopher";
|
|
||||||
philo[i].nbr = i;
|
|
||||||
pthread_create(&tid[i], NULL, &philo_exec, &philo[i]);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
write(1, "main function\n", 14);
|
|
||||||
sleep (1);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
#include "philo.h"
|
|
||||||
|
|
||||||
pthread_mutex_t mutex;
|
|
||||||
|
|
||||||
void *philo_exec(void *arg)
|
|
||||||
{
|
|
||||||
t_philo *philo;
|
|
||||||
|
|
||||||
pthread_mutex_lock(&mutex);
|
|
||||||
philo = (t_philo*)arg;
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
write(1, philo->str, ft_strlen(philo->str));
|
|
||||||
write(1, " nb ", 4);
|
|
||||||
ft_putnbr_fd(philo->nbr, 1);
|
|
||||||
write(1, "\n", 1);
|
|
||||||
sleep (1);
|
|
||||||
}
|
|
||||||
pthread_mutex_unlock(&mutex);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
pthread_t *tid;
|
|
||||||
int n_philo;
|
|
||||||
int i;
|
|
||||||
t_philo *philo;
|
|
||||||
|
|
||||||
n_philo = 3;
|
|
||||||
philo = malloc(sizeof(philo) * n_philo);
|
|
||||||
tid = malloc(sizeof(pthread_t) * n_philo);
|
|
||||||
i = 0;
|
|
||||||
pthread_mutex_init(&mutex, NULL);
|
|
||||||
while (i < n_philo)
|
|
||||||
{
|
|
||||||
philo[i].str = "i'm philosopher";
|
|
||||||
philo[i].nbr = i;
|
|
||||||
pthread_create(&tid[i], NULL, &philo_exec, &philo[i]);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
write(1, "main function\n", 14);
|
|
||||||
sleep (1);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
22
srcs/message.c
Normal file
22
srcs/message.c
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#include "philo.h"
|
||||||
|
|
||||||
|
void print_message(t_philo *philo, char *msg)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
19
srcs/time.c
Normal file
19
srcs/time.c
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#include "philo.h"
|
||||||
|
|
||||||
|
void init_time(t_philo *philo)
|
||||||
|
{
|
||||||
|
struct timeval stime;
|
||||||
|
|
||||||
|
gettimeofday(&stime, NULL);
|
||||||
|
philo->t_start_s = stime.tv_sec;
|
||||||
|
philo->t_start_u = stime.tv_usec;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user