Compare commits

..

No commits in common. "e4b5e6ea0db80af62f3d12f484baf70c24caf44f" and "f1927d349bd8d5cbf8708b7074fa203bf2a307f2" have entirely different histories.

5 changed files with 49 additions and 35 deletions

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:33:06 by cchauvet #+# #+# */
/* Updated: 2023/05/25 15:26:38 by cchauvet ### ########.fr */
/* Updated: 2023/05/25 14:49:38 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,7 +22,7 @@ typedef struct s_data
size_t sleep_time;
size_t life_expectency;
size_t nb_philos;
long nb_meals;
size_t nb_meals;
void **philos;
pthread_t *threads;
pthread_mutex_t *forks;

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:33:25 by cchauvet #+# #+# */
/* Updated: 2023/05/25 16:34:22 by cchauvet ### ########.fr */
/* Updated: 2023/04/27 11:49:33 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -75,6 +75,11 @@ int routine(t_data *data)
print_died(philo);
return (1);
}
if (get_min_meal(data) >= data->nb_meals)
{
stop(data);
return (1);
}
i++;
}
return (0);
@ -82,14 +87,13 @@ int routine(t_data *data)
void *check_routine(t_data *data)
{
while (data->stop == 0)
while (true)
{
if (routine(data))
{
return (NULL);
}
}
return (NULL);
}
int main(int ac, char **av)

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:44:14 by cchauvet #+# #+# */
/* Updated: 2023/05/25 19:22:55 by cchauvet ### ########.fr */
/* Updated: 2023/05/24 14:03:42 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,15 +16,23 @@
#include "./print.h"
#include "data.h"
bool check(t_data *data)
void philo_stop(t_philo *philo)
{
pthread_mutex_lock(&philo->stop_mutex);
philo->stop = 1;
pthread_mutex_unlock(&philo->stop_mutex);
}
bool check(t_philo *philo, t_data *data)
{
bool stop;
if (data->nb_meals != -1 && get_min_meal(data) == (size_t) data->nb_meals)
if (get_min_meal(data) == data->nb_meals)
{
pthread_mutex_lock(&data->stop_mutex);
data->stop = 1;
pthread_mutex_unlock(&data->stop_mutex);
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;
@ -36,14 +44,14 @@ int philo_take_forks(t_philo *philo, t_data *data)
{
pthread_mutex_lock(&data->forks[philo->id]);
print_take_a_fork(philo);
if (check(data))
if (check(philo, data))
{
pthread_mutex_unlock(&data->forks[philo->id]);
return (1);
}
while ((philo->id + 1) % data->nb_philos == philo->id)
{
if (check(data))
if (check(philo, data))
{
pthread_mutex_unlock(&data->forks[philo->id]);
return (1);
@ -51,7 +59,7 @@ int philo_take_forks(t_philo *philo, t_data *data)
}
pthread_mutex_lock(&data->forks[(philo->id + 1) % data->nb_philos]);
print_take_a_fork(philo);
if (check(data))
if (check(philo, data))
{
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
pthread_mutex_unlock(&data->forks[philo->id]);
@ -64,18 +72,18 @@ bool philo_eat(t_philo *philo, t_data *data)
{
if (philo_take_forks(philo, data))
return (1);
pthread_mutex_lock(&philo->last_eat_mutex);
philo->last_eat = get_time();
pthread_mutex_unlock(&philo->last_eat_mutex);
print_eating(philo);
usleep(get_time_eat(philo, data) * 1000);
pthread_mutex_unlock(&data->forks[philo->id]);
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
if (check(data))
if (check(philo, data))
return (1);
pthread_mutex_lock(&philo->nb_meal_mutex);
philo->nb_meal++;
pthread_mutex_unlock(&philo->nb_meal_mutex);
pthread_mutex_lock(&philo->last_eat_mutex);
philo->last_eat = get_time();
pthread_mutex_unlock(&philo->last_eat_mutex);
return (0);
}
@ -97,15 +105,13 @@ void *philo_routine(void *arg)
usleep((philo->id % 2) * (get_time_eat(philo, data)) * 1000);
while (true)
{
if (check(data)
if (check(philo, data)
|| philo_eat(philo, data)
|| check(data)
|| check(philo, data)
|| philo_sleep(data, philo)
|| check(data))
|| check(philo, data))
{
pthread_mutex_lock(&philo->stop_mutex);
philo->stop = 1;
pthread_mutex_unlock(&philo->stop_mutex);
philo_stop(philo);
return (NULL);
}
print_thinking(philo);

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:33:33 by cchauvet #+# #+# */
/* Updated: 2023/05/25 16:47:54 by cchauvet ### ########.fr */
/* Updated: 2023/05/17 13:33:17 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -30,7 +30,7 @@ static void print(t_data *data, size_t id, char *str)
if (stop == false)
{
time = get_time();
printf("%07zu %03zu %s\n", time, id + 1, str);
printf("%07zu %07zu %s\n", time, id + 1, str);
}
pthread_mutex_unlock(&data->print_mutex);
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:35:26 by cchauvet #+# #+# */
/* Updated: 2023/05/25 17:28:19 by cchauvet ### ########.fr */
/* Updated: 2023/05/16 13:24:30 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -33,19 +33,23 @@ size_t get_time(void)
size_t get_time_eat(t_philo *philo, t_data *data)
{
size_t time;
size_t value;
time = get_time();
if (time + data->eat_time > data->life_expectency + philo->last_eat)
return (data->life_expectency + philo->last_eat - time);
return (data->eat_time);
value = (data->life_expectency - philo->last_eat - time);
if (value > data->eat_time)
value = data->eat_time;
return (value);
}
size_t get_time_sleep(t_philo *philo, t_data *data)
{
size_t time;
size_t value;
time = get_time();
if (time + data->sleep_time > data->life_expectency + philo->last_eat)
return (data->life_expectency + philo->last_eat - time);
return (data->sleep_time);
value = (data->life_expectency - (time - philo->last_eat));
if (value > data->sleep_time)
value = data->sleep_time;
return (value);
}