Compare commits
4 Commits
f1927d349b
...
e4b5e6ea0d
Author | SHA1 | Date | |
---|---|---|---|
|
e4b5e6ea0d | ||
|
f88abe9631 | ||
|
984f4c59bf | ||
|
61b599b9f2 |
@ -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;
|
||||
|
10
philo/main.c
10
philo/main.c
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/27 11:33:25 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/04/27 11:49:33 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/05/25 16:34:22 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -75,11 +75,6 @@ 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);
|
||||
@ -87,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)
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/27 11:44:14 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/05/24 14:03:42 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/05/25 19:22:55 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -16,23 +16,15 @@
|
||||
#include "./print.h"
|
||||
#include "data.h"
|
||||
|
||||
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 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;
|
||||
@ -44,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);
|
||||
@ -59,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,18 +64,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(philo, data))
|
||||
if (check(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);
|
||||
}
|
||||
|
||||
@ -105,13 +97,15 @@ void *philo_routine(void *arg)
|
||||
usleep((philo->id % 2) * (get_time_eat(philo, data)) * 1000);
|
||||
while (true)
|
||||
{
|
||||
if (check(philo, data)
|
||||
|| philo_eat(philo, data)
|
||||
|| check(philo, data)
|
||||
|| philo_sleep(data, philo)
|
||||
|| check(philo, data))
|
||||
if (check(data)
|
||||
|| philo_eat(philo, data)
|
||||
|| check(data)
|
||||
|| philo_sleep(data, philo)
|
||||
|| check(data))
|
||||
{
|
||||
philo_stop(philo);
|
||||
pthread_mutex_lock(&philo->stop_mutex);
|
||||
philo->stop = 1;
|
||||
pthread_mutex_unlock(&philo->stop_mutex);
|
||||
return (NULL);
|
||||
}
|
||||
print_thinking(philo);
|
||||
|
@ -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);
|
||||
}
|
||||
|
18
philo/time.c
18
philo/time.c
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user