From 796e186c3690df0e32f0fc7ddfd69d1f6d0ae5fc Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Thu, 27 Apr 2023 12:05:14 +0000 Subject: [PATCH] clean: norm --- Makefile | 2 + data.c | 36 ++++++++------ data.h | 13 +++++ main.c | 59 +++++++++++++++-------- parsing.c | 12 +++++ parsing.h | 17 ++++++- philo.c | 125 +++++------------------------------------------- philo_routine.c | 118 +++++++++++++++++++++++++++++++++++++++++++++ philos.c | 12 +++++ philos.h | 16 +++++++ print.c | 29 +++++------ print.h | 16 +++++++ print2.c | 32 +++++++++++++ threads.c | 15 +++++- threads.h | 15 ++++++ time.c | 27 ++++++++++- time.h | 19 ++++++++ utils/ft_atoi.c | 4 +- utils/putchar.c | 12 +++++ utils/putnum.c | 12 +++++ utils/putstr.c | 12 +++++ 21 files changed, 434 insertions(+), 169 deletions(-) create mode 100644 philo_routine.c create mode 100644 print2.c diff --git a/Makefile b/Makefile index 66b0a18..29a7523 100644 --- a/Makefile +++ b/Makefile @@ -7,8 +7,10 @@ SRCS = parsing.c \ main.c \ time.c \ print.c \ + print2.c \ data.c \ philo.c \ + philo_routine.c \ philos.c \ threads.c diff --git a/data.c b/data.c index 6b1de35..164c3f4 100644 --- a/data.c +++ b/data.c @@ -1,7 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* data.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet #include #include #include +#include #include #include "./data.h" #include "philo.h" @@ -10,7 +23,7 @@ bool data_init(t_data *data) { - size_t i; + ssize_t i; data->stop = 0; data->forks = malloc(sizeof(pthread_mutex_t) * data->nb_philos); @@ -19,10 +32,9 @@ bool data_init(t_data *data) memset(data->forks, 1, data->nb_philos); data->philos = malloc(sizeof(t_philo) * data->nb_philos); if (data->philos == NULL) - { free(data->forks); + if (data->philos == NULL) return (1); - } data->threads = malloc(sizeof(pthread_t) * data->nb_philos); if (data->forks == NULL) { @@ -30,12 +42,9 @@ bool data_init(t_data *data) free(data->forks); return (1); } - i = 0; - while (i < data->nb_philos) - { + i = -1; + while (++i < (ssize_t) data->nb_philos) pthread_mutex_init(&data->forks[i], NULL); - i++; - } pthread_mutex_init(&data->stop_mutex, NULL); pthread_mutex_init(&data->print_mutex, NULL); return (0); @@ -43,12 +52,12 @@ bool data_init(t_data *data) void data_destroyer(t_data *data) { - size_t i; + ssize_t i; t_philo *philo; bool stop; i = 0; - while (i < data->nb_philos) + while (i < (ssize_t) data->nb_philos) { philo = data->philos[i]; pthread_mutex_lock(&philo->stop_mutex); @@ -58,12 +67,9 @@ void data_destroyer(t_data *data) i++; usleep(1000); } - i = 0; - while (i < data->nb_philos) - { + i = -1; + while (++i < (ssize_t) data->nb_philos) pthread_mutex_destroy(&data->forks[i]); - i++; - } pthread_mutex_destroy(&data->stop_mutex); pthread_mutex_destroy(&data->print_mutex); free(data->threads); diff --git a/data.h b/data.h index 3648656..b255b08 100644 --- a/data.h +++ b/data.h @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* data.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet @@ -20,4 +32,5 @@ typedef struct s_data bool data_init(t_data *data); void data_destroyer(t_data *data); +size_t get_min_meal(t_data *data); #endif diff --git a/main.c b/main.c index d49a2f4..93d7b1c 100644 --- a/main.c +++ b/main.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet #include #include @@ -44,33 +56,42 @@ void stop(t_data *data) pthread_mutex_unlock(&data->stop_mutex); } -void *check_routine(t_data *data) +int routine(t_data *data) { size_t i; bool ok; t_philo *philo; + i = 0; + while (i < data->nb_philos) + { + philo = data->philos[i]; + pthread_mutex_lock(&philo->last_eat_mutex); + ok = !(philo->last_eat + data->life_expectency > get_time()); + pthread_mutex_unlock(&philo->last_eat_mutex); + if (ok == 1) + { + stop(data); + print_died(philo); + return (1); + } + if (get_min_meal(data) >= data->nb_meals) + { + stop(data); + return (1); + } + i++; + } + return (0); +} + +void *check_routine(t_data *data) +{ while (true) { - i = 0; - while (i < data->nb_philos) + if (routine(data)) { - philo = data->philos[i]; - pthread_mutex_lock(&philo->last_eat_mutex); - ok = !(philo->last_eat + data->life_expectency > get_time()); - pthread_mutex_unlock(&philo->last_eat_mutex); - if (ok == 1) - { - stop(data); - print_died(philo); - return (NULL); - } - if (get_min_meal(data) >= data->nb_meals) - { - stop(data); - return (NULL); - } - i++; + return (NULL); } } } diff --git a/parsing.c b/parsing.c index c6c7267..b04d069 100644 --- a/parsing.c +++ b/parsing.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet diff --git a/parsing.h b/parsing.h index b817f98..9f1cf72 100644 --- a/parsing.h +++ b/parsing.h @@ -1,4 +1,19 @@ -#include "./data.h" +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet #include #include @@ -15,119 +27,6 @@ void philo_destroyer(t_philo *philo) free(philo); } -bool check(t_philo *philo, t_data *data) -{ - bool stop; - - if (get_min_meal(data) == data->nb_meals) - { - pthread_mutex_lock(&philo->stop_mutex); - philo->stop = 1; - pthread_mutex_unlock(&philo->stop_mutex); - return (1); - } - pthread_mutex_lock(&data->stop_mutex); - stop = data->stop; - pthread_mutex_unlock(&data->stop_mutex); - if (stop) - { - pthread_mutex_lock(&philo->stop_mutex); - philo->stop = 1; - pthread_mutex_unlock(&philo->stop_mutex); - } - return (stop); -} - -bool philo_eat(t_philo *philo, t_data *data) -{ - size_t time; - - pthread_mutex_lock(&data->forks[philo->id]); - print_take_a_fork(philo); - if (check(philo, data)) - { - pthread_mutex_unlock(&data->forks[philo->id]); - return (1); - } - while ((philo->id + 1) % data->nb_philos == philo->id) - if (check(philo, data)) - { - pthread_mutex_unlock(&data->forks[philo->id]); - return (1); - } - pthread_mutex_lock(&data->forks[(philo->id + 1) % data->nb_philos]); - print_take_a_fork(philo); - if (check(philo, data)) - { - pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]); - pthread_mutex_unlock(&data->forks[philo->id]); - return (1); - } - print_eating(philo); - time = get_time(); - if (time - philo->last_eat + data->eat_time > data->life_expectency) - usleep((time - philo->last_eat + data->eat_time) * 1000); - else - usleep(data->eat_time * 1000); - pthread_mutex_unlock(&data->forks[philo->id]); - pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]); - if (check(philo, data)) - return (1); - pthread_mutex_lock(&philo->last_eat_mutex); - philo->last_eat = get_time(); - pthread_mutex_unlock(&philo->last_eat_mutex); - pthread_mutex_lock(&philo->nb_meal_mutex); - philo->nb_meal++; - pthread_mutex_unlock(&philo->nb_meal_mutex); - return (0); -} - -void philo_sleep(t_data *data, t_philo *philo) -{ - size_t time; - - print_sleeping(philo); - time = get_time(); - if (time - philo->last_eat + data->eat_time > data->life_expectency) - usleep((time - philo->last_eat + data->eat_time) * 1000); - else - usleep(data->eat_time * 1000); -} - - -void *philo_routine(void *arg) -{ - t_philo *philo; - t_data *data; - - philo = arg; - data = philo->data; - print_thinking(philo); - usleep((philo->id % 2) * (data->eat_time) * 1000); - while (true) - { - if (check(philo, data)) - { - return (NULL); - } - if (philo_eat(philo, data)) - { - return (NULL); - } - if (check(philo, data)) - { - return (NULL); - } - philo_sleep(data, philo); - if (check(philo, data)) - { - return (NULL); - } - print_thinking(philo); - } - return (NULL); -} - t_philo *philo_init(t_data *data) { t_philo *philo; diff --git a/philo_routine.c b/philo_routine.c new file mode 100644 index 0000000..3253507 --- /dev/null +++ b/philo_routine.c @@ -0,0 +1,118 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* philo_routine.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet +#include "./time.h" +#include "./philo.h" +#include "./print.h" +#include "data.h" + +bool check(t_philo *philo, t_data *data) +{ + bool stop; + + if (get_min_meal(data) == data->nb_meals) + { + pthread_mutex_lock(&philo->stop_mutex); + philo->stop = 1; + pthread_mutex_unlock(&philo->stop_mutex); + return (1); + } + pthread_mutex_lock(&data->stop_mutex); + stop = data->stop; + pthread_mutex_unlock(&data->stop_mutex); + if (stop) + { + pthread_mutex_lock(&philo->stop_mutex); + philo->stop = 1; + pthread_mutex_unlock(&philo->stop_mutex); + } + return (stop); +} + +int philo_take_forks(t_philo *philo, t_data *data) +{ + pthread_mutex_lock(&data->forks[philo->id]); + print_take_a_fork(philo); + if (check(philo, data)) + { + pthread_mutex_unlock(&data->forks[philo->id]); + return (1); + } + while ((philo->id + 1) % data->nb_philos == philo->id) + { + if (check(philo, data)) + { + pthread_mutex_unlock(&data->forks[philo->id]); + return (1); + } + } + pthread_mutex_lock(&data->forks[(philo->id + 1) % data->nb_philos]); + print_take_a_fork(philo); + if (check(philo, data)) + { + pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]); + pthread_mutex_unlock(&data->forks[philo->id]); + return (1); + } + return (0); +} + +bool philo_eat(t_philo *philo, t_data *data) +{ + if (philo_take_forks(philo, data)) + return (1); + print_eating(philo); + usleep(get_time_perfect(philo, data) * 1000); + pthread_mutex_unlock(&data->forks[philo->id]); + pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]); + if (check(philo, data)) + return (1); + pthread_mutex_lock(&philo->last_eat_mutex); + philo->last_eat = get_time(); + pthread_mutex_unlock(&philo->last_eat_mutex); + pthread_mutex_lock(&philo->nb_meal_mutex); + philo->nb_meal++; + pthread_mutex_unlock(&philo->nb_meal_mutex); + return (0); +} + +void philo_sleep(t_data *data, t_philo *philo) +{ + print_sleeping(philo); + usleep(data->eat_time * 1000); +} + +void *philo_routine(void *arg) +{ + t_philo *philo; + t_data *data; + + philo = arg; + data = philo->data; + print_thinking(philo); + usleep((philo->id % 2) * (data->eat_time) * 1000); + while (true) + { + if (check(philo, data)) + return (NULL); + if (philo_eat(philo, data)) + return (NULL); + if (check(philo, data)) + return (NULL); + philo_sleep(data, philo); + if (check(philo, data)) + return (NULL); + print_thinking(philo); + } + return (NULL); +} diff --git a/philos.c b/philos.c index 1c3056e..2774037 100644 --- a/philos.c +++ b/philos.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* philos.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet #include diff --git a/philos.h b/philos.h index 1be6aa0..be1b5bd 100644 --- a/philos.h +++ b/philos.h @@ -1,5 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* philos.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet # include "./data.h" bool philos_init(t_data *data); void philos_destroyer(t_data *data); + +#endif diff --git a/print.c b/print.c index f260cba..5cee2ab 100644 --- a/print.c +++ b/print.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet #include @@ -47,20 +59,3 @@ void print_thinking(t_philo *philo) { print(philo->data, philo->id, "is thinking"); } - -void print_died(t_philo *philo) -{ - t_data *data; - size_t time; - - data = philo->data; - time = get_time(); - pthread_mutex_lock(&data->print_mutex); - ft_putnum(time, 7); - ft_putchar(' '); - ft_putnum(philo->id + 1, 3); - ft_putchar(' '); - ft_putstr("died"); - ft_putchar('\n'); - pthread_mutex_unlock(&data->print_mutex); -} diff --git a/print.h b/print.h index 313fdbb..d219dea 100644 --- a/print.h +++ b/print.h @@ -1,3 +1,17 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet data; + time = get_time(); + pthread_mutex_lock(&data->print_mutex); + ft_putnum(time, 7); + ft_putchar(' '); + ft_putnum(philo->id + 1, 3); + ft_putchar(' '); + ft_putstr("died"); + ft_putchar('\n'); + pthread_mutex_unlock(&data->print_mutex); +} diff --git a/threads.c b/threads.c index 9628e59..b3b73d0 100644 --- a/threads.c +++ b/threads.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* threads.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet #include @@ -11,7 +23,8 @@ bool threads_init(t_data *data) i = 0; while (i < data->nb_philos) { - if (pthread_create(&data->threads[i], NULL, philo_routine, data->philos[i])) + if (pthread_create(&data->threads[i], NULL, philo_routine, + data->philos[i])) return (true); pthread_detach(data->threads[i]); i++; diff --git a/threads.h b/threads.h index 17228b5..7d4aba1 100644 --- a/threads.h +++ b/threads.h @@ -1,5 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* threads.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet bool threads_init(t_data *data); +#endif diff --git a/time.c b/time.c index 720d7eb..4063275 100644 --- a/time.c +++ b/time.c @@ -1,17 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* time.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet #include #include #include +#include "./philo.h" +#include "data.h" size_t get_time(void) { size_t time; static size_t start_time = 0; struct timeval tv; - + gettimeofday(&tv, NULL); time = tv.tv_sec * 1000000 + tv.tv_usec; if (start_time == 0) start_time = time; return ((time - start_time) / 1000); } + +size_t get_time_perfect(t_philo *philo, t_data *data) +{ + size_t time; + + time = get_time(); + if (time - philo->last_eat + data->eat_time > data->life_expectency) + return ((time - philo->last_eat + data->eat_time)); + else + return (data->eat_time); +} diff --git a/time.h b/time.h index c8c4cef..ec75214 100644 --- a/time.h +++ b/time.h @@ -1,3 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* time.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet size_t get_time(void); +size_t get_time_perfect(t_philo *philo, t_data *data); + +#endif diff --git a/utils/ft_atoi.c b/utils/ft_atoi.c index e8b20c1..84b0d62 100644 --- a/utils/ft_atoi.c +++ b/utils/ft_atoi.c @@ -6,7 +6,7 @@ /* By: cchauvet void ft_putchar(char c) diff --git a/utils/putnum.c b/utils/putnum.c index 3eed39f..e63b5be 100644 --- a/utils/putnum.c +++ b/utils/putnum.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* putnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet #include #include diff --git a/utils/putstr.c b/utils/putstr.c index 83b579d..ffc9796 100644 --- a/utils/putstr.c +++ b/utils/putstr.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet