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