Compare commits

...

24 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
8371b01390 clean: remove compile file 2023-05-17 13:26:16 +02:00
3e9f5e829f fix: replace write by printf 2023-05-17 13:25:34 +02:00
958448ca4e finish2 2023-05-16 11:26:03 +00:00
291c7b40b1 finish 2023-05-16 11:25:20 +00:00
b940b46924 ?fix 2023-05-05 16:59:36 +00:00
5c309f2f02 fix: sleeptime and eat time if time to die < time eat|sleep 2023-05-02 11:44:16 +00:00
796e186c36 clean: norm 2023-04-27 12:05:14 +00:00
7e7538aa40 fix: do not print message exit eating 2023-04-25 15:10:25 +00:00
41bd1149d8 fix: time mix 2023-04-25 15:01:54 +00:00
69e34424c1 fix: philo alone died now 2023-04-25 14:56:57 +00:00
02a80af93b fix: stop when nb_meal is reached 2023-04-25 14:50:01 +00:00
891431c1be fix: work with odd numbers of philos 2023-04-25 14:39:07 +00:00
37 changed files with 730 additions and 565 deletions

23
data.h
View File

@ -1,23 +0,0 @@
#ifndef DATA_H
# define DATA_H
# include <pthread.h>
# include <sys/types.h>
typedef struct s_data
{
size_t eat_time;
size_t sleep_time;
size_t life_expectency;
size_t nb_philos;
ssize_t nb_meals;
void **philos;
pthread_t *threads;
pthread_mutex_t *forks;
pthread_mutex_t stop_mutex;
bool stop;
pthread_mutex_t print_mutex;
} t_data;
bool data_init(t_data *data);
void data_destroyer(t_data *data);
#endif

99
main.c
View File

@ -1,99 +0,0 @@
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "./philos.h"
#include "./data.h"
#include "./parsing.h"
#include "./threads.h"
#include "./time.h"
#include "./print.h"
#include "./utils/utils.h"
size_t get_min_meal(t_data *data)
{
size_t i;
size_t min;
size_t value;
t_philo *philo;
philo = data->philos[0];
pthread_mutex_lock(&philo->nb_meal_mutex);
min = philo->nb_meal;
pthread_mutex_unlock(&philo->nb_meal_mutex);
i = 1;
while (i < data->nb_philos)
{
philo = data->philos[i];
pthread_mutex_lock(&philo->nb_meal_mutex);
value = philo->nb_meal;
pthread_mutex_unlock(&philo->nb_meal_mutex);
if (min > value)
min = value;
i++;
}
return (min);
}
void stop(t_data *data)
{
pthread_mutex_lock(&data->stop_mutex);
data->stop = 1;
pthread_mutex_unlock(&data->stop_mutex);
}
void *check_routine(t_data *data)
{
size_t i;
bool ok;
t_philo *philo;
while (true)
{
i = 0;
while (i < data->nb_philos)
{
philo = data->philos[i];
pthread_mutex_lock(&philo->last_eat_mutex);
ok = !(philo->last_eat + data->life_expectency > get_time());
pthread_mutex_unlock(&philo->last_eat_mutex);
if (ok == 1)
{
stop(data);
print_died(philo);
return (NULL);
}
i++;
if ((ssize_t) get_min_meal(data) == data->nb_meals)
{
stop(data);
return (NULL);
}
}
}
}
int main(int ac, char **av)
{
t_data data;
get_time();
if (parsing(&data, av + 1, ac - 1))
return (1);
if (data_init(&data))
return (1);
if (philos_init(&data))
{
data_destroyer(&data);
return (1);
}
if (threads_init(&data))
{
data_destroyer(&data);
return (1);
}
check_routine(&data);
data_destroyer(&data);
}

View File

@ -1,50 +0,0 @@
#include "utils/utils.h"
#include "./data.h"
#include <stdbool.h>
#include <stddef.h>
#include <unistd.h>
static bool check_amount_of_argument(size_t n)
{
return (n < 4 || n > 5);
}
static bool check_value(char **av, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
if (ft_isnum(av[i]) == 0)
return (1);
i++;
}
return (0);
}
static void set_value(t_data *data, char **args, size_t n)
{
data->nb_philos = ft_atoi(args[0]);
data->life_expectency = ft_atoi(args[1]);
data->eat_time = ft_atoi(args[2]);
data->sleep_time = ft_atoi(args[3]);
if (n == 5)
data->nb_meals = ft_atoi(args[4]);
else
data->nb_meals = -1;
}
bool parsing(t_data *data, char **args, size_t n)
{
if (check_amount_of_argument(n)
|| check_value(args, n))
{
write(2, "Argument error !\n", 16);
return (1);
}
set_value(data, args, n);
if (data->nb_philos == 0)
write(2, "Argument error !\n", 16);
return (data->nb_philos == 0);
}

View File

@ -1,4 +0,0 @@
#include "./data.h"
bool parsing(t_data *data, char **args, size_t n);

132
philo.c
View File

@ -1,132 +0,0 @@
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include "./print.h"
#include "philo.h"
#include "time.h"
#include "data.h"
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)
{
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);
}
return (stop);
}
bool philo_eat(t_philo *philo, t_data *data)
{
size_t time;
pthread_mutex_lock(&data->forks[philo->id]);
print_take_a_fork(philo);
if (check(philo, data))
{
pthread_mutex_unlock(&data->forks[philo->id]);
return (1);
}
pthread_mutex_lock(&data->forks[(philo->id + 1) % data->nb_philos]);
print_take_a_fork(philo);
if (check(philo, data))
{
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
pthread_mutex_unlock(&data->forks[philo->id]);
return (1);
}
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);
pthread_mutex_unlock(&data->forks[philo->id]);
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
if (check(philo, data))
return (1);
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)
{
size_t time;
print_sleeping(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);
}
void *philo_routine(void *arg)
{
t_philo *philo;
t_data *data;
philo = arg;
data = philo->data;
print_thinking(philo);
usleep((philo->id * (data->life_expectency / data->nb_philos)) * 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))
{
return (NULL);
}
print_thinking(philo);
}
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;
philo->stop = 0;
philo->last_eat = get_time();
pthread_mutex_init(&philo->nb_meal_mutex, NULL);
pthread_mutex_init(&philo->last_eat_mutex, NULL);
pthread_mutex_init(&philo->stop_mutex, NULL);
return (philo);
}

View File

@ -1,14 +1,13 @@
SRCS = parsing.c \
utils/ft_isnum.c \
utils/putchar.c \
utils/putnum.c \
utils/putstr.c \
utils/ft_atoi.c \
main.c \
time.c \
print.c \
print2.c \
data.c \
philo.c \
philo_routine.c \
philos.c \
threads.c

View File

@ -1,28 +1,34 @@
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* data.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:28:36 by cchauvet #+# #+# */
/* Updated: 2023/05/25 14:48:52 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "./data.h"
#include "philo.h"
#include "philos.h"
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
bool data_init(t_data *data)
{
size_t i;
ssize_t i;
data->stop = 0;
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);
if (data->philos == NULL)
return (1);
}
data->threads = malloc(sizeof(pthread_t) * data->nb_philos);
if (data->forks == NULL)
{
@ -30,12 +36,9 @@ bool data_init(t_data *data)
free(data->forks);
return (1);
}
i = 0;
while (i < data->nb_philos)
{
i = -1;
while (++i < (ssize_t) data->nb_philos)
pthread_mutex_init(&data->forks[i], NULL);
i++;
}
pthread_mutex_init(&data->stop_mutex, NULL);
pthread_mutex_init(&data->print_mutex, NULL);
return (0);
@ -43,12 +46,12 @@ bool data_init(t_data *data)
void data_destroyer(t_data *data)
{
size_t i;
ssize_t i;
t_philo *philo;
bool stop;
i = 0;
while (i < data->nb_philos)
while (i < (ssize_t) data->nb_philos)
{
philo = data->philos[i];
pthread_mutex_lock(&philo->stop_mutex);
@ -58,12 +61,10 @@ void data_destroyer(t_data *data)
i++;
usleep(1000);
}
i = 0;
while (i < data->nb_philos)
{
usleep(1000);
i = -1;
while (++i < (ssize_t) data->nb_philos)
pthread_mutex_destroy(&data->forks[i]);
i++;
}
pthread_mutex_destroy(&data->stop_mutex);
pthread_mutex_destroy(&data->print_mutex);
free(data->threads);

37
philo/data.h Normal file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* data.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#ifndef DATA_H
# define DATA_H
# include <pthread.h>
# include <sys/types.h>
# include <stdbool.h>
typedef struct s_data
{
size_t eat_time;
size_t sleep_time;
size_t life_expectency;
size_t nb_philos;
long nb_meals;
void **philos;
pthread_t *threads;
pthread_mutex_t *forks;
pthread_mutex_t stop_mutex;
bool stop;
pthread_mutex_t print_mutex;
} t_data;
bool data_init(t_data *data);
void data_destroyer(t_data *data);
size_t get_min_meal(t_data *data);
#endif

116
philo/main.c Normal file
View File

@ -0,0 +1,116 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "./philos.h"
#include "./data.h"
#include "./parsing.h"
#include "./threads.h"
#include "./time.h"
#include "./print.h"
#include "./utils/utils.h"
size_t get_min_meal(t_data *data)
{
size_t i;
size_t min;
size_t value;
t_philo *philo;
philo = data->philos[0];
pthread_mutex_lock(&philo->nb_meal_mutex);
min = philo->nb_meal;
pthread_mutex_unlock(&philo->nb_meal_mutex);
i = 1;
while (i < data->nb_philos)
{
philo = data->philos[i];
pthread_mutex_lock(&philo->nb_meal_mutex);
value = philo->nb_meal;
pthread_mutex_unlock(&philo->nb_meal_mutex);
if (min > value)
min = value;
i++;
}
return (min);
}
void stop(t_data *data)
{
pthread_mutex_lock(&data->stop_mutex);
data->stop = 1;
pthread_mutex_unlock(&data->stop_mutex);
}
int routine(t_data *data)
{
size_t i;
bool ok;
t_philo *philo;
i = 0;
while (i < data->nb_philos)
{
philo = data->philos[i];
pthread_mutex_lock(&philo->last_eat_mutex);
ok = !(philo->last_eat + data->life_expectency > get_time());
pthread_mutex_unlock(&philo->last_eat_mutex);
if (ok == 1)
{
stop(data);
print_died(philo);
return (1);
}
i++;
}
return (0);
}
void *check_routine(t_data *data)
{
while (data->stop == 0)
{
if (routine(data))
{
return (NULL);
}
}
return (NULL);
}
int main(int ac, char **av)
{
t_data data;
get_time();
if (parsing(&data, av + 1, ac - 1))
return (1);
if (data_init(&data))
return (1);
if (philos_init(&data))
{
data_destroyer(&data);
return (1);
}
if (threads_init(&data))
{
data_destroyer(&data);
return (1);
}
check_routine(&data);
data_destroyer(&data);
}

64
philo/parsing.c Normal file
View File

@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:34:57 by cchauvet #+# #+# */
/* Updated: 2023/05/17 13:43:29 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "utils/utils.h"
#include "./data.h"
#include <stdbool.h>
#include <stddef.h>
#include <unistd.h>
static bool check_amount_of_argument(size_t n)
{
return (n < 4 || n > 5);
}
static bool check_value(char **av, size_t n)
{
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);
}
static void set_value(t_data *data, char **args, size_t n)
{
data->nb_philos = ft_atoi(args[0]);
data->life_expectency = ft_atoi(args[1]);
data->eat_time = ft_atoi(args[2]);
data->sleep_time = ft_atoi(args[3]);
if (n == 5)
data->nb_meals = ft_atoi(args[4]);
else
data->nb_meals = -1;
}
bool parsing(t_data *data, char **args, size_t n)
{
if (check_amount_of_argument(n)
|| check_value(args, n))
{
write(2, "Argument error !\n", 16);
return (1);
}
set_value(data, args, n);
if (data->nb_philos == 0)
write(2, "Argument error !\n", 16);
return (data->nb_philos == 0);
}

19
philo/parsing.h Normal file
View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:39:55 by cchauvet #+# #+# */
/* Updated: 2023/04/27 11:40:42 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PARSING_H
# define PARSING_H
# include "./data.h"
bool parsing(t_data *data, char **args, size_t n);
#endif

47
philo/philo.c Normal file
View File

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:38:52 by cchauvet #+# #+# */
/* Updated: 2023/04/27 11:56:04 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include "./print.h"
#include "philo.h"
#include "time.h"
#include "data.h"
void philo_destroyer(t_philo *philo)
{
pthread_mutex_destroy(&philo->nb_meal_mutex);
pthread_mutex_destroy(&philo->last_eat_mutex);
free(philo);
}
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;
philo->stop = 0;
philo->last_eat = get_time();
pthread_mutex_init(&philo->nb_meal_mutex, NULL);
pthread_mutex_init(&philo->last_eat_mutex, NULL);
pthread_mutex_init(&philo->stop_mutex, NULL);
return (philo);
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 14:38:14 by cchauvet #+# #+# */
/* Updated: 2023/04/25 14:05:15 by cchauvet ### ########.fr */
/* Updated: 2023/04/25 16:41:14 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,7 +20,7 @@
typedef struct s_philo
{
size_t id;
size_t id;
pthread_mutex_t nb_meal_mutex;
size_t nb_meal;
pthread_mutex_t last_eat_mutex;

114
philo/philo_routine.c Normal file
View File

@ -0,0 +1,114 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo_routine.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:44:14 by cchauvet #+# #+# */
/* Updated: 2023/06/01 14:23:53 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "./time.h"
#include "./philo.h"
#include "./print.h"
#include "data.h"
bool check(t_data *data)
{
bool stop;
if (data->nb_meals != -1 && get_min_meal(data) == (size_t) data->nb_meals)
{
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);
return (stop);
}
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))
{
pthread_mutex_unlock(&data->forks[philo->id]);
return (1);
}
while ((philo->id + 1) % data->nb_philos == philo->id)
{
if (check(data))
{
pthread_mutex_unlock(&data->forks[philo->id]);
return (1);
}
}
pthread_mutex_lock(&data->forks[(philo->id + 1) % data->nb_philos]);
print_take_a_fork(philo);
if (check(data))
{
pthread_mutex_unlock(&data->forks[(philo->id + 1) % data->nb_philos]);
pthread_mutex_unlock(&data->forks[philo->id]);
return (1);
}
return (0);
}
bool philo_eat(t_philo *philo, t_data *data)
{
if (philo_take_forks(philo, data))
return (1);
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(data))
return (1);
pthread_mutex_lock(&philo->nb_meal_mutex);
philo->nb_meal++;
pthread_mutex_unlock(&philo->nb_meal_mutex);
return (0);
}
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)
{
t_philo *philo;
t_data *data;
philo = arg;
data = philo->data;
print_thinking(philo);
usleep((philo->id % 2) * (get_time_eat(philo, data)) * 1000);
while (true)
{
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);
}

50
philo/philos.c Normal file
View File

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philos.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:30:32 by cchauvet #+# #+# */
/* Updated: 2023/04/27 11:30:33 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "./philo.h"
#include <stdbool.h>
#include <stddef.h>
int philos_init(t_data *data)
{
size_t i;
i = 0;
while (i < data->nb_philos)
{
data->philos[i] = philo_init(data);
if (data->philos[i] == NULL)
{
while (i > 0)
{
philo_destroyer(data->philos[i]);
i--;
}
return (1);
}
i++;
}
return (0);
}
void philos_destroyer(t_data *data)
{
size_t i;
i = 0;
while (i < data->nb_philos)
{
philo_destroyer(data->philos[i]);
i++;
}
free(data->philos);
}

21
philo/philos.h Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philos.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:33:17 by cchauvet #+# #+# */
/* Updated: 2023/04/27 11:33:17 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PHILOS_H
# define PHILOS_H
# include <stdbool.h>
# include "./data.h"
bool philos_init(t_data *data);
void philos_destroyer(t_data *data);
#endif

56
philo/print.c Normal file
View File

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include "philo.h"
#include <bits/pthreadtypes.h>
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include "./time.h"
#include "./utils/utils.h"
static void print(t_data *data, size_t id, char *str)
{
size_t time;
bool stop;
pthread_mutex_lock(&data->print_mutex);
pthread_mutex_lock(&data->stop_mutex);
stop = data->stop;
pthread_mutex_unlock(&data->stop_mutex);
if (stop == false)
{
time = get_time();
printf("%07zu %03zu %s\n", time, id + 1, str);
}
pthread_mutex_unlock(&data->print_mutex);
}
void print_take_a_fork(t_philo *philo)
{
print(philo->data, philo->id, "has taken a fork");
}
void print_eating(t_philo *philo)
{
print(philo->data, philo->id, "is eating");
}
void print_sleeping(t_philo *philo)
{
print(philo->data, philo->id, "is sleeping");
}
void print_thinking(t_philo *philo)
{
print(philo->data, philo->id, "is thinking");
}

23
philo/print.h Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:35:50 by cchauvet #+# #+# */
/* Updated: 2023/04/27 11:36:33 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PRINT_H
# define PRINT_H
# include "./philo.h"
void print_eating(t_philo *philo);
void print_take_a_fork(t_philo *philo);
void print_sleeping(t_philo *philo);
void print_thinking(t_philo *philo);
void print_died(t_philo *philo);
#endif

27
philo/print2.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:41:14 by cchauvet #+# #+# */
/* Updated: 2023/05/17 13:33:33 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
#include "./time.h"
#include "./utils/utils.h"
void print_died(t_philo *philo)
{
t_data *data;
size_t time;
data = philo->data;
time = get_time();
pthread_mutex_lock(&data->print_mutex);
printf("%07zu %03zu %s\n", time, philo->id + 1, "died");
pthread_mutex_unlock(&data->print_mutex);
}

33
philo/threads.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* threads.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:33:45 by cchauvet #+# #+# */
/* Updated: 2023/04/27 11:37:40 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
#include <pthread.h>
#include <stdbool.h>
#include <unistd.h>
#include <stddef.h>
bool threads_init(t_data *data)
{
size_t i;
i = 0;
while (i < data->nb_philos)
{
if (pthread_create(&data->threads[i], NULL, philo_routine,
data->philos[i]))
return (true);
pthread_detach(data->threads[i]);
i++;
}
return (false);
}

20
philo/threads.h Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* threads.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:37:07 by cchauvet #+# #+# */
/* Updated: 2023/04/27 11:37:08 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef THREADS_H
# define THREADS_H
# include "./data.h"
# include <stdbool.h>
bool threads_init(t_data *data);
#endif

51
philo/time.c Normal file
View File

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* time.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include <bits/types/struct_timeval.h>
#include <sys/time.h>
#include <stddef.h>
#include <pthread.h>
#include "./philo.h"
#include "data.h"
size_t get_time(void)
{
size_t time;
static size_t start_time = 0;
struct timeval tv;
gettimeofday(&tv, NULL);
time = tv.tv_sec * 1000000 + tv.tv_usec;
if (start_time == 0)
start_time = time;
return ((time - start_time) / 1000);
}
size_t get_time_eat(t_philo *philo, t_data *data)
{
size_t time;
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);
}
size_t get_time_sleep(t_philo *philo, t_data *data)
{
size_t time;
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);
}

23
philo/time.h Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* time.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 11:32:57 by cchauvet #+# #+# */
/* Updated: 2023/05/02 11:27:53 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef TIME_H
# define TIME_H
# include "philo.h"
# include "data.h"
# include <stddef.h>
size_t get_time(void);
size_t get_time_eat(t_philo *philo, t_data *data);
size_t get_time_sleep(t_philo *philo, t_data *data);
#endif

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 16:14:34 by cchauvet #+# #+# */
/* Updated: 2023/04/12 11:12:49 by cchauvet ### ########.fr */
/* Updated: 2023/04/27 11:27:44 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,7 +21,7 @@ int ft_atoi(const char *str)
sign = 1;
i = 0;
while (str[i] == '-' || str[i]== '+')
while (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
sign = -1;

View File

@ -1,38 +0,0 @@
#include "./philo.h"
#include <stdbool.h>
#include <stddef.h>
int philos_init(t_data *data)
{
size_t i;
i = 0;
while (i < data->nb_philos)
{
data->philos[i] = philo_init(data);
if (data->philos[i] == NULL)
{
while (i > 0)
{
philo_destroyer(data->philos[i]);
i--;
}
return (1);
}
i++;
}
return (0);
}
void philos_destroyer(t_data *data)
{
size_t i;
i = 0;
while (i < data->nb_philos)
{
philo_destroyer(data->philos[i]);
i++;
}
free(data->philos);
}

View File

@ -1,5 +0,0 @@
# include <stdbool.h>
# include "./data.h"
bool philos_init(t_data *data);
void philos_destroyer(t_data *data);

66
print.c
View File

@ -1,66 +0,0 @@
#include "philo.h"
#include <bits/pthreadtypes.h>
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include "./time.h"
#include "./utils/utils.h"
static void print(t_data *data, size_t id, char *str)
{
size_t time;
bool stop;
time = get_time();
pthread_mutex_lock(&data->print_mutex);
pthread_mutex_lock(&data->stop_mutex);
stop = data->stop;
pthread_mutex_unlock(&data->stop_mutex);
if (stop == false)
{
ft_putnum(time, 7);
ft_putchar(' ');
ft_putnum(id + 1, 3);
ft_putchar(' ');
ft_putstr(str);
ft_putchar('\n');
}
pthread_mutex_unlock(&data->print_mutex);
}
void print_take_a_fork(t_philo *philo)
{
print(philo->data, philo->id, "has taken a fork");
}
void print_eating(t_philo *philo)
{
print(philo->data, philo->id, "is eating");
}
void print_sleeping(t_philo *philo)
{
print(philo->data, philo->id, "is sleeping");
}
void print_thinking(t_philo *philo)
{
print(philo->data, philo->id, "is thinking");
}
void print_died(t_philo *philo)
{
t_data *data;
size_t time;
data = philo->data;
time = get_time();
pthread_mutex_lock(&data->print_mutex);
ft_putnum(time, 7);
ft_putchar(' ');
ft_putnum(philo->id + 1, 3);
ft_putchar(' ');
ft_putstr("died");
ft_putchar('\n');
pthread_mutex_unlock(&data->print_mutex);
}

View File

@ -1,7 +0,0 @@
# include "./philo.h"
void print_eating(t_philo *philo);
void print_take_a_fork(t_philo *philo);
void print_sleeping(t_philo *philo);
void print_thinking(t_philo *philo);
void print_died(t_philo *philo);

View File

@ -1,20 +0,0 @@
#include "philo.h"
#include <pthread.h>
#include <stdbool.h>
#include <unistd.h>
#include <stddef.h>
bool threads_init(t_data *data)
{
size_t i;
i = 0;
while (i < data->nb_philos)
{
if (pthread_create(&data->threads[i], NULL, philo_routine, data->philos[i]))
return (true);
pthread_detach(data->threads[i]);
i++;
}
return (false);
}

View File

@ -1,5 +0,0 @@
# include "./data.h"
# include <stdbool.h>
bool threads_init(t_data *data);

17
time.c
View File

@ -1,17 +0,0 @@
#include <bits/types/struct_timeval.h>
#include <sys/time.h>
#include <stddef.h>
#include <pthread.h>
size_t get_time(void)
{
size_t time;
static size_t start_time = 0;
struct timeval tv;
gettimeofday(&tv, NULL);
time = tv.tv_sec * 1000000 + tv.tv_usec;
if (start_time == 0)
start_time = time;
return ((time - start_time) / 1000);
}

3
time.h
View File

@ -1,3 +0,0 @@
# include <stddef.h>
size_t get_time(void);

View File

@ -1,6 +0,0 @@
#include <unistd.h>
void ft_putchar(char c)
{
write(1, &c, 1);
}

View File

@ -1,47 +0,0 @@
#include <stddef.h>
#include <unistd.h>
#include <stdlib.h>
#include "utils.h"
static size_t get_size(size_t num)
{
size_t nb_digit;
size_t rest;
nb_digit = 0;
if (num == 0)
nb_digit++;
rest = num;
while (rest != 0)
{
nb_digit++;
rest = rest / 10;
}
return (nb_digit);
}
static void ft_putdigit(size_t num)
{
if (num > 9)
{
ft_putdigit(num / 10);
ft_putchar(num % 10 + 48);
}
else
ft_putchar(num + 48);
}
void ft_putnum(size_t num, size_t padding)
{
size_t size;
size_t i;
i = 0;
size = get_size(num);
while (i < padding - size)
{
ft_putchar('0');
i++;
}
ft_putdigit(num);
}

View File

@ -1,14 +0,0 @@
#include "utils.h"
#include <stddef.h>
void ft_putstr(char *str)
{
size_t i;
i = 0;
while (str[i] != 0)
{
ft_putchar(str[i]);
i++;
}
}