Add files via upload

This commit is contained in:
Raphaël 2024-06-25 17:04:47 +02:00 committed by GitHub
commit cde3e458fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 806 additions and 0 deletions

View file

@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_actions.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/10 00:23:09 by rparodi #+# #+# */
/* Updated: 2024/06/18 15:50:49 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/philo.h"
void ft_logs(t_str msg, t_philo *philo)
{
const t_usize time = ft_time() - philo->start_time;
if (msg != NULL)
printf("%s%zu %s%i %s%s%s", BLUE, time, GOLD, philo->id, GREEN, msg, END);
}
t_error ft_thinking(t_philo *philo)
{
ft_logs("is thinking\n", philo);
return (NO_ERROR);
}
t_error ft_start_eating(t_philo *philo)
{
const t_usize time = ft_time() - philo->start_time;
if (philo->id % 2 == 0)
{
pthread_mutex_lock(philo->l_fork);
ft_logs("has taken a fork\n", philo);
pthread_mutex_lock(philo->r_fork);
ft_logs("has taken a fork\n", philo);
}
else
{
pthread_mutex_lock(philo->r_fork);
ft_logs("has taken a fork\n", philo);
pthread_mutex_lock(philo->l_fork);
ft_logs("has taken a fork\n", philo);
}
philo->eating = true;
ft_logs("is eating\n", philo);
philo->t_last_eat = time;
philo->eating_count++;
ft_pause(EATTIME * 1000);
pthread_mutex_unlock(philo->r_fork);
pthread_mutex_unlock(philo->l_fork);
philo->eating = false;
return (NO_ERROR);
}
t_error ft_sleeping(t_philo *philo)
{
ft_logs("is sleeping\n", philo);
ft_pause(philo->t_sleep);
return (NO_ERROR);
}

64
philo/sources/ft_atou.c Normal file
View file

@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atou.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/09 16:27:52 by rparodi #+# #+# */
/* Updated: 2024/06/09 18:27:02 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/philo.h"
static t_i32 ft_check_space(t_i32 c)
{
if (c == 32 || (c >= 9 && c <= 13))
return (1);
return (0);
}
static t_i32 ft_check_sign(const char *nptr, t_i32 *i)
{
while (ft_check_space(nptr[*i]))
(*i)++;
if (nptr[*i] == '-')
{
(*i)++;
return (-1);
}
else if (nptr[*i] == '+')
(*i)++;
return (1);
}
t_error ft_atou(const char *nptr, t_usize *value)
{
t_i32 i;
t_i32 sign;
i = 0;
sign = ft_check_sign(nptr, &i);
(*value) = 0;
if (sign != 1)
ft_exit("Negative number if find !\n", ERROR);
while (nptr[i])
{
if (nptr[i] >= '0' && nptr[i] <= '9')
(*value) = ((*value) * 10) + nptr[i] - '0';
else
break ;
i++;
}
return (NO_ERROR);
}
t_usize ft_atou_return(t_str str)
{
t_usize number;
number = 0;
ft_atou(str, &number);
return (number);
}

60
philo/sources/ft_exit.c Normal file
View file

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 12:23:50 by rparodi #+# #+# */
/* Updated: 2024/06/17 12:38:01 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/philo.h"
t_usize ft_strlen(t_str str)
{
t_usize i;
i = 0;
while (str[i] != '\0')
i++;
return (i);
}
void ft_putchar_fd(t_i32 fd, t_str str)
{
if (str != NULL)
write(fd, str, ft_strlen(str));
}
void ft_free(void *ptr)
{
if (ptr != NULL)
{
free(ptr);
ptr = NULL;
}
}
void ft_exit(t_str msg, t_u8 status)
{
ft_putchar_fd(status + 1, msg);
exit(status);
}
void ft_destroy_exit(t_str msg, t_u8 status, t_program *prog, t_mutex *forks)
{
t_usize i;
i = 0;
pthread_mutex_destroy(&prog->print_lock);
pthread_mutex_destroy(&prog->meal_lock);
pthread_mutex_destroy(&prog->dead_lock);
while (i < prog->philos[0].nb_philo)
{
pthread_mutex_destroy(&forks[i]);
i++;
}
ft_exit(msg, status);
}

25
philo/sources/main.c Normal file
View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 11:30:25 by rparodi #+# #+# */
/* Updated: 2024/06/16 12:22:40 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/philo.h"
t_i32 main(t_i32 argc, t_str *argv)
{
t_philo philo[MAXSUPPORT];
t_program prog;
if (argc != 6 && argc != 5)
ft_exit(ARGS, 1);
if (ft_init(argc, argv, &prog, philo))
ft_exit("Issues during initialisation\n", ERROR);
return (0);
}

View file

@ -0,0 +1,111 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 11:42:37 by rparodi #+# #+# */
/* Updated: 2024/06/18 15:09:06 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/philo.h"
t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo)
{
t_usize tmp;
tmp = 0;
if (argc == 6)
{
ft_atou(argv[5], &tmp);
philo->nb_eat = tmp;
}
else
philo->t_eat = -1;
ft_atou(argv[1], &tmp);
philo->nb_philo = tmp;
ft_atou(argv[2], &tmp);
philo->t_death = tmp;
ft_atou(argv[3], &tmp);
philo->t_eat = tmp;
ft_atou(argv[4], &tmp);
philo->t_sleep = tmp;
return (NO_ERROR);
}
t_error ft_init_fork(t_philo *philo, t_mutex *fork)
{
t_usize i;
i = 0;
while (i < philo->nb_philo)
{
pthread_mutex_init(&fork[i], NULL);
i++;
}
return (NO_ERROR);
}
t_usize ft_time(void)
{
struct timeval time;
if (gettimeofday(&time, NULL) == -1)
ft_exit("Error of during ft_time !\n", 1);
return (time.tv_sec * 1000 + time.tv_usec / 1000);
}
void ft_pause(size_t milliseconds)
{
const t_usize start = ft_time();
while ((ft_time() - start) < milliseconds)
usleep(500);
}
t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks)
{
t_usize i;
i = 0;
while (i < ft_atou_return(argv[1]))
{
prog->philos[i].id = i + 1;
prog->philos[i].eating = 0;
ft_parsing_args(argc, argv, &prog->philos[i]);
prog->philos[i].start_time = ft_time();
prog->philos[i].t_last_eat = ft_time();
prog->philos[i].print_lock = &prog->print_lock;
prog->philos[i].dead_lock = &prog->dead_lock;
prog->philos[i].meal_lock = &prog->meal_lock;
prog->philos[i].dead_check = &prog->dead_flag;
prog->philos[i].l_fork = &forks[i];
if (i == 0)
prog->philos[i].r_fork = &forks[prog->philos[i].nb_philo - 1];
else
prog->philos[i].r_fork = &forks[i - 1];
i++;
}
if (ft_parsing_args(argc, argv, prog->philos))
return (ERROR);
return (NO_ERROR);
}
t_error ft_init(t_i32 argc, t_str *argv, t_program *prog, t_philo *philo)
{
static t_mutex forks[MAXSUPPORT] = { 0 };
pthread_mutex_init(&prog->print_lock, NULL);
pthread_mutex_init(&prog->dead_lock, NULL);
pthread_mutex_init(&prog->meal_lock, NULL);
prog->philos = philo;
prog->dead_flag = 0;
if (ft_init_philo(argc, argv, prog, forks))
return (ERROR);
if (philo->nb_philo != 0 && philo->nb_philo > MAXSUPPORT)
return (ERROR);
ft_init_thread(prog, forks);
return (NO_ERROR);
}

112
philo/sources/routine.c Normal file
View file

@ -0,0 +1,112 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* routine.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 11:42:31 by rparodi #+# #+# */
/* Updated: 2024/06/20 15:04:13 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/philo.h"
t_bool dead_loop(t_philo *philo)
{
pthread_mutex_lock(philo->dead_lock);
if (*philo->dead_check == true)
return (pthread_mutex_unlock(philo->dead_lock), true);
pthread_mutex_unlock(philo->dead_lock);
return (false);
}
t_bool check_dead(t_philo *philo)
{
t_usize i;
i = 0;
while (i < philo[0].nb_philo)
{
if (ft_time() - philo[i].t_last_eat > philo[i].t_eat)
return (true);
}
i++;
return (false);
}
t_bool check_eat(t_philo *philo)
{
t_usize i;
t_u8 check;
i = 0;
check = 0;
if (philo[0].nb_eat == -1)
return (false);
while (i < philo[0].nb_philo)
{
if (philo[i].eating_count >= philo[i].nb_eat)
check++;
i++;
}
if (check == philo[0].nb_philo)
return (true);
return (false);
}
void *ft_watch_dogs(void *ptr)
{
t_philo *philo;
philo = (t_philo *) ptr;
if (philo == NULL)
return (NULL);
while (true)
{
if (check_dead(philo))
break ;
if (check_eat(philo))
break ;
}
return (philo);
}
void *ft_routine(void *ptr)
{
t_philo *philo;
philo = (t_philo *) ptr;
if (philo == NULL)
return (NULL);
while (!dead_loop(philo))
{
ft_start_eating(philo);
ft_sleeping(philo);
ft_thinking(philo);
}
return (philo);
}
t_error ft_init_thread(t_program *prog, t_mutex *forks)
{
t_thread o_block;
t_usize i;
i = 0;
if (pthread_create(&o_block, NULL, &ft_watch_dogs, prog->philos))
ft_destroy_exit("Allocation of watch_dogs failed\n", \
ERROR, prog, forks);
while (i < prog->philos[0].nb_philo)
{
if (pthread_create(&prog->philos[i].thread, NULL, &ft_routine, \
&prog->philos[i]))
ft_destroy_exit("Allocation of one philo thread failed !\n", \
ERROR, prog, forks);
i++;
}
pthread_join(o_block, NULL);
while (--i > 0)
pthread_join(prog->philos[i].thread, NULL);
return (NO_ERROR);
}