1
0

🏗️」 wip: testing things, might broke.

This commit is contained in:
2024-07-21 20:55:35 +02:00
parent 4c6644e1b2
commit b706482b26
13 changed files with 155 additions and 27 deletions

View File

@ -6,7 +6,7 @@
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ # # By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/07/07 15:08:52 by adjoly #+# #+# # # Created: 2024/07/07 15:08:52 by adjoly #+# #+# #
# Updated: 2024/07/07 15:24:47 by adjoly ### ########.fr # # Updated: 2024/07/16 20:03:49 by adjoly ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -18,9 +18,9 @@ CC = cc
OBJSDIR = obj/ OBJSDIR = obj/
SRC = $(shell find src -name '*.c') SRC = $(shell find . -name '*.c')
I_DIR = include/ I_DIR = ./
INCLUDE = -I $(I_DIR) INCLUDE = -I $(I_DIR)

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/09 12:02:24 by adjoly #+# #+# */ /* Created: 2024/07/09 12:02:24 by adjoly #+# #+# */
/* Updated: 2024/07/09 13:14:50 by adjoly ### ########.fr */ /* Updated: 2024/07/16 21:12:05 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,5 +15,8 @@
uint16_t get_time_in_ms(struct timeval t0, struct timeval t1) uint16_t get_time_in_ms(struct timeval t0, struct timeval t1)
{ {
return (((t1.tv_sec-t0.tv_sec) * 1000000 + t1.tv_usec-t0.tv_usec)/1000); int diff;
diff = t1.tv_sec - t0.tv_sec;
return (((diff) * 1000000 + diff) / 1000);
} }

27
philo/init_fork.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_fork.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/16 19:53:01 by adjoly #+# #+# */
/* Updated: 2024/07/18 21:03:00 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
void init_fork(t_philo *philo, t_pdata data)
{
int i;
i = 0;
while (philo[i] && i <= data.philo_nbr)
{
pthread_mutex_init(philo[i].fork.left);
if (i == data.philo_nbr)
philo[i].fork.right = &philo[0].fork.left;
else
philo[i].fork.right = &philo[i + 1].fork.left;
i++;
}
}

34
philo/init_philo.c Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_philo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 14:36:59 by adjoly #+# #+# */
/* Updated: 2024/07/17 22:53:48 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
void init_philo(t_pdata data)
{
t_philo philo[PHILO_MAX];
t_philo *tmp;
pthread_mutex_t *print;
int ret;
tmp = philo;
i = 0;
init_fork(philo, data);
while (tmp && i < data->philo_nbr)
{
ret = pthread_create(&(tmp->thread), NULL, philo_routine, tmp);
if (ret != 0)
return ;
tmp->data = data;
tmp->state = EAT;
tmp++;
}
}

View File

@ -6,23 +6,28 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 16:12:20 by adjoly #+# #+# */ /* Created: 2024/07/07 16:12:20 by adjoly #+# #+# */
/* Updated: 2024/07/08 16:22:28 by adjoly ### ########.fr */ /* Updated: 2024/07/16 20:24:43 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philo.h" #include "philo.h"
#include "philo_msg.h" #include "philo_msg.h"
#include <stdio.h>
void log_philo(uint32_t timestamp, uint16_t philo, t_pstate type) void log_philo(uint32_t timestamp, t_philo philo)
{ {
if (type == EAT) static pthread_mutex_t print = {0};
printf("%u %hu %s", timestamp, philo, EATING_MSG);
else if (type == THINK) pthread_mutex_lock(&print);
printf("%u %hu %s", timestamp, philo, THINK_MSG); if (philo->state == EAT)
else if (type == SLEEP) printf("%u %hu %s", timestamp, philo->nbr, EATING_MSG);
printf("%u %hu %s", timestamp, philo, SLEEP_MSG); else if (philo->state == THINK)
else if (type == DIED) printf("%u %hu %s", timestamp, philo->nbr, THINK_MSG);
printf("%u %hu %s", timestamp, philo, DIED_MSG); else if (philo->state == SLEEP)
else if (type == FORK_TAKEN) printf("%u %hu %s", timestamp, philo->nbr, SLEEP_MSG);
printf("%u %hu %s", timestamp, philo, FORK_MSG); else if (philo->state == DIED)
printf("%u %hu %s", timestamp, philo->nbr, DIED_MSG);
else if (philo->state == FORK_TAKEN)
printf("%u %hu %s", timestamp, philo->nbr, FORK_MSG);
pthread_mutex_unlock(&print);
} }

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/09 13:17:55 by adjoly ### ########.fr */ /* Updated: 2024/07/10 18:47:00 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

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/09 12:04:14 by adjoly ### ########.fr */ /* Updated: 2024/07/18 18:30:56 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,6 +17,9 @@
# include <stdint.h> # include <stdint.h>
# include <stdbool.h> # include <stdbool.h>
# include <sys/time.h> # include <sys/time.h>
# include <sys/types.h>
# define PHILO_MAX 200
typedef enum s_pstate typedef enum s_pstate
{ {
@ -37,10 +40,39 @@ typedef struct s_pdata
bool error; bool error;
} t_pdata; } t_pdata;
typedef struct s_fork
{
pthread_mutex_t left;
pthread_mutex_t *right;
} t_fork;
typedef struct s_philo
{
uint16_t nbr;
struct timeval t0;
t_fork fork;
t_pstate state;
t_pdata data;
pthread_t thread;
} t_philo;
/**
* Utils
*/
void log_philo(uint32_t timestamp, uint16_t philo, t_pstate type); void log_philo(uint32_t timestamp, uint16_t philo, t_pstate type);
t_pdata philo_parse(char **argv, int ac); t_pdata philo_parse(char **argv, int ac);
long long ft_atoll(const char *nptr);
uint16_t get_time_in_ms(struct timeval t0, struct timeval t1); uint16_t get_time_in_ms(struct timeval t0, struct timeval t1);
void *philo_routine(void *param)
/**
* Main path
* by order of call
*/
t_philo *init_philo(t_pdata data, uint16_t philo_nbr);
void init_fork(t_philo *philo, t_pdata data);
void start_philo(t_philo *philo, t_pdata data);
/** /**
* For debug purpose to be REMOVED * For debug purpose to be REMOVED
*/ */

View File

@ -1,18 +1,19 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* utils.h :+: :+: :+: */ /* philo_routine.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 17:46:19 by adjoly #+# #+# */ /* Created: 2024/07/16 19:54:02 by adjoly #+# #+# */
/* Updated: 2024/07/08 17:46:50 by adjoly ### ########.fr */ /* Updated: 2024/07/16 20:57:54 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef UTILS_H void *philo_routine(void *param)
# define UTILS_H {
t_philo philo;
long long ft_atoll(const char *nptr); philo = (t_philo)param;
log
#endif }

26
philo/start_philo.c Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* start_philo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/18 18:31:21 by adjoly #+# #+# */
/* Updated: 2024/07/18 19:08:43 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
void start_philo(t_philo *philo, t_pdata data)
{
int i;
int ret;
i = 0;
while (philo[i] && i < data.philo_nbr)
{
ret = pthread_join(&(philo->thread), NULL);
if (ret != 0)
return ;
i++;
}
}