organize
This commit is contained in:
parent
5297fe7830
commit
23dfef3dba
@ -16,7 +16,7 @@ SRC_PATH = src
|
|||||||
INC_PATH = inc
|
INC_PATH = inc
|
||||||
OBJ_PATH = obj
|
OBJ_PATH = obj
|
||||||
|
|
||||||
VPATH = src:src/utils
|
VPATH = src:src/utils:src/routines:src/init:src/cleanup
|
||||||
SOURCES = get_time.c ph_atoi.c print_rules.c print_status.c \
|
SOURCES = get_time.c ph_atoi.c print_rules.c print_status.c \
|
||||||
philo_routine.c philo_sleep.c set_death.c \
|
philo_routine.c philo_sleep.c set_death.c \
|
||||||
synchronize_philos.c take_forks.c think_time.c \
|
synchronize_philos.c take_forks.c think_time.c \
|
||||||
@ -34,8 +34,7 @@ CC = cc
|
|||||||
RM = rm -rf
|
RM = rm -rf
|
||||||
|
|
||||||
INCLUDES = -I./$(INC_PATH)
|
INCLUDES = -I./$(INC_PATH)
|
||||||
CFLAGS = -Wall -Wextra -Werror -MMD -g
|
override CFLAGS := -Wall -Wextra -Werror -MMD $(CFLAGS)
|
||||||
UNAME_S := $(shell uname -s)
|
|
||||||
ifeq ($(UNAME_S),Linux)
|
ifeq ($(UNAME_S),Linux)
|
||||||
LDLIBS := -pthread
|
LDLIBS := -pthread
|
||||||
endif
|
endif
|
||||||
|
|||||||
@ -67,33 +67,35 @@ typedef struct s_philo
|
|||||||
// pthread_mutex_destroy, pthread_mutex_lock,
|
// pthread_mutex_destroy, pthread_mutex_lock,
|
||||||
// pthread_mutex_unlock
|
// pthread_mutex_unlock
|
||||||
|
|
||||||
int ph_atoi(const char *nptr, int *res);
|
int check_philos(t_rules *rules);
|
||||||
int get_time(void);
|
|
||||||
void free_philos(t_rules *rules);
|
|
||||||
int destroy_mutexes(t_rules *rules);
|
|
||||||
int create_philos(t_rules *rules);
|
|
||||||
int parse_arguments(int argc, char *argv[], t_rules *rules);
|
|
||||||
int create_mutexes(t_rules *rules);
|
int create_mutexes(t_rules *rules);
|
||||||
|
int create_philo_mutexes(t_philo *philo);
|
||||||
|
int create_philos(t_rules *rules);
|
||||||
int create_threads(t_rules *rules);
|
int create_threads(t_rules *rules);
|
||||||
void print_status(t_philo *philo, char *status);
|
int destroy_mutexes(t_rules *rules);
|
||||||
void *philo_routine(void *philo);
|
int ft_min(int a, int b);
|
||||||
|
void free_philos(t_rules *rules);
|
||||||
|
int get_finished(t_rules *rules);
|
||||||
|
int get_death(t_philo *philo);
|
||||||
|
int get_time(void);
|
||||||
int join_threads(t_rules *rules);
|
int join_threads(t_rules *rules);
|
||||||
void print_rules(t_rules *rules);
|
void *monitor_routine(void *arg);
|
||||||
int check_death(t_philo *philo);
|
void *one_philo(t_philo *philo);
|
||||||
|
int parse_arguments(int argc, char *argv[], t_rules *rules);
|
||||||
|
int ph_atoi(const char *nptr, int *res);
|
||||||
void philo_die(t_philo *philo);
|
void philo_die(t_philo *philo);
|
||||||
int philo_eat(t_philo *philo);
|
int philo_eat(t_philo *philo);
|
||||||
void *monitor_routine(void *arg);
|
void *philo_routine(void *philo);
|
||||||
int check_philos(t_rules *rules);
|
|
||||||
int ft_min(int a, int b);
|
|
||||||
void set_finished(t_rules *rules);
|
|
||||||
int get_finished(t_rules *rules);
|
|
||||||
void synchronize_philos(t_philo *philo);
|
|
||||||
int philo_sleep(t_philo *philo, int time);
|
int philo_sleep(t_philo *philo, int time);
|
||||||
void unlock_forks(t_philo *philo);
|
void print_rules(t_rules *rules);
|
||||||
|
void print_status(t_philo *philo, char *status);
|
||||||
|
void print_usage(void);
|
||||||
|
void set_finished(t_rules *rules);
|
||||||
|
void set_death(t_philo *philo);
|
||||||
|
void synchronize_philos(t_philo *philo);
|
||||||
void take_forks(t_philo *philo);
|
void take_forks(t_philo *philo);
|
||||||
int think_time(t_rules *rules);
|
int think_time(t_rules *rules);
|
||||||
int create_philo_mutexes(t_philo *philo);
|
void unlock_forks(t_philo *philo);
|
||||||
void one_philo(t_philo *philo);
|
|
||||||
void print_usage(void);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -12,10 +12,11 @@
|
|||||||
|
|
||||||
#include "philo.h"
|
#include "philo.h"
|
||||||
|
|
||||||
void one_philo(t_philo *philo)
|
void *one_philo(t_philo *philo)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(philo->l_fork);
|
pthread_mutex_lock(philo->l_fork);
|
||||||
print_status(philo, "has taken a fork");
|
print_status(philo, "has taken a fork");
|
||||||
pthread_mutex_unlock(philo->l_fork);
|
pthread_mutex_unlock(philo->l_fork);
|
||||||
philo_sleep(philo, philo->rules->time_to_die);
|
philo_sleep(philo, philo->rules->time_to_die);
|
||||||
|
return (NULL);
|
||||||
}
|
}
|
||||||
@ -15,8 +15,6 @@
|
|||||||
void philo_die(t_philo *philo)
|
void philo_die(t_philo *philo)
|
||||||
{
|
{
|
||||||
print_status(philo, "died");
|
print_status(philo, "died");
|
||||||
pthread_mutex_lock(philo->death_lock);
|
set_death(philo);
|
||||||
philo->death = 1;
|
|
||||||
pthread_mutex_unlock(philo->death_lock);
|
|
||||||
set_finished(philo->rules);
|
set_finished(philo->rules);
|
||||||
}
|
}
|
||||||
@ -19,19 +19,16 @@ void *philo_routine(void *arg)
|
|||||||
philo = (t_philo *)arg;
|
philo = (t_philo *)arg;
|
||||||
synchronize_philos(philo);
|
synchronize_philos(philo);
|
||||||
if (philo->rules->n_philos == 1)
|
if (philo->rules->n_philos == 1)
|
||||||
{
|
return (one_philo(philo));
|
||||||
one_philo(philo);
|
while (!get_death(philo) && !get_finished(philo->rules))
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
while (!philo->death && !get_finished(philo->rules))
|
|
||||||
{
|
{
|
||||||
if (!philo_eat(philo))
|
if (!philo_eat(philo))
|
||||||
break ;
|
break ;
|
||||||
if (!philo->death && !get_finished(philo->rules))
|
if (!get_death(philo) && !get_finished(philo->rules))
|
||||||
print_status(philo, "is sleeping");
|
print_status(philo, "is sleeping");
|
||||||
if (!philo_sleep(philo, philo->rules->time_to_sleep))
|
if (!philo_sleep(philo, philo->rules->time_to_sleep))
|
||||||
break ;
|
break ;
|
||||||
if (!philo->death && !get_finished(philo->rules))
|
if (!get_death(philo) && !get_finished(philo->rules))
|
||||||
print_status(philo, "is thinking");
|
print_status(philo, "is thinking");
|
||||||
if (!philo_sleep(philo, think_time(philo->rules)))
|
if (!philo_sleep(philo, think_time(philo->rules)))
|
||||||
break ;
|
break ;
|
||||||
Loading…
Reference in New Issue
Block a user