1
0

🏗️」 wip: testing things, might broke.

This commit is contained in:
2024-07-26 15:43:22 +02:00
parent fb1c93389f
commit 930867803d
5 changed files with 21 additions and 29 deletions

View File

@ -6,22 +6,24 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/22 15:29:08 by adjoly #+# #+# */ /* Created: 2024/07/22 15:29:08 by adjoly #+# #+# */
/* Updated: 2024/07/25 19:49:35 by adjoly ### ########.fr */ /* Updated: 2024/07/26 15:24:07 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
t_init *init_fork(t_init *init, uint16_t philo_nbr) void init_fork(t_pdata data)
{ {
uint16_t i; uint16_t i;
t_init init[PHILO_MAX];
i = 0; i = 0;
while (i < philo_nbr) while (i < data.philo_nbr)
{ {
pthread_mutex_init(&(init[i].data.fork.left), NULL); pthread_mutex_init(&(init[i].data.fork.left), NULL);
init[i].data.fork.right = &init[(i - 1) % philo_nbr].data.fork.left; init[i].data.fork.right = &init[(i - 1) % data.philo_nbr].data.fork.left;
i++; i++;
} }
return (init); init_philo(data, init);
} }

View File

@ -6,16 +6,15 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 14:36:59 by adjoly #+# #+# */ /* Created: 2024/07/11 14:36:59 by adjoly #+# #+# */
/* Updated: 2024/07/25 19:30:28 by adjoly ### ########.fr */ /* Updated: 2024/07/26 15:25:58 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
#include <stdlib.h> #include <stdlib.h>
void init_philo(t_pdata data) void init_philo(t_pdata data, t_init *init)
{ {
t_init *init = malloc((sizeof(t_init) + 1) * data.philo_nbr);
int r; int r;
pthread_mutex_t check; pthread_mutex_t check;
uint16_t i; uint16_t i;
@ -23,7 +22,6 @@ void init_philo(t_pdata data)
i = 0; i = 0;
pthread_mutex_init(&check, NULL); pthread_mutex_init(&check, NULL);
get_death(false, false); get_death(false, false);
init_fork(init, data.philo_nbr);
while (i < data.philo_nbr) while (i < data.philo_nbr)
{ {
init[i].data.id = i + 1; init[i].data.id = i + 1;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 15:10:29 by adjoly #+# #+# */ /* Created: 2024/07/07 15:10:29 by adjoly #+# #+# */
/* Updated: 2024/07/25 16:24:01 by adjoly ### ########.fr */ /* Updated: 2024/07/26 15:23:45 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,6 +23,6 @@ int main(int ac, char **av)
data = philo_parse(av, ac); data = philo_parse(av, ac);
if (data.error == true) if (data.error == true)
return (EXIT_FAILURE); return (EXIT_FAILURE);
init_philo(data); init_fork(data);
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 15:11:02 by adjoly #+# #+# */ /* Created: 2024/07/07 15:11:02 by adjoly #+# #+# */
/* Updated: 2024/07/25 17:06:09 by adjoly ### ########.fr */ /* Updated: 2024/07/26 15:25:57 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -78,11 +78,11 @@ bool get_death(bool in, bool ret);
* Main path * Main path
* by order of call * by order of call
*/ */
void init_philo(t_pdata data); void init_fork(t_pdata data);
t_init *init_fork(t_init *init, uint16_t philo_nbr); void init_philo(t_pdata data, t_init *init);
void start_philo(t_init *init, uint16_t philo_nbr); void start_philo(t_init *init, uint16_t philo_nbr);
void *philo_routine(void *content); void *philo_routine(void *content);
/** /**
* For debug purpose to be REMOVED * For debug purpose to be REMOVED

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/22 21:24:53 by adjoly #+# #+# */ /* Created: 2024/07/22 21:24:53 by adjoly #+# #+# */
/* Updated: 2024/07/25 20:06:20 by adjoly ### ########.fr */ /* Updated: 2024/07/26 15:15:13 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -36,26 +36,18 @@ void *philo_routine(void *content)
gettimeofday(&(philo.t0), NULL); gettimeofday(&(philo.t0), NULL);
while (i < philo.data.meal_nbr) while (i < philo.data.meal_nbr)
{ {
log_philo(philo);
take_fork(&philo.fork, philo.id);
philo.state = FORK_TAKEN; philo.state = FORK_TAKEN;
take_fork(&philo.fork, philo.id);
log_philo(philo); log_philo(philo);
take_fork(&philo.fork, philo.id + 1); take_fork(&philo.fork, philo.id + 1);
log_philo(philo); log_philo(philo);
philo.state = EAT; philo.state = EAT;
log_philo(philo); log_philo(philo);
death = sleep_phil(philo.data.eat_time, philo.check); death = sleep_phil(philo.data.eat_time, philo.check);
pthread_mutex_unlock(&philo.fork.left); if (death == true)
return (NULL);
pthread_mutex_unlock(philo.fork.right); pthread_mutex_unlock(philo.fork.right);
if (death == true) pthread_mutex_unlock(&philo.fork.left);
return (NULL);
philo.state = SLEEP;
log_philo(philo);
death = sleep_phil(philo.data.sleep_time, philo.check);
if (death == true)
return (NULL);
philo.state = THINK;
log_philo(philo);
i++; i++;
} }
return (NULL); return (NULL);