This commit is contained in:
Camille Chauvet 2023-05-25 17:31:02 +02:00
parent 984f4c59bf
commit f88abe9631
5 changed files with 27 additions and 31 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 14:49:38 by cchauvet ### ########.fr */
/* Updated: 2023/05/25 15:26:38 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,7 +22,7 @@ typedef struct s_data
size_t sleep_time;
size_t life_expectency;
size_t nb_philos;
size_t nb_meals;
long 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 15:22:42 by cchauvet ### ########.fr */
/* Updated: 2023/05/25 16:34:22 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -82,13 +82,14 @@ int routine(t_data *data)
void *check_routine(t_data *data)
{
while (true)
while (data->stop == 0)
{
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 15:08:44 by cchauvet ### ########.fr */
/* Updated: 2023/05/25 17:30:05 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,16 +16,15 @@
#include "./print.h"
#include "data.h"
bool check(t_philo *philo, t_data *data)
bool check(t_data *data)
{
bool stop;
if (get_min_meal(data) == data->nb_meals)
if (data->nb_meals != -1 && get_min_meal(data) == (size_t) 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);
data->stop = 1;
pthread_mutex_unlock(&data->stop_mutex);
}
pthread_mutex_lock(&data->stop_mutex);
stop = data->stop;
@ -37,14 +36,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(philo, data))
if (check(data))
{
pthread_mutex_unlock(&data->forks[philo->id]);
return (1);
}
while ((philo->id + 1) % data->nb_philos == philo->id)
{
if (check(philo, data))
if (check(data))
{
pthread_mutex_unlock(&data->forks[philo->id]);
return (1);
@ -52,7 +51,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(philo, data))
if (check(data))
{
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
pthread_mutex_unlock(&data->forks[philo->id]);
@ -72,7 +71,7 @@ bool philo_eat(t_philo *philo, t_data *data)
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(philo, data))
if (check(data))
return (1);
pthread_mutex_lock(&philo->nb_meal_mutex);
philo->nb_meal++;
@ -98,11 +97,11 @@ void *philo_routine(void *arg)
usleep((philo->id % 2) * (get_time_eat(philo, data)) * 1000);
while (true)
{
if (check(philo, data)
if (check(data)
|| philo_eat(philo, data)
|| check(philo, data)
|| check(data)
|| philo_sleep(data, philo)
|| check(philo, data))
|| check(data))
{
pthread_mutex_lock(&philo->stop_mutex);
philo->stop = 1;

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:33:33 by cchauvet #+# #+# */
/* Updated: 2023/05/17 13:33:17 by cchauvet ### ########.fr */
/* Updated: 2023/05/25 16:47:54 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 %07zu %s\n", time, id + 1, str);
printf("%07zu %03zu %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/16 13:24:30 by cchauvet ### ########.fr */
/* Updated: 2023/05/25 17:28:19 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -33,23 +33,19 @@ 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();
value = (data->life_expectency - philo->last_eat - time);
if (value > data->eat_time)
value = data->eat_time;
return (value);
if (time + data->eat_time > data->life_expectency + philo->last_eat)
return (data->life_expectency + philo->last_eat - time);
return (data->eat_time);
}
size_t get_time_sleep(t_philo *philo, t_data *data)
{
size_t time;
size_t value;
time = get_time();
value = (data->life_expectency - (time - philo->last_eat));
if (value > data->sleep_time)
value = data->sleep_time;
return (value);
if (time + data->sleep_time > data->life_expectency + philo->last_eat)
return (data->life_expectency + philo->last_eat - time);
return (data->sleep_time);
}