Adding some stuff
This commit is contained in:
parent
3d145fb1e3
commit
c2db9acc15
4 changed files with 16 additions and 97 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/04 11:30:25 by rparodi #+# #+# */
|
||||
/* Updated: 2024/07/18 21:07:01 by rparodi ### ########.fr */
|
||||
/* Updated: 2024/07/23 19:51:07 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -21,8 +21,11 @@ void *ft_routine(void *ptr)
|
|||
return (NULL);
|
||||
while (!dead_loop(philo))
|
||||
{
|
||||
if (check_dead(philo) == false)
|
||||
ft_start_eating(philo);
|
||||
if (check_dead(philo) == false)
|
||||
ft_sleeping(philo);
|
||||
if (check_dead(philo) == false)
|
||||
ft_thinking(philo);
|
||||
}
|
||||
return (philo);
|
||||
|
|
|
|||
|
|
@ -6,11 +6,12 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/04 11:42:37 by rparodi #+# #+# */
|
||||
/* Updated: 2024/07/18 21:18:10 by rparodi ### ########.fr */
|
||||
/* Updated: 2024/07/23 20:03:47 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/philo.h"
|
||||
#include <pthread.h>
|
||||
|
||||
t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo)
|
||||
{
|
||||
|
|
@ -28,7 +29,7 @@ t_error ft_parsing_args(t_i32 argc, t_str *argv, t_philo *philo)
|
|||
philo->nb_eat = tmp;
|
||||
}
|
||||
else
|
||||
philo->nb_eat = -1;
|
||||
philo->nb_eat = ~0u;
|
||||
philo->nb_philo = tmp;
|
||||
ft_atou(argv[2], &tmp);
|
||||
philo->t_death = tmp;
|
||||
|
|
@ -92,10 +93,11 @@ 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);
|
||||
pthread_mutex_init(&prog->check_eating_count, NULL);
|
||||
prog->print_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER;
|
||||
prog->dead_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER;
|
||||
prog->meal_lock = (t_mutex)PTHREAD_MUTEX_INITIALIZER;
|
||||
prog->check_eating_count = (t_mutex)PTHREAD_MUTEX_INITIALIZER;
|
||||
ft_init_fork(prog->philos, forks);
|
||||
prog->philos = philo;
|
||||
prog->dead_flag = false;
|
||||
if (ft_init_philo(argc, argv, prog, forks))
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/11 11:42:31 by rparodi #+# #+# */
|
||||
/* Updated: 2024/07/18 21:18:49 by rparodi ### ########.fr */
|
||||
/* Updated: 2024/07/23 19:49:21 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -51,8 +51,6 @@ void counting_launch(t_philo *philo, t_u8 *counter)
|
|||
t_usize i;
|
||||
|
||||
i = 0;
|
||||
if (philo->nb_eat == -1)
|
||||
return ;
|
||||
while (i < philo[0].nb_philo)
|
||||
{
|
||||
pthread_mutex_lock(philo[i].check_eating_count);
|
||||
|
|
|
|||
84
philo/test.c
84
philo/test.c
|
|
@ -1,84 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* test.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/09 15:08:23 by rparodi #+# #+# */
|
||||
/* Updated: 2024/06/17 12:24:16 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/_types/_null.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// printf derriere un mutex pour le printf
|
||||
// zero printf apres une mort
|
||||
// mort shared avec un mutex
|
||||
// compte le nombre de ois manger par philo, et chec via un thread si tous a manger X fois
|
||||
|
||||
pthread_t philosopher[15];
|
||||
pthread_mutex_t chopstick[15];
|
||||
|
||||
int ft_sleep(size_t milliseconds)
|
||||
{
|
||||
struct timeval start;
|
||||
struct timeval tempo;
|
||||
|
||||
gettimeofday(&tempo, NULL);
|
||||
gettimeofday(&start, NULL);
|
||||
while ((tempo - start) < milliseconds)
|
||||
{
|
||||
usleep(500);
|
||||
gettimeofday(&tempo, NULL);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void *func(void *arg) {
|
||||
int n = *((int *)arg);
|
||||
printf("Philosopher %d is thinking\n", n);
|
||||
|
||||
pthread_mutex_lock(&chopstick[n]);
|
||||
pthread_mutex_lock(&chopstick[(n + 1) % 15]);
|
||||
printf("Philosopher %d is eating\n", n);
|
||||
ft_sleep(3000000); // sleep for 3 seconds to simulate eating
|
||||
pthread_mutex_unlock(&chopstick[n]);
|
||||
pthread_mutex_unlock(&chopstick[(n + 1) % 15]);
|
||||
printf("Philosopher %d finished eating\n", n);
|
||||
|
||||
free(arg); // Free the allocated memory
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 15; i++)
|
||||
pthread_mutex_init(&chopstick[i], NULL);
|
||||
|
||||
for (i = 0; i < 15; i++) {
|
||||
int *arg = malloc(sizeof(*arg));
|
||||
if (arg == NULL) {
|
||||
fprintf(stderr, "Couldn't allocate memory for thread arg.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
*arg = i;
|
||||
pthread_create(&philosopher[i], NULL, func, arg);
|
||||
}
|
||||
|
||||
for (i = 0; i < 15; i++)
|
||||
pthread_join(philosopher[i], NULL);
|
||||
|
||||
for (i = 0; i < 15; i++)
|
||||
pthread_mutex_destroy(&chopstick[i]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue