diff --git a/Makefile b/Makefile index 176ce93..66b0a18 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,8 @@ SRCS = parsing.c \ utils/ft_isnum.c \ + utils/putchar.c \ + utils/putnum.c \ + utils/putstr.c \ utils/ft_atoi.c \ main.c \ time.c \ diff --git a/main.c b/main.c index 754ed08..2ba46b8 100644 --- a/main.c +++ b/main.c @@ -10,7 +10,7 @@ #include "./threads.h" #include "./time.h" #include "./print.h" -#include "./struct.h" +#include "./utils/utils.h" size_t get_min_meal(t_data *data) { @@ -61,25 +61,14 @@ void *check_routine(t_data *data) pthread_mutex_unlock(&philo->last_eat_mutex); if (ok == 1) { - print_died(philo); stop(data); - return (NULL); - } - pthread_mutex_lock(&philo->last_sleep_mutex); - // printf("time=%zu, last_sleep=%zu, life_expectency=%zu\n", get_time(), philo->last_sleep, data->life_expectency); - ok = !(philo->last_sleep + data->life_expectency > get_time()); - pthread_mutex_unlock(&philo->last_sleep_mutex); - if (ok == 1) - { print_died(philo); - stop(data); return (NULL); } i++; - if ((ssize_t) get_min_meal(data) >= data->nb_meals) + if ((ssize_t) get_min_meal(data) == data->nb_meals) { stop(data); - printf("g pu faim\n"); return (NULL); } } diff --git a/philo.c b/philo.c index 6311040..e4e3c60 100644 --- a/philo.c +++ b/philo.c @@ -1,18 +1,16 @@ -#include "philo.h" -#include "struct.h" -#include "time.h" -#include "data.h" #include #include #include #include #include #include "./print.h" +#include "philo.h" +#include "time.h" +#include "data.h" void philo_destroyer(t_philo *philo) { pthread_mutex_destroy(&philo->nb_meal_mutex); - pthread_mutex_destroy(&philo->last_sleep_mutex); pthread_mutex_destroy(&philo->last_eat_mutex); free(philo); } @@ -33,55 +31,35 @@ bool check(t_philo *philo, t_data *data) return (stop); } -void eat(t_philo *philo, t_data *data) -{ - print_take_a_fork(philo); - print_take_a_fork(philo); - print_eating(philo); - pthread_mutex_lock(&philo->last_eat_mutex); - if (get_time() - philo->last_eat + data->eat_time > data->life_expectency) - { - pthread_mutex_unlock(&philo->last_eat_mutex); - usleep((data->life_expectency - - (get_time() - philo->last_eat)) * 1000); - return ; - } - else - usleep(data->eat_time * 1000); - pthread_mutex_unlock(&philo->last_eat_mutex); - pthread_mutex_lock(&philo->nb_meal_mutex); - philo->nb_meal++; - pthread_mutex_unlock(&philo->nb_meal_mutex); - pthread_mutex_lock(&data->forks_mutex); - data->forks[philo->id] = 1; - data->forks[(philo->id + 1) % data->nb_philos] = 1; - pthread_mutex_unlock(&data->forks_mutex); -} - bool philo_eat(t_philo *philo, t_data *data) { - bool left_fork; - bool right_fork; + size_t time; - left_fork = 0; - right_fork = 0; - while (left_fork == 0 || right_fork == 0) + pthread_mutex_lock(&data->forks[philo->id]); + print_take_a_fork(philo); + if (check(philo, data)) { - if (check(philo, data)) - return (1); - pthread_mutex_lock(&data->forks_mutex); - left_fork = data->forks[philo->id]; - if (((philo->id + 1) % data->nb_philos) != philo->id) - right_fork = data->forks[(philo->id + 1) % data->nb_philos]; - if (right_fork && left_fork) - { - data->forks[philo->id] = 0; - data->forks[(philo->id + 1) % data->nb_philos] = 0; - } - pthread_mutex_unlock(&data->forks_mutex); - usleep(10); + pthread_mutex_unlock(&data->forks[philo->id]); + return (1); } - eat(philo, data); + 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); @@ -90,23 +68,14 @@ bool philo_eat(t_philo *philo, t_data *data) void philo_sleep(t_data *data, t_philo *philo) { - print_sleeping(philo); - pthread_mutex_lock(&philo->last_sleep_mutex); - // printf("time=%zu, last_sleep=%zu, sleep_time=%zu, life_expectency=%zu\n", get_time(), philo->last_sleep, data->sleep_time, data->life_expectency); - if (get_time() - philo->last_sleep + data->sleep_time > data->life_expectency) - { - pthread_mutex_unlock(&philo->last_sleep_mutex); - // printf("rompiche=%zu\n", (data->life_expectency - - // (get_time() - philo->last_sleep))); - usleep((data->life_expectency - - (get_time() - philo->last_sleep)) * 1000); - return ; - } - else - usleep(data->sleep_time * 1000); - philo->last_sleep = get_time(); - pthread_mutex_unlock(&philo->last_sleep_mutex); + 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); } @@ -118,18 +87,26 @@ void *philo_routine(void *arg) philo = arg; data = philo->data; print_thinking(philo); - usleep(philo->id * (data->life_expectency / data->nb_philos)); + usleep((philo->id * (data->life_expectency / data->nb_philos)) * 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); @@ -148,10 +125,8 @@ t_philo *philo_init(t_data *data) philo->nb_meal = 0; philo->stop = 0; philo->last_eat = get_time(); - philo->last_sleep = get_time(); pthread_mutex_init(&philo->nb_meal_mutex, NULL); pthread_mutex_init(&philo->last_eat_mutex, NULL); - pthread_mutex_init(&philo->last_sleep_mutex, NULL); pthread_mutex_init(&philo->stop_mutex, NULL); return (philo); } diff --git a/philos.c b/philos.c index 5708861..1c3056e 100644 --- a/philos.c +++ b/philos.c @@ -1,5 +1,4 @@ #include "./philo.h" -#include "struct.h" #include #include diff --git a/threads.c b/threads.c index 1414fe9..9628e59 100644 --- a/threads.c +++ b/threads.c @@ -1,5 +1,4 @@ #include "philo.h" -#include "struct.h" #include #include #include diff --git a/threads.h b/threads.h index 4d14787..17228b5 100644 --- a/threads.h +++ b/threads.h @@ -1,5 +1,4 @@ # include "./data.h" -#include "struct.h" # include bool threads_init(t_data *data);