Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from malatinipro/refacotr
Browse files Browse the repository at this point in the history
refactor: wip
  • Loading branch information
mahautlatinis authored Oct 13, 2023
2 parents 7d7d0f2 + dd7cded commit be90bd8
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 192 deletions.
18 changes: 0 additions & 18 deletions .github/workflows/ubuntu-compilation.yml

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*/*.o
*/*philo
51 changes: 29 additions & 22 deletions philo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,53 @@
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: malatini <[email protected]> +#+ +:+ +#+ #
# By: mahautlatinis <[email protected] +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2021/06/29 14:14:41 by malatini #+# #+# #
# Updated: 2021/07/23 18:10:31 by malatini ### ########.fr #
# Updated: 2023/10/13 18:50:43 by mahautlatin ### ########.fr #
# #
# **************************************************************************** #

NAME = philo
CC = clang
OBJDIR = objects
SRC = philosophers.c atoi.c print.c routine.c check_args.c timer.c init.c
OBJ = $(addprefix $(OBJDIR)/, $(SRC:.c=.o))

SRC = ./srcs/main.c \
./srcs/philosophers.c \
./srcs/atoi.c \
./srcs/print.c \
./srcs/routine.c \
./srcs/check_args.c \
./srcs/timer.c \
./srcs/init.c

OBJ = $(SRC:.c=.o)
CFLAGS = -Wall -Wextra -Werror -pthread

ifneq (,$(findstring xterm,${TERM}))
GREEN := $(shell tput -Txterm setaf 2)
RESET := $(shell tput -Txterm sgr0)
GREEN := $(shell tput -Txterm setaf 2)
RESET := $(shell tput -Txterm sgr0)
else
GREEN := ""
RESET := ""
GREEN := ""
RESET := ""
endif

all: $(NAME)
.c.o:
$(CC) $(CFLAGS) -c $< -o ${<:.c=.o} -I./include/

bonus: all
$(NAME): $(OBJ)
$(CC) $(CFLAGS) -o $(NAME) $(OBJ) -I./include/ -L./libftprintf/ -lftprintf

$(NAME): $(OBJ)
@$(CC) $(CFLAGS) $(OBJ) -o $(NAME)
@echo "${GREEN}Compilation Done${RESET}"

$(OBJDIR)/%.o: %.c
@mkdir -p objects
@$(CC) $(CFLAGS) -c $< -o $@
all: $(NAME)

clean:
@rm -f $(OBJ)
@rm -rf $(OBJ)

fclean: clean
@rm -rf $(NAME)

fclean: clean
@rm -f $(NAME)
re: fclean all

re: fclean all
bonus: all

.PHONY: clean fclean all re bonus
.PHONY: clean fclean all re bonus
30 changes: 30 additions & 0 deletions philo/include/ft_printf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mahautlatinis <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/13 18:18:59 by mahautlatin #+# #+# */
/* Updated: 2023/10/13 18:52:25 by mahautlatin ### ########.fr */
/* */
/* ************************************************************************** */

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mahautlatinis <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/03 21:34:08 by malatini #+# #+# */
/* Updated: 2023/10/07 18:35:50 by mahautlatin ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef FT_PRINTF_H
# define FT_PRINTF_H

int ft_printf(const char *format, ...);

#endif
72 changes: 36 additions & 36 deletions philo/philosophers.h → philo/include/philosophers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@
/* By: mahautlatinis <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/06/29 17:37:13 by malatini #+# #+# */
/* Updated: 2023/10/09 21:25:28 by mahautlatin ### ########.fr */
/* Updated: 2023/10/13 18:34:08 by mahautlatin ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef PHILOSOPHERS_H
# define PHILOSOPHERS_H

# include <pthread.h>
# include <stdbool.h>
# include <stdio.h>
# include <unistd.h>
# include <stdlib.h>
# include <string.h>
# include <sys/time.h>
# include <pthread.h>
# include <stdbool.h>
# include <sys/errno.h>
# define ERROR -1
# define SUCCESS 1
# include <sys/time.h>
# include <unistd.h>

# define ERROR -1
# define SUCCESS 1

typedef struct s_philo
{
Expand Down Expand Up @@ -55,36 +56,35 @@ typedef struct s_mem

typedef struct s_atoi_var
{
long i;
long max;
long sign;
long result;
long i;
long max;
long sign;
long result;
} t_atoi_var;

int ft_isdigit(int c);
long ft_atoi(char *str);
void print_args(t_mem *mem);
int save_args(int argc, char **argv, t_mem *mem);
void *give_actions(void *arg);
void init_time(t_mem *mem);
void take_time_doing(t_mem *mem, long unsigned int t);
void init_mem(t_mem *mem);
long unsigned int get_time(t_mem *mem);
void manage_only_one_philo(t_mem *mem);
int init_philo_thread(t_mem *mem);
int launch_threads(t_mem *mem);
int check_args(t_mem *mem);
int init_philos(t_mem *mem);
int check_death(t_mem *mem, int *stop);
int init_all_mutex(t_mem *mem);
void print_message_safe(t_philo *philo, t_mem *mem,
char *str, int *stop);
void print_args(t_mem *mem);
int init_args(int argc, char **argv, t_mem *mem);
int manage_threads(t_mem *mem);
int ft_isalpha(int c);
int save_args_bis(int ac, char **av, t_mem *m, int *i);
int check_stop(t_mem *mem, int *stop);
void manage_only_one_philo(t_mem *mem);
int check_args(t_mem *mem);
int check_death(t_mem *mem, int *stop);
int check_stop(t_mem *mem, int *stop);
int ft_isalpha(int c);
int ft_isdigit(int c);
int init_all_mutex(t_mem *mem);
int init_args(int argc, char **argv, t_mem *mem);
int init_philo_thread(t_mem *mem);
int init_philos(t_mem *mem);
int launch_threads(t_mem *mem);
int manage_threads(t_mem *mem);
int save_args_bis(int ac, char **av, t_mem *m, int *i);
int save_args(int argc, char **argv, t_mem *mem);
long ft_atoi(char *str);
long unsigned int get_time(t_mem *mem);
void *give_actions(void *arg);
void init_mem(t_mem *mem);
void init_time(t_mem *mem);
void manage_only_one_philo(t_mem *mem);
void manage_only_one_philo(t_mem *mem);
void print_args(t_mem *mem);
void print_message_safe(t_philo *philo,
t_mem *mem, char *str, int *stop);
void take_time_doing(t_mem *mem, long unsigned int t);

#endif
Binary file added philo/libftprintf/libftprintf.a
Binary file not shown.
8 changes: 4 additions & 4 deletions philo/atoi.c → philo/srcs/atoi.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils0.c :+: :+: :+: */
/* atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: malatini <[email protected]> +#+ +:+ +#+ */
/* By: mahautlatinis <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/06/29 17:54:50 by malatini #+# #+# */
/* Updated: 2021/07/21 09:49:41 by malatini ### ########.fr */
/* Updated: 2023/10/13 18:50:24 by mahautlatin ### ########.fr */
/* */
/* ************************************************************************** */

#include "./philosophers.h"
#include <philosophers.h>

int ft_isdigit(int c)
{
Expand Down
14 changes: 3 additions & 11 deletions philo/check_args.c → philo/srcs/check_args.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
/* ::: :::::::: */
/* check_args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: malatini <[email protected]> +#+ +:+ +#+ */
/* By: mahautlatinis <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/06/29 18:22:27 by malatini #+# #+# */
/* Updated: 2021/07/21 09:50:20 by malatini ### ########.fr */
/* Updated: 2023/10/13 18:51:01 by mahautlatin ### ########.fr */
/* */
/* ************************************************************************** */

#include "./philosophers.h"
#include <philosophers.h>

int save_args_bis(int argc, char **argv, t_mem *mem, int *i)
{
Expand Down Expand Up @@ -40,10 +40,6 @@ int save_args_bis(int argc, char **argv, t_mem *mem, int *i)
return (1);
}

/*
** Permet d'enregistrer les arguments du main pour les utiliser durant
** le programme.
*/
int save_args(int argc, char **argv, t_mem *mem)
{
int i;
Expand Down Expand Up @@ -72,10 +68,6 @@ int save_args(int argc, char **argv, t_mem *mem)
return (ret);
}

/*
** Permet de verifier que les arguments du main sont correctes.
** Trop de philos peut faire planter l'ordinateur.
*/
int check_args(t_mem *mem)
{
if (mem->number_of_philosophers > 200)
Expand Down
21 changes: 4 additions & 17 deletions philo/init.c → philo/srcs/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@
/* ::: :::::::: */
/* init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: malatini <[email protected]> +#+ +:+ +#+ */
/* By: mahautlatinis <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/20 09:22:14 by malatini #+# #+# */
/* Updated: 2021/07/20 14:30:56 by malatini ### ########.fr */
/* Updated: 2023/10/13 18:51:05 by mahautlatin ### ########.fr */
/* */
/* ************************************************************************** */

#include "./philosophers.h"
#include <philosophers.h>

/*
** Initialise la structure principale et verifie les arguments.
** Gere le cas particulier de 1 seul philo.
*/
int init_args(int argc, char **argv, t_mem *mem)
{
if (argc < 5 || argc > 6)
Expand All @@ -33,9 +29,6 @@ int init_args(int argc, char **argv, t_mem *mem)
return (1);
}

/*
** Initialise le tableau de philos pour ne pas avoir de garbage value.
*/
int init_philos(t_mem *mem)
{
int i;
Expand All @@ -52,9 +45,6 @@ int init_philos(t_mem *mem)
return (SUCCESS);
}

/*
** Initialise la structure principale pour eviter des garbages values.
*/
void init_mem(t_mem *mem)
{
mem->number_of_philosophers = 0;
Expand All @@ -65,12 +55,9 @@ void init_mem(t_mem *mem)
mem->stop = false;
init_time(mem);
mem->stop = false;
return ;
}

/*
** Initialise les mutex, indique les fourchettes droites et gauches pour
** les philos.
*/
int init_all_mutex(t_mem *mem)
{
int i;
Expand Down
33 changes: 33 additions & 0 deletions philo/srcs/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mahautlatinis <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/13 18:37:17 by mahautlatin #+# #+# */
/* Updated: 2023/10/13 18:54:47 by mahautlatin ### ########.fr */
/* */
/* ************************************************************************** */

#include <philosophers.h>

int main(int argc, char **argv)
{
t_mem mem;
pthread_mutex_t m_stop;
pthread_mutex_t m_write;

if (pthread_mutex_init(&m_stop, NULL))
return (errno);
mem.stop_protect = m_stop;
if (pthread_mutex_init(&m_write, NULL))
return (errno);
mem.write_protect = m_write;
if (init_args(argc, argv, &mem) == 0)
return (0);
if (init_all_mutex(&mem) != 0)
return (errno);
init_philos(&mem);
return (manage_threads(&mem));
}
Loading

0 comments on commit be90bd8

Please sign in to comment.