mise a la norme et renommer fonctions

This commit is contained in:
Hugo LAMY
2022-01-26 15:33:29 +01:00
parent ab0c4efa35
commit b192efa4a9
13 changed files with 213 additions and 77 deletions

3
.gitmodules vendored
View File

@@ -1,3 +0,0 @@
[submodule "libft"]
path = libft
url = git@bitbucket.org:LuckyLaszlo/libft.git

View File

@@ -7,7 +7,7 @@ CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -g3 # del g3
VPATH = $(DIR_SRCS) VPATH = $(DIR_SRCS)
DIR_SRCS = srcs DIR_SRCS = srcs
INCLUDES = -I$(HEADERS_D) -I$(LIBFT_D) INCLUDES = -I$(HEADERS_D)
HEADERS_D = ./headers HEADERS_D = ./headers
HEADERS = philo.h \ HEADERS = philo.h \
@@ -15,16 +15,13 @@ HEADERS = philo.h \
philo_proto.h \ philo_proto.h \
philo_macro.h philo_macro.h
LIBS = -L $(LIBFT_D) -lft \ LIBS = -lpthread
-lpthread
LIBFT_D = ./libft
LIBFT = $(LIBFT_D)/libft.a
SRCS = main.c \ SRCS = main.c \
init.c \ init.c \
main_thread.c \ pere_fouettard.c \
exec.c \ exec.c \
utils.c \
generic.c generic.c
DIR_OBJS = builds DIR_OBJS = builds
@@ -34,13 +31,7 @@ OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o)
# ------ RULES ------- # ------ RULES -------
# -------------------- # --------------------
all: subsystem $(NAME) all: $(NAME)
subsystem:
@cd $(LIBFT_D) && $(MAKE)
$(LIBFT): # dispensable. utile seulement pour un appel direct à $(NAME), si $(LIBFT) n'existe pas
cd $(LIBFT_D) && $(MAKE)
$(DIR_OBJS)/%.o: %.c | $(DIR_OBJS) $(DIR_OBJS)/%.o: %.c | $(DIR_OBJS)
$(CC) $(CFLAGS) -c $< -o $@ $(CC) $(CFLAGS) -c $< -o $@
@@ -50,7 +41,7 @@ $(DIR_OBJS):
$(OBJS): $(HEADERS:%=$(HEADERS_D)/%) $(OBJS): $(HEADERS:%=$(HEADERS_D)/%)
$(NAME): $(OBJS) $(LIBFT) $(NAME): $(OBJS)
$(CC) $(OBJS) -o $(NAME) $(LIBS) $(CC) $(OBJS) -o $(NAME) $(LIBS)
clean: clean:

View File

@@ -1,7 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:27:01 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:27:02 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PHILO_H #ifndef PHILO_H
# define PHILO_H # define PHILO_H
# include "libft.h"
# include <stdio.h> # include <stdio.h>
# include <stdlib.h> # include <stdlib.h>
# include <string.h> # include <string.h>

View File

@@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo_macro.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:27:08 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:27:09 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PHILO_MACRO_H #ifndef PHILO_MACRO_H
# define PHILO_MACRO_H # define PHILO_MACRO_H

View File

@@ -1,21 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo_proto.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:27:13 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:29:49 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PHILO_PROTO_H #ifndef PHILO_PROTO_H
# define PHILO_PROTO_H # define PHILO_PROTO_H
// init.c // init.c
t_philo *init(int ac, char **av, pthread_t **id); t_philo *init(int ac, char **av, pthread_t **id);
// main_thread.c // pere_fouettard.c
int is_dead(t_philo *philo); int is_dead(t_philo *philo);
void launch_threads(t_philo *philo, pthread_t *id); void launch_threads(t_philo *philo, pthread_t *id);
// exec.c // exec.c
void *philo_exec(void *arg); void *philo_exec(void *arg);
// generic.c // utils.c
void init_time(t_philo *philo); void init_time(t_philo *philo);
void update_time(t_philo *philo); void update_time(t_philo *philo);
int diff_time(t_time old, struct timeval new); int diff_time(t_time old, struct timeval new);
int print_message(t_philo *philo, char *clr, char *msg); int print_message(t_philo *philo, char *clr, char *msg);
// generic.c
int ft_atoi(const char *str);
#endif #endif

View File

@@ -1,7 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo_struct.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:27:23 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:29:09 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PHILO_STRUCT_H #ifndef PHILO_STRUCT_H
# define PHILO_STRUCT_H # define PHILO_STRUCT_H
typedef pthread_mutex_t t_mtx; typedef pthread_mutex_t t_mtx;
typedef struct s_time typedef struct s_time
{ {
@@ -11,11 +23,11 @@ typedef struct s_time
typedef struct s_params typedef struct s_params
{ {
int n_phi; // number_of_philosophers int n_phi;
int t_die; // time_to_die int t_die;
int t_eat; // time_to_eat int t_eat;
int t_slp; // time_to_sleep int t_slp;
int n_eat; // [number_of_times_each_philosopher_must_eat] int n_eat;
} t_params; } t_params;
typedef struct s_global typedef struct s_global

1
libft

Submodule libft deleted from af88c1ce23

View File

@@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:28 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:32:57 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
void action_delay(t_philo *philo, int action_time) void action_delay(t_philo *philo, int action_time)
@@ -45,7 +57,7 @@ void *philo_exec(void *arg)
{ {
t_philo *philo; t_philo *philo;
philo = (t_philo*)arg; philo = (t_philo *)arg;
init_time(philo); init_time(philo);
if (philo->p_nbr % 2 == 0) if (philo->p_nbr % 2 == 0)
usleep(10 * 1000); usleep(10 * 1000);

View File

@@ -1,48 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* generic.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:19 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:31:15 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
void init_time(t_philo *philo) int ft_atoi(const char *str)
{ {
struct timeval stime; int i;
int result;
int is_negative;
if (philo->global->t_start.ts == 0) is_negative = 0;
result = 0;
i = 0;
while (str[i] == ' ' || (str[i] >= 9 && str[i] <= 13))
i++;
if (str[i] == '+')
i++;
else if (str[i] == '-')
{ {
gettimeofday(&stime, NULL); is_negative = 1;
philo->global->t_start.ts = stime.tv_sec; i++;
philo->global->t_start.tu = stime.tv_usec;
} }
philo->t_last_meal.ts = philo->global->t_start.ts; while (str[i] >= '0' && str[i] <= '9')
philo->t_last_meal.tu = philo->global->t_start.tu;
}
void update_time(t_philo *philo)
{
struct timeval stime;
gettimeofday(&stime, NULL);
philo->t_last_meal.ts = stime.tv_sec;
philo->t_last_meal.tu = stime.tv_usec;
}
int diff_time(t_time old, struct timeval new)
{
return ((new.tv_sec - old.ts) * 1000 + (new.tv_usec - old.tu) / 1000);
}
int print_message(t_philo *philo, char *clr, char *msg)
{
long int time_stamp;
struct timeval stime;
pthread_mutex_lock(&(philo->global->m_print));
if (philo->global->dead)
{ {
pthread_mutex_unlock(&(philo->global->m_print)); result = (result * 10) + (str[i] - '0');
return (1); i++;
} }
gettimeofday(&stime, NULL); if (is_negative)
time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000; result = result * -1;
time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000; return (result);
ft_printf("%s%i %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET);
pthread_mutex_unlock(&(philo->global->m_print));
return (0);
} }

View File

@@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:34 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:30:35 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
t_philo *lst_add_philo(t_params *params, t_global *global, int i) t_philo *lst_add_philo(t_params *params, t_global *global, int i)

View File

@@ -1,14 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:40 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:32:30 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
int main(int ac, char **av) int main(int ac, char **av)
{ {
pthread_t *id; pthread_t *id;
t_philo *philo; t_philo *philo;
int i; int i;
philo = init(ac, av, &id); philo = init(ac, av, &id);
if (philo == NULL) if (philo == NULL)
return (0); return (0);
launch_threads(philo, id); launch_threads(philo, id);
i = 0; i = 0;
while (i < philo->params->n_phi) while (i < philo->params->n_phi)
@@ -16,5 +28,5 @@ int main(int ac, char **av)
pthread_join(id[i], NULL); pthread_join(id[i], NULL);
i++; i++;
} }
return (0); return (0);
} }

View File

@@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pere_fouettard.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:49 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:31:40 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h" #include "philo.h"
int is_dead(t_philo *philo) int is_dead(t_philo *philo)
@@ -17,10 +29,9 @@ int is_dead(t_philo *philo)
{ {
pthread_mutex_lock(&(philo->global->m_print)); pthread_mutex_lock(&(philo->global->m_print));
philo->global->dead = 1; philo->global->dead = 1;
time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000; time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000;
time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000; time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000;
ft_printf("%s%i %i died%s\n", B_RED, time_stamp, philo->p_nbr, RESET); printf("%s%li %i died%s\n", B_RED, time_stamp, philo->p_nbr, RESET);
pthread_mutex_unlock(&(philo->global->m_print)); pthread_mutex_unlock(&(philo->global->m_print));
return (1); return (1);
} }

60
srcs/utils.c Normal file
View File

@@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:54 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:30:55 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
void init_time(t_philo *philo)
{
struct timeval stime;
if (philo->global->t_start.ts == 0)
{
gettimeofday(&stime, NULL);
philo->global->t_start.ts = stime.tv_sec;
philo->global->t_start.tu = stime.tv_usec;
}
philo->t_last_meal.ts = philo->global->t_start.ts;
philo->t_last_meal.tu = philo->global->t_start.tu;
}
void update_time(t_philo *philo)
{
struct timeval stime;
gettimeofday(&stime, NULL);
philo->t_last_meal.ts = stime.tv_sec;
philo->t_last_meal.tu = stime.tv_usec;
}
int diff_time(t_time old, struct timeval new)
{
return ((new.tv_sec - old.ts) * 1000 + (new.tv_usec - old.tu) / 1000);
}
int print_message(t_philo *philo, char *clr, char *msg)
{
long int time_stamp;
struct timeval stime;
pthread_mutex_lock(&(philo->global->m_print));
if (philo->global->dead)
{
pthread_mutex_unlock(&(philo->global->m_print));
return (1);
}
gettimeofday(&stime, NULL);
time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000;
time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000;
printf("%s%li %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET);
pthread_mutex_unlock(&(philo->global->m_print));
return (0);
}