commit 11ad2d874154a00a4b6c91c4cc35f880067d2f5c Author: Adam Joly Date: Mon Jul 8 10:33:28 2024 +0200 「🎉」 init: hello world ! diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ad4fb1b --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +philo/obj/ +philo/philosphers +philo/.direnv +philo/.envrc +philo/flake.nix +philo/compile_commands.json +philo/.cache +philo/vgcore.* + diff --git a/philo/Makefile b/philo/Makefile new file mode 100644 index 0000000..83b5855 --- /dev/null +++ b/philo/Makefile @@ -0,0 +1,60 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: adjoly +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/07/07 15:08:52 by adjoly #+# #+# # +# Updated: 2024/07/07 15:24:47 by adjoly ### ########.fr # +# # +# **************************************************************************** # + +SHELL = bash + +NAME = philo + +CC = cc + +OBJSDIR = obj/ + +SRC = $(shell find src -name '*.c') + +I_DIR = include/ + +INCLUDE = -I $(I_DIR) + +OBJS = $(addprefix $(OBJSDIR), $(SRC:.c=.o)) + +FLAGS = -Werror -Wall -Wextra -g + +# -------------------------------- + +all: $(NAME) + +$(LIB): + @make -sj$(nproc) -C $(LIBFT_DIR) + +$(NAME): $(LIB) $(OBJS) + @printf "\x1B[2K\r \x1B[1;32m[ 󱌣 ]\x1B[0m objects compiled." + @printf "\n \x1B[1;33m[  ]\x1B[0m compiling $(NAME)..." + @$(CC) $(FLAGS) $(OBJS) $(LIB) -o $(NAME) -lpthread + @printf "\x1B[2K\r \x1B[1;33m[  ]\x1B[0m $(NAME) compiled.\n" + +$(OBJSDIR)%.o: %.c + @mkdir -p $(@D) + @$(CC) $(INCLUDE) $(FLAGS) $< -c -o $@ + @printf "\x1B[2K\r \x1B[1;32m[ 󱌣 ]\x1B[0m compiling objects... : $<" + +clean: + @rm -f $(OBJS) + @printf " \x1B[1;31m[  ]\x1B[0m deleted $(NAME).\n" + +fclean: clean + @rm -f $(NAME) + @rm -Rf $(OBJSDIR) + @printf " \x1B[1;31m[  ]\x1B[0m deleted objects.\n" + +re: fclean all + +.PHONY: clean all re fclean diff --git a/philo/include/philo.h b/philo/include/philo.h new file mode 100644 index 0000000..4b884b7 --- /dev/null +++ b/philo/include/philo.h @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* philo.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/07 15:11:02 by adjoly #+# #+# */ +/* Updated: 2024/07/07 16:18:23 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PHILO_H +# define PHILO_H + +# include +# include +# include + +typedef enum s_philo_state +{ + DIED = -1, + EAT, + THINK, + SLEEP, + FORK_TAKEN +} t_philo_state; + +void log_philo(uint32_t timestamp, uint16_t philo, t_philo_state type); + +#endif diff --git a/philo/include/philo_msg.h b/philo/include/philo_msg.h new file mode 100644 index 0000000..36dee37 --- /dev/null +++ b/philo/include/philo_msg.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* philo_msg.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/07 17:50:46 by adjoly #+# #+# */ +/* Updated: 2024/07/07 17:53:53 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PHILO_MSG_H +# define PHILO_MSG_H + +# define EATING_MSG "is eating\n" +# define THINK_MSG "is thinking\n" +# define SLEEP_MSG "is sleeping\n" +# define DIED_MSG "died\n" +# define FORK_MSG "has taken a fork\n" + +#endif diff --git a/philo/philo b/philo/philo new file mode 100755 index 0000000..6c0685f Binary files /dev/null and b/philo/philo differ diff --git a/philo/src/log.c b/philo/src/log.c new file mode 100644 index 0000000..869da9b --- /dev/null +++ b/philo/src/log.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* log.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/07 16:12:20 by adjoly #+# #+# */ +/* Updated: 2024/07/07 17:56:28 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo.h" +#include "philo_msg.h" + +void log_philo(uint32_t timestamp, uint16_t philo, t_philo_state type) +{ + if (type == EAT) + printf("%u %hu %s", timestamp, philo, EATING_MSG); + else if (type == THINK) + printf("%u %hu %s", timestamp, philo, THINK_MSG); + else if (type == SLEEP) + printf("%u %hu %s", timestamp, philo, SLEEP_MSG); + else if (type == DIED) + printf("%u %hu %s", timestamp, philo, DIED_MSG); + else if (type == FORK_TAKEN) + printf("%u %hu %s", timestamp, philo, FORK_MSG); +} diff --git a/philo/src/philo.c b/philo/src/philo.c new file mode 100644 index 0000000..2640641 --- /dev/null +++ b/philo/src/philo.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* philo.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/07 15:10:29 by adjoly #+# #+# */ +/* Updated: 2024/07/07 17:44:08 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo.h" + +int main(int ac, char **av) +{ + (void)ac; + (void)av; + log_philo(10, 5, DIED); +}