This commit is contained in:
Etienne Rey-bethbeder 2023-04-10 17:39:30 +02:00
parent 29832e45c3
commit 140889a1fd
8 changed files with 76 additions and 53 deletions

BIN
philo/.utils_threads2.c.swp Normal file

Binary file not shown.

View file

@ -1,4 +1,4 @@
SRCS = main.c parsing.c threads.c manage_threads.c utils_threads.c utils_manage_threads.c utils_threads2.c
SRCS = main.c parsing.c threads.c manage_threads.c utils_threads.c utils_manage_threads.c utils_threads2.c utils_threads3.c
OBJS = ${SRCS:.c=.o}
UTILS = utils/utils.a
CC = clang

View file

@ -6,26 +6,12 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/29 20:44:30 by erey-bet #+# #+# */
/* Updated: 2023/04/08 19:40:03 by erey-bet ### ########.fr */
/* Updated: 2023/04/10 15:55:42 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
int *init_forks(t_config *config)
{
int *forks;
int i;
forks = ft_calloc(sizeof(int), config->nbr_philo);
if (!forks)
return (NULL);
i = 0;
while (i < config->nbr_philo)
forks[i++] = 1;
return (forks);
}
static t_same *create_struct_same(t_config *config)
{
t_same *same;
@ -35,24 +21,12 @@ static t_same *create_struct_same(t_config *config)
return (NULL);
same->forks = init_forks(config);
if (!same->forks)
{
free(same);
return (NULL);
}
return (same);
same->mutex = ft_calloc(sizeof(pthread_mutex_t), config->nbr_philo + 3);
if (!same->mutex)
{
free(same->forks);
free(same);
return (NULL);
}
return (same);
if (init_mutex(same->mutex, config->nbr_philo + 3))
{
free(same->forks);
free(same->mutex);
free(same);
return (NULL);
}
return (same);
same->config = config;
same->death = 0;
same->all_eat = 0;

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 14:51:16 by erey-bet #+# #+# */
/* Updated: 2023/04/09 19:29:44 by erey-bet ### ########.fr */
/* Updated: 2023/04/10 11:21:38 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -71,6 +71,12 @@ int get_fork(t_philo *philo, int id_fork);
void take_fork(t_philo *philo, int id_fork);
void drop_fork(t_philo *philo, int id_fork, int next_id_fork);
void ft_sleep(long long time_to_sleep);
int *init_forks(t_config *config);
int before_eat(t_philo *philo, t_only *only,
t_config *config, int nbr_philo);
int in_loop_eat_sleep(t_philo *philo, t_only *only,
t_config *config, int nbr_philo);
void wait_fork(t_philo *philo, t_only *only, int id);
/* THREADS */
void *philosopher(t_philo *philo);

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 20:08:33 by erey-bet #+# #+# */
/* Updated: 2023/04/09 19:45:30 by erey-bet ### ########.fr */
/* Updated: 2023/04/10 17:22:22 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -41,6 +41,8 @@ void init_philo(t_philo *philo, t_only *only,
*nbr_philo = philo->same->config->nbr_philo;
only->next_id = (only->id + 1) % *nbr_philo;
only->last_eat_time = get_utime();
if (only->id == only->next_id)
return ;
if (only->id % 2 == 1 || (*nbr_philo % 2 == 1
&& only->id == *nbr_philo - 1))
{
@ -87,8 +89,7 @@ void loop_philosopher(t_philo *philo, t_only *only,
{
while (!finish(philo, only->last_eat_time))
{
while (!finish(philo, only->last_eat_time) && !get_fork(philo, only->id))
;
wait_fork(philo, only, only->id);
if (get_fork(philo, only->id))
{
take_fork(philo, only->id);
@ -96,16 +97,10 @@ void loop_philosopher(t_philo *philo, t_only *only,
while (only->id == only->next_id
&& !finish(philo, only->last_eat_time))
;
while (!finish(philo, only->last_eat_time) && !get_fork(philo, only->next_id))
;
wait_fork(philo, only, only->next_id);
if (get_fork(philo, only->next_id))
{
if (verif_finish(philo, only))
break ;
take_fork(philo, only->next_id);
message(philo, "has taken a fork\n");
message(philo, "is eating\n");
if (!in_loop_eat_sleep(philo, only, config, nbr_philo))
if (!before_eat(philo, only, config, nbr_philo))
break ;
}
else

View file

@ -6,12 +6,26 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/30 01:04:41 by erey-bet #+# #+# */
/* Updated: 2023/04/09 18:09:18 by erey-bet ### ########.fr */
/* Updated: 2023/04/10 09:53:55 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
int *init_forks(t_config *config)
{
int *forks;
int i;
forks = ft_calloc(sizeof(int), config->nbr_philo);
if (!forks)
return (NULL);
i = 0;
while (i < config->nbr_philo)
forks[i++] = 1;
return (forks);
}
int init_mutex(pthread_mutex_t *mutex, int nbr)
{
int i;

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* forks_threads.c :+: :+: :+: */
/* utils_threads2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/09 20:49:44 by erey-bet #+# #+# */
/* Updated: 2023/04/09 20:49:47 by erey-bet ### ########.fr */
/* Updated: 2023/04/10 17:31:37 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,6 +17,7 @@ void take_fork(t_philo *philo, int id_fork)
pthread_mutex_lock(&philo->same->mutex[id_fork]);
philo->same->forks[id_fork] = 0;
pthread_mutex_unlock(&philo->same->mutex[id_fork]);
usleep(10);
}
void drop_fork(t_philo *philo, int id_fork, int next_id_fork)
@ -30,6 +31,7 @@ void drop_fork(t_philo *philo, int id_fork, int next_id_fork)
philo->same->forks[next_id_fork] = 1;
pthread_mutex_unlock(&philo->same->mutex[next_id_fork]);
}
usleep(10);
}
int get_fork(t_philo *philo, int id_fork)
@ -42,10 +44,3 @@ int get_fork(t_philo *philo, int id_fork)
usleep(10);
return (fork);
}
void ft_sleep(long long time_to_sleep)
{
if (time_to_sleep < 0)
return ;
usleep(time_to_sleep);
}

39
philo/utils_threads3.c Normal file
View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_threads3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/10 10:39:43 by erey-bet #+# #+# */
/* Updated: 2023/04/10 10:39:56 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
void ft_sleep(long long time_to_sleep)
{
if (time_to_sleep < 0)
return ;
usleep(time_to_sleep);
}
int before_eat(t_philo *philo, t_only *only,
t_config *config, int nbr_philo)
{
if (verif_finish(philo, only))
return (0);
take_fork(philo, only->next_id);
message(philo, "has taken a fork\n");
message(philo, "is eating\n");
if (!in_loop_eat_sleep(philo, only, config, nbr_philo))
return (0);
return (1);
}
void wait_fork(t_philo *philo, t_only *only, int id)
{
while (!finish(philo, only->last_eat_time) && !get_fork(philo, id))
;
}