From d8f7f4ce0f131470e334691bcbb40bcfbb5ce43b Mon Sep 17 00:00:00 2001 From: Danny Jonker Date: Wed, 24 May 2023 02:37:18 +0200 Subject: [PATCH] bonus_done --- philo/src/philo.c | 4 ++-- philo/src/util.c | 3 ++- philo_bonus/philo.h | 6 +++--- philo_bonus/src/init.c | 23 ++++++++++++++++++++--- philo_bonus/src/philo.c | 8 +++----- philo_bonus/src/watch.c | 32 +++++++++++--------------------- 6 files changed, 41 insertions(+), 35 deletions(-) diff --git a/philo/src/philo.c b/philo/src/philo.c index 852aed3..984cb2e 100644 --- a/philo/src/philo.c +++ b/philo/src/philo.c @@ -6,7 +6,7 @@ /* By: houtworm // \ \ __| | | \ \/ / */ /* (| | )|_| |_| |> < */ /* Created: 2023/03/11 06:42:31 by houtworm /'\_ _/`\__|\__,_/_/\_\ */ -/* Updated: 2023/05/20 09:47:03 by djonker \___)=(___/ */ +/* Updated: 2023/05/24 01:08:23 by djonker \___)=(___/ */ /* */ /* ************************************************************************** */ @@ -89,7 +89,7 @@ int ft_liveordie(t_strct *stct, int i) } else pthread_mutex_unlock(&stct->drlastfood); - usleep(100); + usleep(1); i++; pthread_mutex_lock(&stct->drdone); } diff --git a/philo/src/util.c b/philo/src/util.c index bc5ec9f..8044af3 100644 --- a/philo/src/util.c +++ b/philo/src/util.c @@ -6,7 +6,7 @@ /* By: houtworm // \ \ __| | | \ \/ / */ /* (| | )|_| |_| |> < */ /* Created: 2023/03/15 02:01:41 by houtworm /'\_ _/`\__|\__,_/_/\_\ */ -/* Updated: 2023/05/21 23:43:36 by djonker \___)=(___/ */ +/* Updated: 2023/05/24 01:09:41 by djonker \___)=(___/ */ /* */ /* ************************************************************************** */ @@ -91,3 +91,4 @@ void ft_aliveprint(t_philo *ps, char *str) pthread_mutex_unlock(ps->drdead); printf("%lld %d %s\n", ft_time() - *ps->strtt, ps->id, str); } +// write? diff --git a/philo_bonus/philo.h b/philo_bonus/philo.h index acab5c8..9da6439 100644 --- a/philo_bonus/philo.h +++ b/philo_bonus/philo.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* .--. _ */ -/* philo.h :+: :+: :+: */ +/* philo.h |o_o || | */ /* |:_/ || |_ _ ___ __ */ /* By: houtworm // \ \ __| | | \ \/ / */ /* (| | )|_| |_| |> < */ /* Created: 2023/03/11 07:19:18 by houtworm /'\_ _/`\__|\__,_/_/\_\ */ -/* Updated: 2023/05/22 07:37:35 by houtworm ### ########.fr */ +/* Updated: 2023/05/24 01:38:55 by djonker \___)=(___/ */ /* */ /* ************************************************************************** */ @@ -69,7 +69,7 @@ t_strct *ft_parseandvalidateinput(int argc, char **argv); int ft_initstructandmutex(t_strct *strct, int i); int ft_startcycle(t_strct *strct); void ft_cycle(void *pointer); -int ft_releasetheminds(t_strct *strct, int m); +int ft_releasetheminds(t_strct *strct); int ft_cyclecounter(t_strct *strct); int ft_waitingfordeath(t_strct *strct); void *ft_alive(void *pointer); diff --git a/philo_bonus/src/init.c b/philo_bonus/src/init.c index ae93a01..7d62c9f 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/24 00:23:31 by djonker \___)=(___/ */ +/* Updated: 2023/05/24 02:14:22 by djonker \___)=(___/ */ /* */ /* ************************************************************************** */ @@ -62,14 +62,30 @@ t_strct *ft_parseandvalidateinput(int argc, char **argv) return (strct); } -int ft_initstructandmutex(t_strct *strct, int i) +int ft_restoresemaphore(t_strct *strct) { - ft_releasetheminds(strct, 0); + 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"); strct->dead = sem_open("dead", O_CREAT | O_EXCL, 0666, 0); strct->done = sem_open("done", O_CREAT | O_EXCL, 0666, 0); strct->hold = sem_open("hold", O_CREAT | O_EXCL, 0666, 0); strct->print = sem_open("print", O_CREAT | O_EXCL, 0666, 1); strct->forks = sem_open("forks", O_CREAT | O_EXCL, 0666, strct->philos); + return (0); +} + +int ft_initstructandmutex(t_strct *strct, int i) +{ + ft_restoresemaphore(strct); + strct->alive = 1; while (i <= strct->philos) { strct->philo[i].id = i + 1; @@ -80,6 +96,7 @@ int ft_initstructandmutex(t_strct *strct, int i) strct->philo[i].strtt = &strct->strtt; strct->philo[i].philos = &strct->philos; strct->philo[i].target = &strct->target; + strct->philo[i].alive = &strct->alive; strct->philo[i].forks = strct->forks; strct->philo[i].done = strct->done; strct->philo[i].dead = strct->dead; diff --git a/philo_bonus/src/philo.c b/philo_bonus/src/philo.c index c7718c3..5c47cbc 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/24 00:29:03 by djonker \___)=(___/ */ +/* Updated: 2023/05/24 02:15:07 by djonker \___)=(___/ */ /* */ /* ************************************************************************** */ @@ -63,15 +63,13 @@ int ft_startcycle(t_strct *st) i = 0; st->strtt = ft_time(); - st->alive = 1; while (i < st->philos) { st->pid[i] = fork(); if (st->pid[i] == 0) { - st->philo[i].alive = &st->alive; - pthread_create(&st->philo[i].thrd, NULL, ft_alive, &st->philo[i]); st->philo[i].lastfood = ft_time(); + pthread_create(&st->philo[i].thrd, NULL, ft_alive, &st->philo[i]); ft_cycle(&st->philo[i]); sem_close(st->dead); sem_close(st->done); @@ -108,7 +106,7 @@ int main(int argc, char **argv) strct->pid[502] = fork(); if (strct->pid[502] == 0) ft_cyclecounter(strct); - ft_releasetheminds(strct, 1); + ft_releasetheminds(strct); while (i != strct->pid[501] && i != strct->pid[502]) i = waitpid(-1, NULL, 0); ft_cleanup(strct); diff --git a/philo_bonus/src/watch.c b/philo_bonus/src/watch.c index 90947ad..327f3e6 100644 --- a/philo_bonus/src/watch.c +++ b/philo_bonus/src/watch.c @@ -6,37 +6,21 @@ /* By: djonker // \ \ __| | | \ \/ / */ /* (| | )|_| |_| |> < */ /* Created: 2023/05/24 00:23:22 by djonker /'\_ _/`\__|\__,_/_/\_\ */ -/* Updated: 2023/05/24 00:28:28 by djonker \___)=(___/ */ +/* Updated: 2023/05/24 02:24:11 by djonker \___)=(___/ */ /* */ /* ************************************************************************** */ #include "../philo.h" -int ft_releasetheminds(t_strct *strct, int m) +int ft_releasetheminds(t_strct *strct) { int i; i = 0; - if (m) + while (i < (strct->philos * 2)) { - while (i < (strct->philos * 2)) - { - 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"); + sem_post(strct->hold); + i++; } return (0); } @@ -73,8 +57,14 @@ int ft_cyclecounter(t_strct *strct) void *ft_alive(void *pointer) { t_philo *philo; + sem_t *hold; + sem_t *print; philo = pointer; + sem_close(philo->hold); + sem_close(philo->print); + hold = sem_open("hold", 0); + print = sem_open("print", 0); sem_wait(philo->hold); while (ft_time() - philo->lastfood < *philo->dietime) usleep(500);