Finish thx to garfi the goat
This commit is contained in:
parent
d948c639ec
commit
501b45d763
7 changed files with 31 additions and 8 deletions
5
philo/.clangd
Normal file
5
philo/.clangd
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
CompilerFlags:
|
||||||
|
Add:
|
||||||
|
- "-xc"
|
||||||
|
- "-I/usr/include"
|
||||||
|
- "-I/home/rparodi/Documents/philo-perso/philo/includes"
|
||||||
5
philo/.vscode/settings.json
vendored
Normal file
5
philo/.vscode/settings.json
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"philo.h": "c"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
|
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
|
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
|
||||||
# Updated: 2024/07/04 15:20:22 by rparodi ### ########.fr #
|
# Updated: 2024/07/18 18:51:45 by rparodi ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
|
@ -23,6 +23,8 @@ RM = rm -rf
|
||||||
# Flags
|
# Flags
|
||||||
CFLAGS = -Werror -Wextra -Wall -Wno-unused-command-line-argument -g3 -MMD
|
CFLAGS = -Werror -Wextra -Wall -Wno-unused-command-line-argument -g3 -MMD
|
||||||
|
|
||||||
|
# CFLAGS += -fsanitize=thread
|
||||||
|
|
||||||
INCLUDES = ./includes/philo.h \
|
INCLUDES = ./includes/philo.h \
|
||||||
./includes/types.h
|
./includes/types.h
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,12 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/06/10 00:23:09 by rparodi #+# #+# */
|
/* Created: 2024/06/10 00:23:09 by rparodi #+# #+# */
|
||||||
/* Updated: 2024/07/18 18:42:02 by rparodi ### ########.fr */
|
/* Updated: 2024/07/18 19:29:14 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../includes/philo.h"
|
#include "../includes/philo.h"
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
void ft_logs(t_str msg, t_philo *philo)
|
void ft_logs(t_str msg, t_philo *philo)
|
||||||
{
|
{
|
||||||
|
|
@ -47,6 +48,8 @@ t_error ft_start_eating(t_philo *philo)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(philo->l_fork);
|
pthread_mutex_lock(philo->l_fork);
|
||||||
ft_logs("has taken a fork\n", philo);
|
ft_logs("has taken a fork\n", philo);
|
||||||
|
if (philo->nb_philo == 1)
|
||||||
|
return ((void)pthread_mutex_unlock(philo->l_fork), NO_ERROR);
|
||||||
pthread_mutex_lock(philo->r_fork);
|
pthread_mutex_lock(philo->r_fork);
|
||||||
ft_logs("has taken a fork\n", philo);
|
ft_logs("has taken a fork\n", philo);
|
||||||
}
|
}
|
||||||
|
|
@ -59,7 +62,9 @@ t_error ft_ending_eating(t_philo *philo)
|
||||||
|
|
||||||
philo->eating = true;
|
philo->eating = true;
|
||||||
ft_logs("is eating\n", philo);
|
ft_logs("is eating\n", philo);
|
||||||
|
pthread_mutex_lock(philo->meal_lock);
|
||||||
philo->t_last_eat = time;
|
philo->t_last_eat = time;
|
||||||
|
pthread_mutex_unlock(philo->meal_lock);
|
||||||
pthread_mutex_lock(philo->check_eating_count);
|
pthread_mutex_lock(philo->check_eating_count);
|
||||||
philo->eating_count++;
|
philo->eating_count++;
|
||||||
pthread_mutex_unlock(philo->check_eating_count);
|
pthread_mutex_unlock(philo->check_eating_count);
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/06/04 11:30:25 by rparodi #+# #+# */
|
/* Created: 2024/06/04 11:30:25 by rparodi #+# #+# */
|
||||||
/* Updated: 2024/07/11 17:34:21 by rparodi ### ########.fr */
|
/* Updated: 2024/07/18 19:59:24 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -48,7 +48,7 @@ t_error ft_init_thread(t_program *prog, t_mutex *forks)
|
||||||
ERROR, prog, forks);
|
ERROR, prog, forks);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
while (--i > 0)
|
while (i-- > 0)
|
||||||
pthread_join(prog->philos[i].thread, NULL);
|
pthread_join(prog->philos[i].thread, NULL);
|
||||||
pthread_join(watch_dogs, NULL);
|
pthread_join(watch_dogs, NULL);
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/06/04 11:42:37 by rparodi #+# #+# */
|
/* Created: 2024/06/04 11:42:37 by rparodi #+# #+# */
|
||||||
/* Updated: 2024/07/18 18:37:43 by rparodi ### ########.fr */
|
/* Updated: 2024/07/18 20:00:09 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -17,6 +17,9 @@ t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo)
|
||||||
t_usize tmp;
|
t_usize tmp;
|
||||||
|
|
||||||
tmp = 0;
|
tmp = 0;
|
||||||
|
ft_atou(argv[1], &tmp);
|
||||||
|
if (tmp == 0)
|
||||||
|
return (ERROR);
|
||||||
if (argc == 6)
|
if (argc == 6)
|
||||||
{
|
{
|
||||||
ft_atou(argv[5], &tmp);
|
ft_atou(argv[5], &tmp);
|
||||||
|
|
@ -24,7 +27,6 @@ t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
philo->nb_eat = -1;
|
philo->nb_eat = -1;
|
||||||
ft_atou(argv[1], &tmp);
|
|
||||||
philo->nb_philo = tmp;
|
philo->nb_philo = tmp;
|
||||||
ft_atou(argv[2], &tmp);
|
ft_atou(argv[2], &tmp);
|
||||||
philo->t_death = tmp;
|
philo->t_death = tmp;
|
||||||
|
|
@ -65,6 +67,7 @@ t_error ft_init_philo(t_i32 argc, t_str *argv, t_program *prog, t_mutex *forks)
|
||||||
{
|
{
|
||||||
prog->philos[i].id = i + 1;
|
prog->philos[i].id = i + 1;
|
||||||
prog->philos[i].eating = 0;
|
prog->philos[i].eating = 0;
|
||||||
|
prog->philos[i].eating_count = 0;
|
||||||
ft_parsing_args(argc, argv, &prog->philos[i]);
|
ft_parsing_args(argc, argv, &prog->philos[i]);
|
||||||
prog->philos[i].start_time = ft_time();
|
prog->philos[i].start_time = ft_time();
|
||||||
prog->philos[i].t_last_eat = 0;
|
prog->philos[i].t_last_eat = 0;
|
||||||
|
|
|
||||||
|
|
@ -29,15 +29,18 @@ t_bool check_dead(t_philo *philo)
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < philo[0].nb_philo)
|
while (i < philo[0].nb_philo)
|
||||||
{
|
{
|
||||||
|
pthread_mutex_lock(philo->meal_lock);
|
||||||
timecheck = ft_time() - philo[i].start_time - philo[i].t_last_eat;
|
timecheck = ft_time() - philo[i].start_time - philo[i].t_last_eat;
|
||||||
if (philo[i].t_last_eat != 0 && timecheck >= philo[i].t_death)
|
if (timecheck >= philo[i].t_death)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(philo[i].dead_lock);
|
pthread_mutex_unlock(philo->meal_lock);
|
||||||
ft_logs("died\n", &philo[i]);
|
ft_logs("died\n", &philo[i]);
|
||||||
|
pthread_mutex_lock(philo[i].dead_lock);
|
||||||
philo[i].dead_check = true;
|
philo[i].dead_check = true;
|
||||||
pthread_mutex_unlock(philo[i].dead_lock);
|
pthread_mutex_unlock(philo[i].dead_lock);
|
||||||
return (true);
|
return (true);
|
||||||
}
|
}
|
||||||
|
pthread_mutex_unlock(philo->meal_lock);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (false);
|
return (false);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue