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