「🏗️」 wip: testing things, might broke.
This commit is contained in:
@ -6,13 +6,13 @@
|
|||||||
/* 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/24 18:47:29 by adjoly ### ########.fr */
|
/* Updated: 2024/07/25 19:49:35 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "philo.h"
|
#include "philo.h"
|
||||||
|
|
||||||
void init_fork(t_init *init, uint16_t philo_nbr)
|
t_init *init_fork(t_init *init, uint16_t philo_nbr)
|
||||||
{
|
{
|
||||||
uint16_t i;
|
uint16_t i;
|
||||||
|
|
||||||
@ -20,13 +20,8 @@ void init_fork(t_init *init, uint16_t philo_nbr)
|
|||||||
while (i < philo_nbr)
|
while (i < 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;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
init[0].data.fork.right = &(init[philo_nbr - 1].data.fork.left);
|
return (init);
|
||||||
i = 1;
|
|
||||||
while (i < philo_nbr)
|
|
||||||
{
|
|
||||||
init[i].data.fork.right = &(init[i - 1].data.fork.left);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -6,28 +6,29 @@
|
|||||||
/* 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 16:18:55 by adjoly ### ########.fr */
|
/* Updated: 2024/07/25 19:30:28 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "philo.h"
|
#include "philo.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
void init_philo(t_pdata data)
|
void init_philo(t_pdata data)
|
||||||
{
|
{
|
||||||
t_init init[PHILO_MAX];
|
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;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
pthread_mutex_init(&check, NULL);
|
pthread_mutex_init(&check, NULL);
|
||||||
|
get_death(false, false);
|
||||||
init_fork(init, data.philo_nbr);
|
init_fork(init, data.philo_nbr);
|
||||||
while (i < data.philo_nbr)
|
while (i < data.philo_nbr)
|
||||||
{
|
{
|
||||||
init[i].data.id = i;
|
init[i].data.id = i + 1;
|
||||||
|
init[i].data.state = SLEEP;
|
||||||
init[i].data.data = data;
|
init[i].data.data = data;
|
||||||
init[i].data.state = EAT;
|
|
||||||
init[i].data.check = ✓
|
init[i].data.check = ✓
|
||||||
r = pthread_create(&init[i].thread, NULL,
|
r = pthread_create(&init[i].thread, NULL,
|
||||||
philo_routine, &(init[i].data));
|
philo_routine, &(init[i].data));
|
||||||
|
@ -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 00:03:53 by adjoly ### ########.fr */
|
/* Updated: 2024/07/25 17:06:09 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -79,7 +79,7 @@ bool get_death(bool in, bool ret);
|
|||||||
* by order of call
|
* by order of call
|
||||||
*/
|
*/
|
||||||
void init_philo(t_pdata data);
|
void init_philo(t_pdata data);
|
||||||
void init_fork(t_init *init, uint16_t philo_nbr);
|
t_init *init_fork(t_init *init, uint16_t philo_nbr);
|
||||||
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);
|
||||||
|
@ -6,13 +6,26 @@
|
|||||||
/* 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 16:27:20 by adjoly ### ########.fr */
|
/* Updated: 2024/07/25 20:06:20 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "philo.h"
|
#include "philo.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
|
void take_fork(t_fork *fork, int id)
|
||||||
|
{
|
||||||
|
if (id % 2)
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(&fork->left);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(fork->right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void *philo_routine(void *content)
|
void *philo_routine(void *content)
|
||||||
{
|
{
|
||||||
t_philo philo;
|
t_philo philo;
|
||||||
@ -21,12 +34,15 @@ void *philo_routine(void *content)
|
|||||||
|
|
||||||
philo = *(t_philo *)content;
|
philo = *(t_philo *)content;
|
||||||
gettimeofday(&(philo.t0), NULL);
|
gettimeofday(&(philo.t0), NULL);
|
||||||
printf("asdf = %hhu\n", philo.data.meal_nbr);
|
|
||||||
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;
|
||||||
|
log_philo(philo);
|
||||||
|
take_fork(&philo.fork, philo.id + 1);
|
||||||
|
log_philo(philo);
|
||||||
philo.state = EAT;
|
philo.state = EAT;
|
||||||
pthread_mutex_lock(&philo.fork.left);
|
|
||||||
pthread_mutex_lock(philo.fork.right);
|
|
||||||
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);
|
pthread_mutex_unlock(&philo.fork.left);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/07/23 17:15:24 by adjoly #+# #+# */
|
/* Created: 2024/07/23 17:15:24 by adjoly #+# #+# */
|
||||||
/* Updated: 2024/07/25 16:08:44 by adjoly ### ########.fr */
|
/* Updated: 2024/07/25 19:57:03 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -20,18 +20,19 @@ uint32_t time_to_ms(void)
|
|||||||
return (time.tv_sec * 1000 + time.tv_usec / 1000);
|
return (time.tv_sec * 1000 + time.tv_usec / 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define SLEEP_SLICE_MS 10
|
||||||
|
|
||||||
bool sleep_phil(uint32_t sleep_time, pthread_mutex_t *death)
|
bool sleep_phil(uint32_t sleep_time, pthread_mutex_t *death)
|
||||||
{
|
{
|
||||||
uint32_t t0;
|
while (sleep_time)
|
||||||
|
|
||||||
t0 = time_to_ms() + sleep_time;
|
|
||||||
while (time_to_ms() < t0)
|
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(death);
|
pthread_mutex_lock(death);
|
||||||
if (get_death(false, true) == true)
|
if (get_death(false, true) == true)
|
||||||
return (true);
|
return (true);
|
||||||
pthread_mutex_unlock(death);
|
pthread_mutex_unlock(death);
|
||||||
usleep(1000);
|
usleep(SLEEP_SLICE_MS * 1000);
|
||||||
|
sleep_time -= SLEEP_SLICE_MS;
|
||||||
}
|
}
|
||||||
return (false);
|
return (false);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/07/24 18:19:14 by adjoly #+# #+# */
|
/* Created: 2024/07/24 18:19:14 by adjoly #+# #+# */
|
||||||
/* Updated: 2024/07/25 16:28:27 by adjoly ### ########.fr */
|
/* Updated: 2024/07/25 16:46:57 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -23,7 +23,6 @@ void start_philo(t_init *init, uint16_t philo_nbr)
|
|||||||
r = pthread_join(init[i].thread, NULL);
|
r = pthread_join(init[i].thread, NULL);
|
||||||
if (r != 0)
|
if (r != 0)
|
||||||
return ;
|
return ;
|
||||||
usleep(500);
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user