1
0

🎉」 init: hello world !

This commit is contained in:
2024-07-08 10:33:28 +02:00
commit 11ad2d8741
7 changed files with 170 additions and 0 deletions

9
.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
philo/obj/
philo/philosphers
philo/.direnv
philo/.envrc
philo/flake.nix
philo/compile_commands.json
philo/.cache
philo/vgcore.*

60
philo/Makefile Normal file
View File

@ -0,0 +1,60 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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

31
philo/include/philo.h Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdio.h>
# include <pthread.h>
# include <stdint.h>
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

22
philo/include/philo_msg.h Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo_msg.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

BIN
philo/philo Executable file

Binary file not shown.

28
philo/src/log.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* log.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

20
philo/src/philo.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}