bozosujet
This commit is contained in:
39
philo/Makefile
Normal file
39
philo/Makefile
Normal file
@ -0,0 +1,39 @@
|
||||
SRCS = parsing.c \
|
||||
utils/ft_isnum.c \
|
||||
utils/ft_atoi.c \
|
||||
main.c \
|
||||
time.c \
|
||||
print.c \
|
||||
print2.c \
|
||||
data.c \
|
||||
philo.c \
|
||||
philo_routine.c \
|
||||
philos.c \
|
||||
threads.c
|
||||
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
|
||||
NAME = philo
|
||||
|
||||
CC = clang
|
||||
|
||||
FLAGS = -Wall -Wextra -Werror -g -pthread
|
||||
|
||||
%.o: %.c
|
||||
${CC} ${FLAGS} -c -o $@ $<
|
||||
|
||||
${NAME}: ${OBJS}
|
||||
${CC} ${FLAGS} ${OBJS} -o ${NAME}
|
||||
|
||||
all: ${NAME}
|
||||
|
||||
clean:
|
||||
rm -f ${OBJS}
|
||||
|
||||
fclean: clean
|
||||
rm -f ${NAME}
|
||||
|
||||
re: fclean
|
||||
make all
|
||||
|
||||
.PHONY: all clean fclean re
|
79
philo/data.c
Normal file
79
philo/data.c
Normal file
@ -0,0 +1,79 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* data.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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>
|
||||
|
||||
bool data_init(t_data *data)
|
||||
{
|
||||
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)
|
||||
{
|
||||
free(data->philos);
|
||||
free(data->forks);
|
||||
return (1);
|
||||
}
|
||||
i = -1;
|
||||
while (++i < (ssize_t) data->nb_philos)
|
||||
pthread_mutex_init(&data->forks[i], NULL);
|
||||
pthread_mutex_init(&data->stop_mutex, NULL);
|
||||
pthread_mutex_init(&data->print_mutex, NULL);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void data_destroyer(t_data *data)
|
||||
{
|
||||
ssize_t i;
|
||||
t_philo *philo;
|
||||
bool stop;
|
||||
|
||||
i = 0;
|
||||
while (i < (ssize_t) data->nb_philos)
|
||||
{
|
||||
philo = data->philos[i];
|
||||
pthread_mutex_lock(&philo->stop_mutex);
|
||||
stop = philo->stop;
|
||||
pthread_mutex_unlock(&philo->stop_mutex);
|
||||
if (stop)
|
||||
i++;
|
||||
usleep(1000);
|
||||
}
|
||||
usleep(1000);
|
||||
i = -1;
|
||||
while (++i < (ssize_t) data->nb_philos)
|
||||
pthread_mutex_destroy(&data->forks[i]);
|
||||
pthread_mutex_destroy(&data->stop_mutex);
|
||||
pthread_mutex_destroy(&data->print_mutex);
|
||||
free(data->threads);
|
||||
free(data->forks);
|
||||
philos_destroyer(data);
|
||||
}
|
36
philo/data.h
Normal file
36
philo/data.h
Normal file
@ -0,0 +1,36 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* data.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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;
|
||||
size_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);
|
||||
size_t get_min_meal(t_data *data);
|
||||
#endif
|
120
philo/main.c
Normal file
120
philo/main.c
Normal file
@ -0,0 +1,120 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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);
|
||||
}
|
||||
if (get_min_meal(data) >= data->nb_meals)
|
||||
{
|
||||
stop(data);
|
||||
return (1);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void *check_routine(t_data *data)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (routine(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);
|
||||
}
|
62
philo/parsing.c
Normal file
62
philo/parsing.c
Normal file
@ -0,0 +1,62 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parsing.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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);
|
||||
}
|
19
philo/parsing.h
Normal file
19
philo/parsing.h
Normal 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
47
philo/philo.c
Normal 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);
|
||||
}
|
36
philo/philo.h
Normal file
36
philo/philo.h
Normal file
@ -0,0 +1,36 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* philo.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 14:38:14 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/04/25 16:41:14 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PHILO_H
|
||||
# define PHILO_H
|
||||
# include <pthread.h>
|
||||
# include <stdio.h>
|
||||
# include <stdbool.h>
|
||||
# include <stdlib.h>
|
||||
# include "./data.h"
|
||||
|
||||
typedef struct s_philo
|
||||
{
|
||||
size_t id;
|
||||
pthread_mutex_t nb_meal_mutex;
|
||||
size_t nb_meal;
|
||||
pthread_mutex_t last_eat_mutex;
|
||||
size_t last_eat;
|
||||
pthread_mutex_t stop_mutex;
|
||||
bool stop;
|
||||
t_data *data;
|
||||
} t_philo;
|
||||
|
||||
t_philo *philo_init(t_data *data);
|
||||
void philo_destroyer(t_philo *philo);
|
||||
void *philo_routine(void *arg);
|
||||
#endif
|
118
philo/philo_routine.c
Normal file
118
philo/philo_routine.c
Normal file
@ -0,0 +1,118 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* philo_routine.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <unistd.h>
|
||||
#include "./time.h"
|
||||
#include "./philo.h"
|
||||
#include "./print.h"
|
||||
#include "data.h"
|
||||
|
||||
bool check(t_philo *philo, t_data *data)
|
||||
{
|
||||
bool stop;
|
||||
|
||||
if (get_min_meal(data) == 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);
|
||||
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);
|
||||
}
|
||||
|
||||
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))
|
||||
{
|
||||
pthread_mutex_unlock(&data->forks[philo->id]);
|
||||
return (1);
|
||||
}
|
||||
while ((philo->id + 1) % data->nb_philos == philo->id)
|
||||
{
|
||||
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);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
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);
|
||||
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))
|
||||
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)
|
||||
{
|
||||
print_sleeping(philo);
|
||||
usleep(get_time_sleep(philo, data) * 1000);
|
||||
}
|
||||
|
||||
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(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);
|
||||
}
|
50
philo/philos.c
Normal file
50
philo/philos.c
Normal 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
21
philo/philos.h
Normal 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
56
philo/print.c
Normal 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/17 13:22:19 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("%zu %zu %s\n", time, id, 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
23
philo/print.h
Normal 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
27
philo/print2.c
Normal 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:21: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("%7zu %3zu %s\n", time, philo->id, "died");
|
||||
pthread_mutex_unlock(&data->print_mutex);
|
||||
}
|
33
philo/threads.c
Normal file
33
philo/threads.c
Normal 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
20
philo/threads.h
Normal 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
|
55
philo/time.c
Normal file
55
philo/time.c
Normal file
@ -0,0 +1,55 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* time.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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;
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
23
philo/time.h
Normal file
23
philo/time.h
Normal 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
|
39
philo/utils/ft_atoi.c
Normal file
39
philo/utils/ft_atoi.c
Normal file
@ -0,0 +1,39 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_atoi.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/09/27 16:14:34 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/04/27 11:27:44 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "utils.h"
|
||||
#include <stddef.h>
|
||||
|
||||
int ft_atoi(const char *str)
|
||||
{
|
||||
int out;
|
||||
char sign;
|
||||
size_t i;
|
||||
|
||||
sign = 1;
|
||||
i = 0;
|
||||
while (str[i] == '-' || str[i] == '+')
|
||||
{
|
||||
if (str[i] == '-')
|
||||
sign = -1;
|
||||
if (str[i] == '+')
|
||||
sign = 1;
|
||||
i++;
|
||||
}
|
||||
out = 0;
|
||||
while (str[i] >= '0' && str[i] <= '9')
|
||||
{
|
||||
out = out * 10 + str[i] - 48;
|
||||
i++;
|
||||
}
|
||||
return (out * sign);
|
||||
}
|
34
philo/utils/ft_isnum.c
Normal file
34
philo/utils/ft_isnum.c
Normal file
@ -0,0 +1,34 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_isnum.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 14:43:33 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/04/19 13:49:58 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../philo.h"
|
||||
#include <stddef.h>
|
||||
#include <limits.h>
|
||||
|
||||
bool ft_isnum(char str[])
|
||||
{
|
||||
size_t i;
|
||||
size_t num;
|
||||
|
||||
num = 0;
|
||||
i = 0;
|
||||
while (str[i] != '\0')
|
||||
{
|
||||
if (str[i] > '9' || str[i] < '0')
|
||||
return (0);
|
||||
num = num * 10 + str[i] - 48;
|
||||
if (num > INT_MAX)
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
24
philo/utils/utils.h
Normal file
24
philo/utils/utils.h
Normal file
@ -0,0 +1,24 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/11 14:38:07 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/04/25 14:09:53 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef UTILS_H
|
||||
# define UTILS_H
|
||||
# include <stddef.h>
|
||||
# include <stdbool.h>
|
||||
|
||||
bool ft_isnum(char str[]);
|
||||
int ft_atoi(const char str[]);
|
||||
void ft_putchar(char c);
|
||||
void ft_putstr(char str[]);
|
||||
void ft_putnum(size_t num, size_t padding);
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user