debut implementation death tout est casse

This commit is contained in:
Hugo LAMY
2022-01-21 18:32:37 +01:00
parent be439700e9
commit 6a4aad9503
12 changed files with 107 additions and 214 deletions

View File

@@ -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);

View File

@@ -1,6 +1,6 @@
#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;
@@ -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)
return (NULL);
new->m_print = m_print;
if (philo)
philo->next = new;
new->prev = philo;
new->death = death;
new->next = NULL;
return (new);
}
// 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 *tmp;
t_philo *start;
int i;
i = 0;
philo = NULL;
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++;
}
end = philo;
while (philo->prev != NULL)
philo = philo->prev;
philo->prev = end;
end->next = philo;
philo->next = start;
return (philo);
}
@@ -64,6 +65,7 @@ t_philo *init(int ac, char **av, pthread_t **id)
t_philo *philo;
t_params *params;
t_mtx *m_print;
int death;
params = init_params(ac, av);
if (params == NULL)
@@ -74,7 +76,8 @@ t_philo *init(int ac, char **av, pthread_t **id)
m_print = malloc(sizeof(t_mtx));
if (pthread_mutex_init(m_print, NULL) != 0)
return (NULL);
philo = init_chain_philo(params, m_print);
death = 0;
philo = init_chain_philo(params, m_print, &death);
if (philo == NULL)
return (NULL);
return (philo);

View File

@@ -1,14 +1,26 @@
#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;
i = 0;
while (i < n)
while (i < philo->params->n_phi)
{
pthread_create(&id[i], NULL, &philo_exec, philo);
philo = philo->next;
pthread_join(id[i], NULL);
i++;
}
}
@@ -17,20 +29,12 @@ int main(int ac, char **av)
{
pthread_t *id;
t_philo *philo;
int i;
int n;
philo = init(ac, av, &id);
if (philo == NULL)
return (0);
n = philo->params->n_phi;
launch_threads(philo, id, n);
i = 0;
while (i < n)
{
pthread_join(id[i], NULL);
i++;
}
launch_threads(philo, id);
watch_threads(philo, id);
write(1, "main function\n", 14);
return (0);
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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
View 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
View 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;
}