From e9c96f26b9bac79a4d8f5cccb4a13ed2856c7102 Mon Sep 17 00:00:00 2001 From: Houtworm Date: Mon, 22 May 2023 08:14:22 +0200 Subject: [PATCH] looking like it --- Readme.md | 3 +- philo/Makefile | 4 +- philo_bonus/Makefile | 9 ++-- philo_bonus/philo.h | 5 +- philo_bonus/src/init.c | 32 +------------ philo_bonus/src/philo.c | 85 +++++++-------------------------- philo_bonus/src/util.c | 19 +------- philo_bonus/src/watch.c | 103 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 134 insertions(+), 126 deletions(-) create mode 100644 philo_bonus/src/watch.c diff --git a/Readme.md b/Readme.md index a33b5c1..4693c1c 100644 --- a/Readme.md +++ b/Readme.md @@ -1,4 +1,5 @@ -# Philosophers +## Philosophers +- Fix people dying too soon with many many philos --- ## Tester diff --git a/philo/Makefile b/philo/Makefile index 0fc4650..518df37 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -1,12 +1,12 @@ # **************************************************************************** # # # # .--. _ # -# Makefile |o_o || | # +# Makefile :+: :+: :+: # # |:_/ || |_ _ ___ __ # # By: djonker // \ \ __| | | \ \/ / # # (| | )|_| |_| |> < # # Created: 2021/08/19 15:20:20 by djonker /'\_ _/`\__|\__,_/_/\_\ # -# Updated: 2023/05/22 00:43:58 by djonker \___)=(___/ # +# Updated: 2023/05/22 07:23:22 by houtworm ### ########.fr # # # # **************************************************************************** # diff --git a/philo_bonus/Makefile b/philo_bonus/Makefile index 8ba5134..3f22608 100644 --- a/philo_bonus/Makefile +++ b/philo_bonus/Makefile @@ -1,22 +1,23 @@ # **************************************************************************** # # # # .--. _ # -# Makefile |o_o || | # +# Makefile :+: :+: :+: # # |:_/ || |_ _ ___ __ # # By: djonker // \ \ __| | | \ \/ / # # (| | )|_| |_| |> < # # Created: 2021/08/19 15:20:20 by djonker /'\_ _/`\__|\__,_/_/\_\ # -# Updated: 2023/05/14 09:48:37 by djonker \___)=(___/ # +# Updated: 2023/05/22 07:37:03 by houtworm ### ########.fr # # # # **************************************************************************** # NAME =philo_bonus CC =gcc -CFLAGS =-Wall -Werror -Wextra# -g -fsanitize=address +CFLAGS =-Wall -Werror -Wextra -g -fsanitize=address RM =rm -f SRC =src/philo.c\ src/init.c\ - src/util.c + src/util.c\ + src/watch.c OBJ =$(SRC:src/%.c=obj/%.o) all: $(NAME) diff --git a/philo_bonus/philo.h b/philo_bonus/philo.h index 2a8a1db..acab5c8 100644 --- a/philo_bonus/philo.h +++ b/philo_bonus/philo.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* .--. _ */ -/* philo.h |o_o || | */ +/* philo.h :+: :+: :+: */ /* |:_/ || |_ _ ___ __ */ /* By: houtworm // \ \ __| | | \ \/ / */ /* (| | )|_| |_| |> < */ /* Created: 2023/03/11 07:19:18 by houtworm /'\_ _/`\__|\__,_/_/\_\ */ -/* Updated: 2023/05/22 04:46:06 by djonker \___)=(___/ */ +/* Updated: 2023/05/22 07:37:35 by houtworm ### ########.fr */ /* */ /* ************************************************************************** */ @@ -72,6 +72,7 @@ void ft_cycle(void *pointer); int ft_releasetheminds(t_strct *strct, int m); int ft_cyclecounter(t_strct *strct); int ft_waitingfordeath(t_strct *strct); +void *ft_alive(void *pointer); int ft_cleanup(t_strct *strct); long long ft_time(void); int philo_atoi(char *str); diff --git a/philo_bonus/src/init.c b/philo_bonus/src/init.c index 4a4fc2d..4a46e04 100644 --- a/philo_bonus/src/init.c +++ b/philo_bonus/src/init.c @@ -6,7 +6,7 @@ /* By: houtworm // \ \ __| | | \ \/ / */ /* (| | )|_| |_| |> < */ /* Created: 2023/03/15 02:00:19 by houtworm /'\_ _/`\__|\__,_/_/\_\ */ -/* Updated: 2023/05/22 07:13:59 by houtworm ### ########.fr */ +/* Updated: 2023/05/22 07:35:35 by houtworm ### ########.fr */ /* */ /* ************************************************************************** */ @@ -91,36 +91,6 @@ int ft_initstructandmutex(t_strct *strct, int i) return (0); } -int ft_releasetheminds(t_strct *strct, int m) -{ - int i; - - i = 0; - if (m) - { - strct->strtt = ft_time(); - while (i < strct->philos) - { - sem_post(strct->hold); - i++; - } - } - else - { - sem_close(strct->dead); - sem_close(strct->done); - sem_close(strct->hold); - sem_close(strct->forks); - sem_close(strct->print); - sem_unlink("dead"); - sem_unlink("done"); - sem_unlink("hold"); - sem_unlink("forks"); - sem_unlink("print"); - } - return (0); -} - void ft_safeprint(t_philo *ps, char *str) { sem_wait(ps->print); diff --git a/philo_bonus/src/philo.c b/philo_bonus/src/philo.c index 55aaee7..24f6332 100644 --- a/philo_bonus/src/philo.c +++ b/philo_bonus/src/philo.c @@ -6,7 +6,7 @@ /* By: houtworm // \ \ __| | | \ \/ / */ /* (| | )|_| |_| |> < */ /* Created: 2023/03/11 06:42:31 by houtworm /'\_ _/`\__|\__,_/_/\_\ */ -/* Updated: 2023/05/22 07:12:17 by houtworm ### ########.fr */ +/* Updated: 2023/05/22 08:10:48 by houtworm ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,11 +15,7 @@ int ft_eatsleepthink(t_philo *ps) { sem_wait(ps->forks); - /*if (ft_time() - ps->lastfood > *ps->dietime)*/ - /*return (1);*/ ft_safeprint(ps, "has taken a fork"); - /*if (*ps->philos == 1)*/ - /*return (1);*/ sem_wait(ps->forks); ft_safeprint(ps, "has taken a fork"); ps->lastfood = ft_time(); @@ -40,20 +36,6 @@ int ft_eatsleepthink(t_philo *ps) return (0); } -void *ft_alivecheck(void *pointer) -{ - t_philo *philo; - - philo = pointer; - while (ft_time() - philo->lastfood < *philo->dietime) - usleep(500); - sem_post(philo->dead); - sem_wait(philo->print); - if (*philo->alive) - printf("%lld %d died\n", ft_time() - *philo->strtt, philo->id); - return (NULL); -} - void ft_cycle(void *pointer) { t_philo *philo; @@ -75,68 +57,35 @@ void ft_cycle(void *pointer) sem_post(philo->print); } -int ft_startcycle(t_strct *strct) +int ft_startcycle(t_strct *st) { int i; i = 0; - strct->strtt = ft_time(); - while (i < strct->philos) + st->strtt = ft_time(); + while (i < st->philos) { - strct->pid[i] = fork(); - strct->alive = 1; - if (strct->pid[i] == 0) + st->pid[i] = fork(); + st->alive = 1; + if (st->pid[i] == 0) { - strct->philo[i].alive = &strct->alive; - pthread_create(&strct->philo[i].thrd, NULL, ft_alivecheck, &strct->philo[i]); - strct->philo[i].lastfood = ft_time(); - ft_cycle(&strct->philo[i]); - sem_close(strct->dead); - sem_close(strct->done); - sem_close(strct->hold); - sem_close(strct->forks); - sem_close(strct->print); - /*pthread_join(strct->philo[i].thrd, NULL);*/ - free(strct); + st->philo[i].alive = &st->alive; + pthread_create(&st->philo[i].thrd, NULL, ft_alive, &st->philo[i]); + st->philo[i].lastfood = ft_time(); + ft_cycle(&st->philo[i]); + sem_close(st->dead); + sem_close(st->done); + sem_close(st->hold); + sem_close(st->forks); + sem_close(st->print); + free(st); exit(0); } i++; - usleep(1000); } return (0); } -int ft_cyclecounter(t_strct *strct) -{ - int i; - sem_t *done; - sem_t *print; - int philos; - int target; - - philos = strct->philos; - target = strct->target; - sem_close(strct->dead); - sem_close(strct->done); - sem_close(strct->hold); - sem_close(strct->forks); - sem_close(strct->print); - free(strct); - done = sem_open("done", 0); - print = sem_open("print", 0); - i = 0; - while (i < philos) - { - sem_wait(done); - i++; - } - sem_close(done); - sem_wait(print); - printf("All philosophers have finished all %d cycles\n", target); - sem_close(print); - exit (0); -} - int main(int argc, char **argv) { t_strct *strct; diff --git a/philo_bonus/src/util.c b/philo_bonus/src/util.c index 671629a..700a044 100644 --- a/philo_bonus/src/util.c +++ b/philo_bonus/src/util.c @@ -6,7 +6,7 @@ /* By: houtworm // \ \ __| | | \ \/ / */ /* (| | )|_| |_| |> < */ /* Created: 2023/03/15 02:01:41 by houtworm /'\_ _/`\__|\__,_/_/\_\ */ -/* Updated: 2023/05/22 07:15:25 by houtworm ### ########.fr */ +/* Updated: 2023/05/22 07:36:43 by houtworm ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,22 +28,6 @@ long long ft_time(void) return (r); } -int ft_waitingfordeath(t_strct *strct) -{ - sem_t *dead; - - sem_close(strct->dead); - sem_close(strct->done); - sem_close(strct->hold); - sem_close(strct->forks); - sem_close(strct->print); - free(strct); - dead = sem_open("dead", 0); - sem_wait(dead); - sem_close(dead); - exit (0); -} - int philo_atoi(char *str) { unsigned int r; @@ -81,7 +65,6 @@ int ft_cleanup(t_strct *strct) kill(strct->pid[502], 15); while (i < strct->philos) { - /*pthread_join(strct->philo[i].thrd, NULL);*/ kill(strct->pid[i], 15); i++; } diff --git a/philo_bonus/src/watch.c b/philo_bonus/src/watch.c new file mode 100644 index 0000000..957d0e1 --- /dev/null +++ b/philo_bonus/src/watch.c @@ -0,0 +1,103 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* watch.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: houtworm +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/22 07:34:59 by houtworm #+# #+# */ +/* Updated: 2023/05/22 08:09:16 by houtworm ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../philo.h" + +int ft_releasetheminds(t_strct *strct, int m) +{ + int i; + + i = 0; + if (m) + { + /*strct->strtt = ft_time();*/ + while (i < strct->philos) + { + sem_post(strct->hold); + i++; + } + } + else + { + sem_close(strct->dead); + sem_close(strct->done); + sem_close(strct->hold); + sem_close(strct->forks); + sem_close(strct->print); + sem_unlink("dead"); + sem_unlink("done"); + sem_unlink("hold"); + sem_unlink("forks"); + sem_unlink("print"); + } + return (0); +} + +int ft_cyclecounter(t_strct *strct) +{ + int i; + sem_t *done; + sem_t *print; + int philos; + int target; + + philos = strct->philos; + target = strct->target; + sem_close(strct->dead); + sem_close(strct->hold); + sem_close(strct->forks); + free(strct); + done = sem_open("done", 0); + print = sem_open("print", 0); + i = 0; + while (i < philos) + { + sem_wait(done); + i++; + } + sem_close(done); + sem_wait(print); + printf("All philosophers have finished all %d cycles\n", target); + sem_close(print); + exit (0); +} + +void *ft_alive(void *pointer) +{ + t_philo *philo; + + philo = pointer; + usleep(*philo->dietime); + while (ft_time() - philo->lastfood < *philo->dietime) + usleep(500); + sem_post(philo->dead); + sem_wait(philo->print); + if (*philo->alive) + printf("%lld %d died\n", ft_time() - *philo->strtt, philo->id); + return (NULL); +} + +int ft_waitingfordeath(t_strct *strct) +{ + sem_t *dead; + + sem_close(strct->dead); + sem_close(strct->done); + sem_close(strct->hold); + sem_close(strct->forks); + sem_close(strct->print); + free(strct); + dead = sem_open("dead", 0); + sem_wait(dead); + sem_close(dead); + exit (0); +}