Compare commits

..

12 Commits

Author SHA1 Message Date
6e383ed9d6 ok j'arrette de troll 2023-06-01 14:24:36 +02:00
b0f5095569 ok g troll 2023-06-01 14:23:15 +02:00
f88abe9631 dsfsd 2023-05-25 17:31:02 +02:00
984f4c59bf norm 2023-05-25 15:24:35 +02:00
61b599b9f2 opti 2023-05-25 15:24:17 +02:00
f1927d349b fix: add stdbool remove useless memset 2023-05-25 14:57:43 +02:00
1b3c96b38d fix: data race 2023-05-24 16:19:21 +02:00
550851a5c8 Merge branch 'master' of git.chauvet.pro:starnakin/42_Philosopher 2023-05-24 13:55:43 +00:00
0956af87ca fix: data race 2023-05-24 15:55:19 +02:00
7e5d4af478 fix: support only > 0 number 2023-05-17 13:44:38 +02:00
008b30e12c fix: add 0 to the print 2023-05-17 13:37:20 +02:00
aefb6ceec0 bozosujet 2023-05-17 13:28:42 +02:00
21 changed files with 49 additions and 64 deletions

View File

@ -6,20 +6,15 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:28:36 by cchauvet #+# #+# */
/* Updated: 2023/05/05 16:58:45 by cchauvet ### ########.fr */
/* Updated: 2023/05/25 14:48:52 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include "./data.h"
#include "philo.h"
#include "philos.h"
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
bool data_init(t_data *data)
{
@ -29,7 +24,6 @@ bool data_init(t_data *data)
data->forks = malloc(sizeof(pthread_mutex_t) * data->nb_philos);
if (data->forks == NULL)
return (1);
memset(data->forks, 1, data->nb_philos);
data->philos = malloc(sizeof(t_philo) * data->nb_philos);
if (data->philos == NULL)
free(data->forks);

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:33:06 by cchauvet #+# #+# */
/* Updated: 2023/04/27 11:33:06 by cchauvet ### ########.fr */
/* Updated: 2023/05/25 15:26:38 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,7 @@
# define DATA_H
# include <pthread.h>
# include <sys/types.h>
# include <stdbool.h>
typedef struct s_data
{
@ -21,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/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)

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:34:57 by cchauvet #+# #+# */
/* Updated: 2023/04/27 11:34:58 by cchauvet ### ########.fr */
/* Updated: 2023/05/17 13:43:29 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,13 +23,15 @@ static bool check_amount_of_argument(size_t n)
static bool check_value(char **av, size_t n)
{
size_t i;
size_t i;
i = 0;
while (i < n)
{
if (ft_isnum(av[i]) == 0)
return (1);
if (!(ft_atoi(av[i]) > 0))
return (1);
i++;
}
return (0);

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:44:14 by cchauvet #+# #+# */
/* Updated: 2023/05/16 13:13:13 by cchauvet ### ########.fr */
/* Updated: 2023/06/01 14:23:53 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,26 +16,19 @@
#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;
pthread_mutex_unlock(&data->stop_mutex);
if (stop)
{
pthread_mutex_lock(&philo->stop_mutex);
philo->stop = 1;
pthread_mutex_unlock(&philo->stop_mutex);
}
return (stop);
}
@ -43,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);
@ -58,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]);
@ -71,25 +64,26 @@ bool philo_eat(t_philo *philo, t_data *data)
{
if (philo_take_forks(philo, data))
return (1);
philo->last_eat = get_time();
print_eating(philo);
pthread_mutex_lock(&philo->last_eat_mutex);
philo->last_eat = get_time();
pthread_mutex_unlock(&philo->last_eat_mutex);
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->last_eat_mutex);
pthread_mutex_unlock(&philo->last_eat_mutex);
pthread_mutex_lock(&philo->nb_meal_mutex);
philo->nb_meal++;
pthread_mutex_unlock(&philo->nb_meal_mutex);
return (0);
}
void philo_sleep(t_data *data, t_philo *philo)
int philo_sleep(t_data *data, t_philo *philo)
{
print_sleeping(philo);
usleep(get_time_sleep(philo, data) * 1000);
return (0);
}
void *philo_routine(void *arg)
@ -103,15 +97,17 @@ void *philo_routine(void *arg)
usleep((philo->id % 2) * (get_time_eat(philo, data)) * 1000);
while (true)
{
if (check(philo, data))
return (NULL);
if (philo_eat(philo, data))
return (NULL);
if (check(philo, data))
return (NULL);
philo_sleep(data, philo);
if (check(philo, data))
if (check(data)
|| philo_eat(philo, data)
|| check(data)
|| philo_sleep(data, philo)
|| check(data))
{
pthread_mutex_lock(&philo->stop_mutex);
philo->stop = 1;
pthread_mutex_unlock(&philo->stop_mutex);
return (NULL);
}
print_thinking(philo);
}
return (NULL);

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:22:19 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("%zu %zu %s\n", time, id, 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:41:14 by cchauvet #+# #+# */
/* Updated: 2023/05/17 13:21:33 by cchauvet ### ########.fr */
/* Updated: 2023/05/17 13:33:33 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,6 +22,6 @@ void print_died(t_philo *philo)
data = philo->data;
time = get_time();
pthread_mutex_lock(&data->print_mutex);
printf("%7zu %3zu %s\n", time, philo->id, "died");
printf("%07zu %03zu %s\n", time, philo->id + 1, "died");
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);
}