commit fc987249ac679c0dae4ca7fefc334664d03689a0 Author: hugogogo Date: Sat Dec 11 12:46:06 2021 +0100 create project with libft and basic thread program diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eaadb6e --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +.DS_Store +Thumbs.db +*.o +*.swp +*.out +*.exe +*.stackdump +*.a +*.so +*.dSYM +.vscode +*.lnk +*.zip +philo diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..cb2e6ae --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libft"] + path = libft + url = git@bitbucket.org:LuckyLaszlo/libft.git diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b970f35 --- /dev/null +++ b/Makefile @@ -0,0 +1,64 @@ +NAME = philo + +CC = clang + +CFLAGS = -Wall -Wextra $(INCLUDES) -g # add -Werror, del -g + +VPATH = $(DIR_SRCS) +DIR_SRCS = srcs + +INCLUDES = -I$(HEADERS_D) -I$(LIBFT_D) + +HEADERS_D = ./headers +HEADERS = philo.h + +LIBS = -L $(LIBFT_D) -lft \ + -lpthread + +LIBFT_D = ./libft +LIBFT = $(LIBFT_D)/libft.a + +SRCS = main.c + +DIR_OBJS = builds +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) + +$(DIR_OBJS)/%.o: %.c | $(DIR_OBJS) + $(CC) $(CFLAGS) -c $< -o $@ + +$(DIR_OBJS): + mkdir $@ + +$(OBJS): $(HEADERS:%=$(HEADERS_D)/%) + +$(NAME): $(OBJS) $(LIBFT) + $(CC) $(OBJS) -o $(NAME) $(LIBS) + +clean: + rm -f $(OBJS) + +fclean: clean + rm -f $(NAME) + +re: fclean all + +valgrind: $(NAME) + valgrind --leak-check=full --leak-resolution=low --show-reachable=yes ./$(NAME) + +run: $(NAME) + ./$(NAME) + +.PHONY : all clean fclean re bonus subsystem run valgrind + diff --git a/README.md b/README.md new file mode 100644 index 0000000..334ef4c --- /dev/null +++ b/README.md @@ -0,0 +1,46 @@ +options : +``` +- n_phi -> number_of_philosophers +- t_die -> time_to_die +- t_eat -> time_to_eat +- t_slp -> time_to_sleep +- n_est -> [number_of_times_each_philosopher_must_eat] +``` + +thread : +- thread are light-weight-process (LWP) that happens inside a process +- a process can be single-threaded or multi-threaded +- different process have a unique PID with a unique LWP, different thread in the same process have the same PID and a different LWP +- thread vs process : + process : + - process is isolated, it doesn't share memory with any other process + - process can have states : new, ready, running, waiting, terminated, suspended + - process is less efficient in communication, it takes more times to create or terminate or switch + - process speed is not impacted by speed of other process (untill it reach the limit of cpu) + - process are created with fork() and execve() (to duplicate a process in a child process, or replacing an existing process) + thread : + - thread are not isolated + - thread can have states : running, ready, blocked + - thread is more efficient in communication, it takes less times to create or terminate or switch + - thread can become slow if the process does many concurrent tasks +- ressources : + - https://www.baeldung.com/linux/process-vs-thread + + +external function : + +memset : fill memory with a constant byte +printf : format and print data +malloc : allocate dynamic memory +free : free dynamic memory +write : write to a file descriptor +usleep : suspend execution for microseconds intervals +gettimeofday : get time +pthread_create : create a new thread +pthread_detach : +pthread_join +pthread_mutex_init +pthread_mutex_destroy +pthread_mutex_lock +pthread_mutex_unlock + diff --git a/headers/philo.h b/headers/philo.h new file mode 100644 index 0000000..e052c10 --- /dev/null +++ b/headers/philo.h @@ -0,0 +1,16 @@ +#ifndef PHILO_H +# define PHILO_H + +# include "libft.h" +# include +# include +# include +# include +# include + +typedef struct s_philo +{ + char *str; +} t_philo; + +#endif diff --git a/libft b/libft new file mode 160000 index 0000000..af88c1c --- /dev/null +++ b/libft @@ -0,0 +1 @@ +Subproject commit af88c1ce23f0daa453c70cee0c61a30f71c2a622 diff --git a/srcs/main.c b/srcs/main.c new file mode 100644 index 0000000..f5fc7e1 --- /dev/null +++ b/srcs/main.c @@ -0,0 +1,27 @@ +#include "philo.h" + +void *philo_exec(void *arg) +{ + t_philo *philo; + + philo = (t_philo*)arg; + while (1) + write(1, philo->str, ft_strlen(philo->str)); +} + +int main(void) +{ + pthread_t id; + int ret; + t_philo *philo; + + philo = malloc(sizeof(philo)); + philo->str = "i'm philosopher\n"; + ret = pthread_create(&id, NULL, &philo_exec, philo); + if (ret == 0) + { + while (1) + write(1, "main function\n", 14); + } + return 0; +}