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)
|
||||
DIR_SRCS = srcs
|
||||
|
||||
INCLUDES = -I$(HEADERS_D) -I$(LIBFT_D)
|
||||
INCLUDES = -I$(HEADERS_D)
|
||||
|
||||
HEADERS_D = ./headers
|
||||
HEADERS = philo.h \
|
||||
@@ -15,16 +15,13 @@ HEADERS = philo.h \
|
||||
philo_proto.h \
|
||||
philo_macro.h
|
||||
|
||||
LIBS = -L $(LIBFT_D) -lft \
|
||||
-lpthread
|
||||
|
||||
LIBFT_D = ./libft
|
||||
LIBFT = $(LIBFT_D)/libft.a
|
||||
LIBS = -lpthread
|
||||
|
||||
SRCS = main.c \
|
||||
init.c \
|
||||
main_thread.c \
|
||||
pere_fouettard.c \
|
||||
exec.c \
|
||||
utils.c \
|
||||
generic.c
|
||||
|
||||
DIR_OBJS = builds
|
||||
@@ -34,13 +31,7 @@ OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o)
|
||||
# ------ RULES -------
|
||||
# --------------------
|
||||
|
||||
all: subsystem $(NAME)
|
||||
|
||||
subsystem:
|
||||
@cd $(LIBFT_D) && $(MAKE)
|
||||
|
||||
$(LIBFT): # dispensable. utile seulement pour un appel direct à $(NAME), si $(LIBFT) n'existe pas
|
||||
cd $(LIBFT_D) && $(MAKE)
|
||||
all: $(NAME)
|
||||
|
||||
$(DIR_OBJS)/%.o: %.c | $(DIR_OBJS)
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
@@ -50,7 +41,7 @@ $(DIR_OBJS):
|
||||
|
||||
$(OBJS): $(HEADERS:%=$(HEADERS_D)/%)
|
||||
|
||||
$(NAME): $(OBJS) $(LIBFT)
|
||||
$(NAME): $(OBJS)
|
||||
$(CC) $(OBJS) -o $(NAME) $(LIBS)
|
||||
|
||||
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
|
||||
# define PHILO_H
|
||||
|
||||
# include "libft.h"
|
||||
# include <stdio.h>
|
||||
# include <stdlib.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
|
||||
# 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
|
||||
# define PHILO_PROTO_H
|
||||
|
||||
// init.c
|
||||
t_philo *init(int ac, char **av, pthread_t **id);
|
||||
|
||||
// main_thread.c
|
||||
int is_dead(t_philo *philo);
|
||||
// pere_fouettard.c
|
||||
int is_dead(t_philo *philo);
|
||||
void launch_threads(t_philo *philo, pthread_t *id);
|
||||
|
||||
// exec.c
|
||||
void *philo_exec(void *arg);
|
||||
|
||||
// generic.c
|
||||
// utils.c
|
||||
void init_time(t_philo *philo);
|
||||
void update_time(t_philo *philo);
|
||||
int diff_time(t_time old, struct timeval new);
|
||||
int print_message(t_philo *philo, char *clr, char *msg);
|
||||
int diff_time(t_time old, struct timeval new);
|
||||
int print_message(t_philo *philo, char *clr, char *msg);
|
||||
|
||||
// generic.c
|
||||
int ft_atoi(const char *str);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -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
|
||||
# define PHILO_STRUCT_H
|
||||
|
||||
typedef pthread_mutex_t t_mtx;
|
||||
typedef pthread_mutex_t t_mtx;
|
||||
|
||||
typedef struct s_time
|
||||
{
|
||||
@@ -11,11 +23,11 @@ typedef struct s_time
|
||||
|
||||
typedef struct s_params
|
||||
{
|
||||
int n_phi; // number_of_philosophers
|
||||
int t_die; // time_to_die
|
||||
int t_eat; // time_to_eat
|
||||
int t_slp; // time_to_sleep
|
||||
int n_eat; // [number_of_times_each_philosopher_must_eat]
|
||||
int n_phi;
|
||||
int t_die;
|
||||
int t_eat;
|
||||
int t_slp;
|
||||
int n_eat;
|
||||
} t_params;
|
||||
|
||||
typedef struct s_global
|
||||
|
||||
1
libft
1
libft
Submodule libft deleted from af88c1ce23
14
srcs/exec.c
14
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"
|
||||
|
||||
void action_delay(t_philo *philo, int action_time)
|
||||
@@ -45,7 +57,7 @@ void *philo_exec(void *arg)
|
||||
{
|
||||
t_philo *philo;
|
||||
|
||||
philo = (t_philo*)arg;
|
||||
philo = (t_philo *)arg;
|
||||
init_time(philo);
|
||||
if (philo->p_nbr % 2 == 0)
|
||||
usleep(10 * 1000);
|
||||
|
||||
@@ -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"
|
||||
|
||||
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);
|
||||
philo->global->t_start.ts = stime.tv_sec;
|
||||
philo->global->t_start.tu = stime.tv_usec;
|
||||
is_negative = 1;
|
||||
i++;
|
||||
}
|
||||
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)
|
||||
while (str[i] >= '0' && str[i] <= '9')
|
||||
{
|
||||
pthread_mutex_unlock(&(philo->global->m_print));
|
||||
return (1);
|
||||
result = (result * 10) + (str[i] - '0');
|
||||
i++;
|
||||
}
|
||||
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);
|
||||
if (is_negative)
|
||||
result = result * -1;
|
||||
return (result);
|
||||
}
|
||||
|
||||
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"
|
||||
|
||||
t_philo *lst_add_philo(t_params *params, t_global *global, int i)
|
||||
|
||||
18
srcs/main.c
18
srcs/main.c
@@ -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"
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
pthread_t *id;
|
||||
t_philo *philo;
|
||||
int i;
|
||||
int i;
|
||||
|
||||
philo = init(ac, av, &id);
|
||||
if (philo == NULL)
|
||||
return (0);
|
||||
return (0);
|
||||
launch_threads(philo, id);
|
||||
i = 0;
|
||||
while (i < philo->params->n_phi)
|
||||
@@ -16,5 +28,5 @@ int main(int ac, char **av)
|
||||
pthread_join(id[i], NULL);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
int is_dead(t_philo *philo)
|
||||
@@ -17,10 +29,9 @@ int is_dead(t_philo *philo)
|
||||
{
|
||||
pthread_mutex_lock(&(philo->global->m_print));
|
||||
philo->global->dead = 1;
|
||||
|
||||
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 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));
|
||||
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