Adding something working
This commit is contained in:
parent
af63bafed0
commit
5215c0e3f4
4 changed files with 50 additions and 31 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
/* 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/04 11:48:33 by rparodi ### ########.fr */
|
/* Updated: 2024/07/07 20:00:00 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -14,12 +14,15 @@
|
||||||
|
|
||||||
void ft_logs(t_str msg, t_philo *philo)
|
void ft_logs(t_str msg, t_philo *philo)
|
||||||
{
|
{
|
||||||
|
pthread_mutex_lock(philo->print_lock);
|
||||||
const t_usize time = ft_time() - philo->start_time;
|
const t_usize time = ft_time() - philo->start_time;
|
||||||
|
|
||||||
if (msg != NULL)
|
if (msg != NULL)
|
||||||
|
{
|
||||||
printf("%s%zu %s%i %s%s%s", BLUE, time, GOLD, philo->id, \
|
printf("%s%zu %s%i %s%s%s", BLUE, time, GOLD, philo->id, \
|
||||||
GREEN, msg, END);
|
GREEN, msg, END);
|
||||||
}
|
}
|
||||||
|
pthread_mutex_unlock(philo->print_lock);
|
||||||
|
}
|
||||||
|
|
||||||
t_error ft_thinking(t_philo *philo)
|
t_error ft_thinking(t_philo *philo)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -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/04 15:27:12 by rparodi ### ########.fr */
|
/* Updated: 2024/07/11 17:34:21 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -19,6 +19,9 @@ void *ft_routine(void *ptr)
|
||||||
philo = (t_philo *) ptr;
|
philo = (t_philo *) ptr;
|
||||||
if (philo == NULL)
|
if (philo == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
pthread_mutex_lock(philo->print_lock);
|
||||||
|
printf("\n\nEntre dans ft_routine\n\n");
|
||||||
|
pthread_mutex_unlock(philo->print_lock);
|
||||||
while (!dead_loop(philo))
|
while (!dead_loop(philo))
|
||||||
{
|
{
|
||||||
ft_start_eating(philo);
|
ft_start_eating(philo);
|
||||||
|
|
@ -30,11 +33,11 @@ void *ft_routine(void *ptr)
|
||||||
|
|
||||||
t_error ft_init_thread(t_program *prog, t_mutex *forks)
|
t_error ft_init_thread(t_program *prog, t_mutex *forks)
|
||||||
{
|
{
|
||||||
t_thread o_block;
|
t_thread watch_dogs;
|
||||||
t_usize i;
|
t_usize i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
if (pthread_create(&o_block, NULL, &ft_watch_dogs, prog->philos))
|
if (pthread_create(&watch_dogs, NULL, &ft_watch_dogs, prog->philos))
|
||||||
ft_destroy_exit("Allocation of watch_dogs failed\n", \
|
ft_destroy_exit("Allocation of watch_dogs failed\n", \
|
||||||
ERROR, prog, forks);
|
ERROR, prog, forks);
|
||||||
while (i < prog->philos[0].nb_philo)
|
while (i < prog->philos[0].nb_philo)
|
||||||
|
|
@ -47,7 +50,7 @@ t_error ft_init_thread(t_program *prog, t_mutex *forks)
|
||||||
}
|
}
|
||||||
while (--i > 0)
|
while (--i > 0)
|
||||||
pthread_join(prog->philos[i].thread, NULL);
|
pthread_join(prog->philos[i].thread, NULL);
|
||||||
pthread_join(o_block, NULL);
|
pthread_join(watch_dogs, NULL);
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -64,9 +67,23 @@ t_i32 main(t_i32 argc, t_str *argv)
|
||||||
{
|
{
|
||||||
t_philo philo[MAXSUPPORT];
|
t_philo philo[MAXSUPPORT];
|
||||||
t_program prog;
|
t_program prog;
|
||||||
|
t_i32 i;
|
||||||
|
t_i32 j;
|
||||||
|
|
||||||
|
j = 1;
|
||||||
if (argc != 6 && argc != 5)
|
if (argc != 6 && argc != 5)
|
||||||
ft_exit(ARGS, 1);
|
ft_exit(ARGS, 1);
|
||||||
|
while (j < argc)
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
while (argv[j][i] != '\0')
|
||||||
|
{
|
||||||
|
if (argv[j][i] < '0' || argv[j][i] > '9')
|
||||||
|
ft_exit("I have to take only numeric arguments !\n", 1);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
j++;
|
||||||
|
}
|
||||||
if (ft_atou_return(argv[1]) > MAXSUPPORT)
|
if (ft_atou_return(argv[1]) > MAXSUPPORT)
|
||||||
ft_exit("Please update the max support !\n", 1);
|
ft_exit("Please update the max support !\n", 1);
|
||||||
if (ft_init(argc, argv, &prog, philo))
|
if (ft_init(argc, argv, &prog, philo))
|
||||||
|
|
|
||||||
|
|
@ -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/04 11:52:27 by rparodi ### ########.fr */
|
/* Updated: 2024/07/11 17:31:53 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -23,7 +23,7 @@ t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo)
|
||||||
philo->nb_eat = tmp;
|
philo->nb_eat = tmp;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
philo->t_eat = -1;
|
philo->t_eat = ~0;
|
||||||
ft_atou(argv[1], &tmp);
|
ft_atou(argv[1], &tmp);
|
||||||
philo->nb_philo = tmp;
|
philo->nb_philo = tmp;
|
||||||
ft_atou(argv[2], &tmp);
|
ft_atou(argv[2], &tmp);
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/06/11 11:42:31 by rparodi #+# #+# */
|
/* Created: 2024/06/11 11:42:31 by rparodi #+# #+# */
|
||||||
/* Updated: 2024/07/04 13:29:39 by rparodi ### ########.fr */
|
/* Updated: 2024/07/11 18:10:49 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -32,10 +32,10 @@ t_bool check_dead(t_philo *philo)
|
||||||
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 (philo[i].t_last_eat != 0 && timecheck >= philo[i].t_death)
|
||||||
{
|
{
|
||||||
pthread_mutex_unlock(philo[i].dead_lock);
|
pthread_mutex_lock(philo[i].dead_lock);
|
||||||
ft_logs("died\n", &philo[i]);
|
ft_logs("died\n", &philo[i]);
|
||||||
philo[i].dead_check = true;
|
philo[i].dead_check = true;
|
||||||
pthread_mutex_lock(philo[i].dead_lock);
|
pthread_mutex_unlock(philo[i].dead_lock);
|
||||||
return (true);
|
return (true);
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
|
|
@ -60,37 +60,27 @@ void counting_launch(t_philo *philo, t_u8 *counter)
|
||||||
|
|
||||||
t_bool check_eat(t_philo *philo)
|
t_bool check_eat(t_philo *philo)
|
||||||
{
|
{
|
||||||
t_usize i;
|
|
||||||
t_u8 check;
|
t_u8 check;
|
||||||
|
|
||||||
i = 0;
|
|
||||||
check = 0;
|
check = 0;
|
||||||
if (philo[0].nb_eat == -1)
|
|
||||||
return (false);
|
|
||||||
counting_launch(philo, &check);
|
counting_launch(philo, &check);
|
||||||
if (check == philo[0].nb_philo)
|
if (check == philo[0].nb_philo)
|
||||||
{
|
|
||||||
pthread_mutex_lock(philo[0].dead_lock);
|
|
||||||
while (i < philo[0].nb_philo)
|
|
||||||
{
|
|
||||||
pthread_mutex_unlock(philo[i].dead_lock);
|
|
||||||
philo[i].dead_check = true;
|
|
||||||
pthread_mutex_lock(philo[i].dead_lock);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
pthread_mutex_unlock(philo[0].dead_lock);
|
|
||||||
return (true);
|
return (true);
|
||||||
}
|
|
||||||
return (false);
|
return (false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *ft_watch_dogs(void *ptr)
|
void *ft_watch_dogs(void *ptr)
|
||||||
{
|
{
|
||||||
t_philo *philo;
|
t_philo *philo;
|
||||||
|
t_usize i;
|
||||||
|
|
||||||
philo = (t_philo *) ptr;
|
philo = (t_philo *) ptr;
|
||||||
if (philo == NULL)
|
if (philo == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
i = philo[0].nb_philo;
|
||||||
|
pthread_mutex_lock(philo->print_lock);
|
||||||
|
printf("\n\nEntre dans ft_watch_dogs\n\n");
|
||||||
|
pthread_mutex_unlock(philo->print_lock);
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (check_eat(philo) == true)
|
if (check_eat(philo) == true)
|
||||||
|
|
@ -98,5 +88,14 @@ void *ft_watch_dogs(void *ptr)
|
||||||
if (check_dead(philo) == true)
|
if (check_dead(philo) == true)
|
||||||
break ;
|
break ;
|
||||||
}
|
}
|
||||||
|
pthread_mutex_lock(philo->print_lock);
|
||||||
|
printf("\n\nSortie de ft_watch_dogs\n\n");
|
||||||
|
pthread_mutex_unlock(philo->print_lock);
|
||||||
|
while (i-- != 0)
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(philo[i].dead_lock);
|
||||||
|
philo[i].dead_check = true;
|
||||||
|
pthread_mutex_unlock(philo[i].dead_lock);
|
||||||
|
}
|
||||||
return (philo);
|
return (philo);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue