mise a la norme et renommer fonctions
This commit is contained in:
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -1,3 +0,0 @@
|
|||||||
[submodule "libft"]
|
|
||||||
path = libft
|
|
||||||
url = git@bitbucket.org:LuckyLaszlo/libft.git
|
|
||||||
21
Makefile
21
Makefile
@@ -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:
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|
||||||
#endif
|
// generic.c
|
||||||
|
int ft_atoi(const char *str);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
@@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* 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
|
||||||
|
|
||||||
@@ -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
1
libft
Submodule libft deleted from af88c1ce23
12
srcs/exec.c
12
srcs/exec.c
@@ -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)
|
||||||
|
|||||||
@@ -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;
|
result = (result * 10) + (str[i] - '0');
|
||||||
|
i++;
|
||||||
gettimeofday(&stime, NULL);
|
|
||||||
philo->t_last_meal.ts = stime.tv_sec;
|
|
||||||
philo->t_last_meal.tu = stime.tv_usec;
|
|
||||||
}
|
}
|
||||||
|
if (is_negative)
|
||||||
int diff_time(t_time old, struct timeval new)
|
result = result * -1;
|
||||||
{
|
return (result);
|
||||||
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;
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|||||||
12
srcs/init.c
12
srcs/init.c
@@ -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)
|
||||||
|
|||||||
12
srcs/main.c
12
srcs/main.c
@@ -1,3 +1,15 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* 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)
|
||||||
|
|||||||
@@ -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
60
srcs/utils.c
Normal 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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user