42_Philosopher/philo.c

133 lines
2.8 KiB
C
Raw Normal View History

2023-04-13 09:00:39 -04:00
#include <pthread.h>
#include <stdbool.h>
2023-04-19 07:53:50 -04:00
#include <stddef.h>
#include <stdio.h>
2023-04-13 09:00:39 -04:00
#include <unistd.h>
#include "./print.h"
2023-04-25 10:33:35 -04:00
#include "philo.h"
#include "time.h"
#include "data.h"
2023-04-13 09:00:39 -04:00
void philo_destroyer(t_philo *philo)
{
pthread_mutex_destroy(&philo->nb_meal_mutex);
pthread_mutex_destroy(&philo->last_eat_mutex);
free(philo);
}
bool check(t_philo *philo, t_data *data)
2023-04-13 09:00:39 -04:00
{
bool stop;
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);
}
2023-04-13 09:00:39 -04:00
return (stop);
}
2023-04-25 10:33:35 -04:00
bool philo_eat(t_philo *philo, t_data *data)
2023-04-13 09:00:39 -04:00
{
2023-04-25 10:33:35 -04:00
size_t time;
pthread_mutex_lock(&data->forks[philo->id]);
2023-04-13 09:00:39 -04:00
print_take_a_fork(philo);
2023-04-25 10:33:35 -04:00
if (check(philo, data))
{
pthread_mutex_unlock(&data->forks[philo->id]);
return (1);
}
pthread_mutex_lock(&data->forks[(philo->id + 1) % data->nb_philos]);
2023-04-13 09:00:39 -04:00
print_take_a_fork(philo);
2023-04-25 10:33:35 -04:00
if (check(philo, data))
{
2023-04-25 10:33:35 -04:00
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
pthread_mutex_unlock(&data->forks[philo->id]);
return (1);
}
2023-04-25 10:33:35 -04:00
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);
2023-04-25 10:33:35 -04:00
pthread_mutex_unlock(&data->forks[philo->id]);
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
if (check(philo, data))
return (1);
2023-04-13 09:00:39 -04:00
pthread_mutex_lock(&philo->last_eat_mutex);
philo->last_eat = get_time();
pthread_mutex_unlock(&philo->last_eat_mutex);
return (0);
}
void philo_sleep(t_data *data, t_philo *philo)
{
2023-04-25 10:33:35 -04:00
size_t time;
2023-04-13 09:00:39 -04:00
print_sleeping(philo);
2023-04-25 10:33:35 -04:00
time = get_time();
if (time - philo->last_eat + data->eat_time > data->life_expectency)
usleep((time - philo->last_eat + data->eat_time) * 1000);
else
2023-04-25 10:33:35 -04:00
usleep(data->eat_time * 1000);
2023-04-13 09:00:39 -04:00
}
void *philo_routine(void *arg)
{
t_philo *philo;
t_data *data;
philo = arg;
data = philo->data;
print_thinking(philo);
2023-04-25 10:39:07 -04:00
usleep((philo->id % 2) * (data->eat_time) * 1000);
2023-04-13 09:00:39 -04:00
while (true)
{
if (check(philo, data))
2023-04-25 10:33:35 -04:00
{
2023-04-19 07:53:50 -04:00
return (NULL);
2023-04-25 10:33:35 -04:00
}
2023-04-19 07:53:50 -04:00
if (philo_eat(philo, data))
2023-04-25 10:33:35 -04:00
{
2023-04-19 07:53:50 -04:00
return (NULL);
2023-04-25 10:33:35 -04:00
}
if (check(philo, data))
2023-04-25 10:33:35 -04:00
{
2023-04-13 09:00:39 -04:00
return (NULL);
2023-04-25 10:33:35 -04:00
}
2023-04-13 09:00:39 -04:00
philo_sleep(data, philo);
if (check(philo, data))
2023-04-25 10:33:35 -04:00
{
return (NULL);
2023-04-25 10:33:35 -04:00
}
print_thinking(philo);
2023-04-13 09:00:39 -04:00
}
return (NULL);
}
t_philo *philo_init(t_data *data)
{
t_philo *philo;
static size_t id = 0;
philo = malloc(sizeof(t_philo));
if (philo == NULL)
return (NULL);
philo->id = id++;
philo->data = data;
philo->nb_meal = 0;
2023-04-20 12:27:12 -04:00
philo->stop = 0;
2023-04-19 07:53:50 -04:00
philo->last_eat = get_time();
2023-04-13 09:00:39 -04:00
pthread_mutex_init(&philo->nb_meal_mutex, NULL);
2023-04-19 07:53:50 -04:00
pthread_mutex_init(&philo->last_eat_mutex, NULL);
pthread_mutex_init(&philo->stop_mutex, NULL);
2023-04-13 09:00:39 -04:00
return (philo);
}