This commit is contained in:
Camille Chauvet 2023-04-25 14:33:35 +00:00
parent 07d4cd05c1
commit d6aee63600
6 changed files with 48 additions and 84 deletions

View File

@ -1,5 +1,8 @@
SRCS = parsing.c \ SRCS = parsing.c \
utils/ft_isnum.c \ utils/ft_isnum.c \
utils/putchar.c \
utils/putnum.c \
utils/putstr.c \
utils/ft_atoi.c \ utils/ft_atoi.c \
main.c \ main.c \
time.c \ time.c \

15
main.c
View File

@ -10,7 +10,7 @@
#include "./threads.h" #include "./threads.h"
#include "./time.h" #include "./time.h"
#include "./print.h" #include "./print.h"
#include "./struct.h" #include "./utils/utils.h"
size_t get_min_meal(t_data *data) 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); pthread_mutex_unlock(&philo->last_eat_mutex);
if (ok == 1) if (ok == 1)
{ {
print_died(philo);
stop(data); 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); print_died(philo);
stop(data);
return (NULL); return (NULL);
} }
i++; i++;
if ((ssize_t) get_min_meal(data) >= data->nb_meals) if ((ssize_t) get_min_meal(data) == data->nb_meals)
{ {
stop(data); stop(data);
printf("g pu faim\n");
return (NULL); return (NULL);
} }
} }

109
philo.c
View File

@ -1,18 +1,16 @@
#include "philo.h"
#include "struct.h"
#include "time.h"
#include "data.h"
#include <pthread.h> #include <pthread.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include "./print.h" #include "./print.h"
#include "philo.h"
#include "time.h"
#include "data.h"
void philo_destroyer(t_philo *philo) void philo_destroyer(t_philo *philo)
{ {
pthread_mutex_destroy(&philo->nb_meal_mutex); pthread_mutex_destroy(&philo->nb_meal_mutex);
pthread_mutex_destroy(&philo->last_sleep_mutex);
pthread_mutex_destroy(&philo->last_eat_mutex); pthread_mutex_destroy(&philo->last_eat_mutex);
free(philo); free(philo);
} }
@ -33,55 +31,35 @@ bool check(t_philo *philo, t_data *data)
return (stop); 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 philo_eat(t_philo *philo, t_data *data)
{ {
bool left_fork; size_t time;
bool right_fork;
left_fork = 0; pthread_mutex_lock(&data->forks[philo->id]);
right_fork = 0; print_take_a_fork(philo);
while (left_fork == 0 || right_fork == 0) 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)) if (check(philo, data))
return (1); 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);
}
eat(philo, data);
pthread_mutex_lock(&philo->last_eat_mutex); pthread_mutex_lock(&philo->last_eat_mutex);
philo->last_eat = get_time(); philo->last_eat = get_time();
pthread_mutex_unlock(&philo->last_eat_mutex); 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) void philo_sleep(t_data *data, t_philo *philo)
{ {
print_sleeping(philo); size_t time;
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);
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; philo = arg;
data = philo->data; data = philo->data;
print_thinking(philo); print_thinking(philo);
usleep(philo->id * (data->life_expectency / data->nb_philos)); usleep((philo->id * (data->life_expectency / data->nb_philos)) * 1000);
while (true) while (true)
{ {
if (check(philo, data)) if (check(philo, data))
{
return (NULL); return (NULL);
}
if (philo_eat(philo, data)) if (philo_eat(philo, data))
{
return (NULL); return (NULL);
}
if (check(philo, data)) if (check(philo, data))
{
return (NULL); return (NULL);
}
philo_sleep(data, philo); philo_sleep(data, philo);
if (check(philo, data)) if (check(philo, data))
{
return (NULL); return (NULL);
}
print_thinking(philo); print_thinking(philo);
} }
return (NULL); return (NULL);
@ -148,10 +125,8 @@ t_philo *philo_init(t_data *data)
philo->nb_meal = 0; philo->nb_meal = 0;
philo->stop = 0; philo->stop = 0;
philo->last_eat = get_time(); philo->last_eat = get_time();
philo->last_sleep = get_time();
pthread_mutex_init(&philo->nb_meal_mutex, NULL); pthread_mutex_init(&philo->nb_meal_mutex, NULL);
pthread_mutex_init(&philo->last_eat_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); pthread_mutex_init(&philo->stop_mutex, NULL);
return (philo); return (philo);
} }

View File

@ -1,5 +1,4 @@
#include "./philo.h" #include "./philo.h"
#include "struct.h"
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>

View File

@ -1,5 +1,4 @@
#include "philo.h" #include "philo.h"
#include "struct.h"
#include <pthread.h> #include <pthread.h>
#include <stdbool.h> #include <stdbool.h>
#include <unistd.h> #include <unistd.h>

View File

@ -1,5 +1,4 @@
# include "./data.h" # include "./data.h"
#include "struct.h"
# include <stdbool.h> # include <stdbool.h>
bool threads_init(t_data *data); bool threads_init(t_data *data);