From 140889a1fd78d14125db2d5b43189fde332b8a5c Mon Sep 17 00:00:00 2001 From: Etienne Rey-bethbeder Date: Mon, 10 Apr 2023 17:39:30 +0200 Subject: [PATCH] _ --- philo/.utils_threads2.c.swp | Bin 0 -> 12288 bytes philo/Makefile | 2 +- philo/manage_threads.c | 34 ++++-------------------------- philo/philo.h | 8 ++++++- philo/threads.c | 17 ++++++--------- philo/utils_manage_threads.c | 16 +++++++++++++- philo/utils_threads2.c | 13 ++++-------- philo/utils_threads3.c | 39 +++++++++++++++++++++++++++++++++++ 8 files changed, 76 insertions(+), 53 deletions(-) create mode 100644 philo/.utils_threads2.c.swp create mode 100644 philo/utils_threads3.c diff --git a/philo/.utils_threads2.c.swp b/philo/.utils_threads2.c.swp new file mode 100644 index 0000000000000000000000000000000000000000..4c3c9f955d528c0ac0fcfdb2fbe39f04133b0518 GIT binary patch literal 12288 zcmeI2O-~b17=>SSV^P3}#>CZ?8YwO9Og{`VNMni{7seHCP@5@pOKZQz`5+LYZgAt; zr9VKz!mSH;CMNy?6BBi9O#Ba?na-3J3N+Y_=Dp3sy)*O9nS0LdEJ$xjeQ@`lm`|yM zcAn^KB4>=hc|E$ja+;(iHj?fEuITiRx1^vjrJ~-r#6W|0m z0ZxDu-~>1UPJk2O1ULasfD_;Z{=o!vgXrNIA~k}^vKnYBPYv3}t z1TKPKnBPzE9ee{jU>n>A1&{%rhgral6W|0m0ZxDu-~>27if#T@tSn}r^cM3|1c5vyIFyYBAC$(5lr@&!tvn7nsUP_ zcDsx8@zqlV>uDI=_lQFs_T1~mw*{kR5$irL@{2;9IpK2jnQ!PPm}>7TA3D?UE=+QZ z)>~C0VpuKn*cE6mN4K3mYtlP~0l#QRJ5x{eEeo7EdQBed3_haO=4#zFq?qz2ld4P+ zDRB`WS`%@_6C7d-1(!`jcch_-jGD`bOR|SK17YYs_aIi$v YLP?CghLXP$Jot!EaA+?IUOI*T05}5uMgRZ+ literal 0 HcmV?d00001 diff --git a/philo/Makefile b/philo/Makefile index 72879b9..638aef1 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -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 diff --git a/philo/manage_threads.c b/philo/manage_threads.c index 85dc77b..be9f9d7 100644 --- a/philo/manage_threads.c +++ b/philo/manage_threads.c @@ -6,26 +6,12 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; diff --git a/philo/philo.h b/philo/philo.h index 8e153a7..c1fdb37 100644 --- a/philo/philo.h +++ b/philo/philo.h @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); diff --git a/philo/threads.c b/philo/threads.c index d5c6714..b3a5749 100644 --- a/philo/threads.c +++ b/philo/threads.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/philo/utils_manage_threads.c b/philo/utils_manage_threads.c index cffbe89..af76f11 100644 --- a/philo/utils_manage_threads.c +++ b/philo/utils_manage_threads.c @@ -6,12 +6,26 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; diff --git a/philo/utils_threads2.c b/philo/utils_threads2.c index c218069..be820e9 100644 --- a/philo/utils_threads2.c +++ b/philo/utils_threads2.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* forks_threads.c :+: :+: :+: */ +/* utils_threads2.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); -} diff --git a/philo/utils_threads3.c b/philo/utils_threads3.c new file mode 100644 index 0000000..16bbb7c --- /dev/null +++ b/philo/utils_threads3.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils_threads3.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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)) + ; +}