create project with libft and basic thread program
This commit is contained in:
14
.gitignore
vendored
Normal file
14
.gitignore
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
*.o
|
||||
*.swp
|
||||
*.out
|
||||
*.exe
|
||||
*.stackdump
|
||||
*.a
|
||||
*.so
|
||||
*.dSYM
|
||||
.vscode
|
||||
*.lnk
|
||||
*.zip
|
||||
philo
|
||||
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
[submodule "libft"]
|
||||
path = libft
|
||||
url = git@bitbucket.org:LuckyLaszlo/libft.git
|
||||
64
Makefile
Normal file
64
Makefile
Normal file
@@ -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
|
||||
|
||||
46
README.md
Normal file
46
README.md
Normal file
@@ -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
|
||||
|
||||
16
headers/philo.h
Normal file
16
headers/philo.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef PHILO_H
|
||||
# define PHILO_H
|
||||
|
||||
# include "libft.h"
|
||||
# include <stdio.h>
|
||||
# include <stdlib.h>
|
||||
# include <string.h>
|
||||
# include <unistd.h>
|
||||
# include <pthread.h>
|
||||
|
||||
typedef struct s_philo
|
||||
{
|
||||
char *str;
|
||||
} t_philo;
|
||||
|
||||
#endif
|
||||
1
libft
Submodule
1
libft
Submodule
Submodule libft added at af88c1ce23
27
srcs/main.c
Normal file
27
srcs/main.c
Normal file
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user