diff --git a/philo/Makefile b/philo/Makefile index 5be19f0..2e930b2 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -6,7 +6,7 @@ # By: djonker // \ \ __| | | \ \/ / # # (| | )|_| |_| |> < # # Created: 2021/08/19 15:20:20 by djonker /'\_ _/`\__|\__,_/_/\_\ # -# Updated: 2023/05/20 06:21:11 by djonker \___)=(___/ # +# Updated: 2023/05/20 08:42:21 by djonker \___)=(___/ # # # # **************************************************************************** # diff --git a/philo/philo.h b/philo/philo.h index 26df9dd..6d21d9b 100644 --- a/philo/philo.h +++ b/philo/philo.h @@ -6,7 +6,7 @@ /* By: houtworm // \ \ __| | | \ \/ / */ /* (| | )|_| |_| |> < */ /* Created: 2023/03/11 07:19:18 by houtworm /'\_ _/`\__|\__,_/_/\_\ */ -/* Updated: 2023/05/20 06:26:21 by djonker \___)=(___/ */ +/* Updated: 2023/05/20 08:07:24 by djonker \___)=(___/ */ /* */ /* ************************************************************************** */ @@ -62,7 +62,7 @@ typedef struct s_strct } t_strct; t_strct *ft_parseandvalidateinput(int argc, char **argv); -int ft_initstructandmutex(t_strct *strct); +int ft_initstructandmutex(t_strct *strct, int i); int ft_startcycle(t_strct *strct); void *ft_cycle(void *pointer); int ft_liveordie(t_strct *strct, int i); diff --git a/philo/src/init.c b/philo/src/init.c index c98bc44..b8500ba 100644 --- a/philo/src/init.c +++ b/philo/src/init.c @@ -6,7 +6,7 @@ /* By: houtworm // \ \ __| | | \ \/ / */ /* (| | )|_| |_| |> < */ /* Created: 2023/03/15 02:00:19 by houtworm /'\_ _/`\__|\__,_/_/\_\ */ -/* Updated: 2023/05/20 06:28:45 by djonker \___)=(___/ */ +/* Updated: 2023/05/20 08:06:24 by djonker \___)=(___/ */ /* */ /* ************************************************************************** */ @@ -62,11 +62,8 @@ t_strct *ft_parseandvalidateinput(int argc, char **argv) return (strct); } -int ft_initstructandmutex(t_strct *strct) +int ft_initstructandmutex(t_strct *strct, int i) { - int i; - - i = 0; while (i <= strct->philos) { strct->philo[i].id = i + 1; diff --git a/philo/src/philo.c b/philo/src/philo.c index 3f1a3d3..9224ee4 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 06:31:00 by djonker \___)=(___/ */ +/* Updated: 2023/05/20 09:08:25 by djonker \___)=(___/ */ /* */ /* ************************************************************************** */ @@ -32,13 +32,13 @@ void ft_eatsleepthink(t_philo *ps) ft_aliveprint(ps, "is thinking"); pthread_mutex_lock(ps->drcycle); ps->cycles++; - pthread_mutex_unlock(ps->drcycle); if (ps->cycles == *ps->target) { pthread_mutex_lock(ps->drdone); *ps->done = *ps->done + 1; pthread_mutex_unlock(ps->drdone); } + pthread_mutex_unlock(ps->drcycle); } void *ft_cycle(void *pointer) @@ -47,7 +47,7 @@ void *ft_cycle(void *pointer) philo = pointer; if ((philo->id - 1) % 2) - usleep(*philo->eattime * 1000); + usleep(*philo->eattime * 900); pthread_mutex_lock(philo->drdead); while (*philo->dead == 0) { @@ -55,15 +55,17 @@ void *ft_cycle(void *pointer) ft_eatsleepthink(philo); if (*philo->philos == 1) break ; + pthread_mutex_lock(philo->drdone); if (*philo->done == *philo->philos) { pthread_mutex_lock(philo->drdead); *philo->dead = 1; pthread_mutex_unlock(philo->drdead); } - else - pthread_mutex_lock(philo->drdead); + pthread_mutex_unlock(philo->drdone); + pthread_mutex_lock(philo->drdead); } + pthread_mutex_unlock(philo->drdead); return (NULL); } @@ -85,13 +87,14 @@ int ft_liveordie(t_strct *stct, int i) pthread_mutex_unlock(&stct->drdead); return (1); } + else + pthread_mutex_unlock(&stct->drlastfood); usleep(100); i++; pthread_mutex_lock(&stct->drdone); } pthread_mutex_unlock(&stct->drdone); - printf("All %d philosophers have", stct->philos); - printf(" finished all %d cycles\n", stct->target); + printf("All philosophers have finished all %d cycles", stct->target); return (0); } @@ -130,7 +133,7 @@ int main(int argc, char **argv) pthread_mutex_init(&strct->drdead, NULL); pthread_mutex_init(&strct->drcycle, NULL); pthread_mutex_init(&strct->drlastfood, NULL); - ft_initstructandmutex(strct); + ft_initstructandmutex(strct, 0); ft_startcycle(strct); ft_cleanup(strct); return (0); diff --git a/philo/src/util.c b/philo/src/util.c index 0069148..995d0fb 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/20 06:31:38 by djonker \___)=(___/ */ +/* Updated: 2023/05/20 08:27:44 by djonker \___)=(___/ */ /* */ /* ************************************************************************** */ @@ -84,6 +84,10 @@ void ft_aliveprint(t_philo *ps, char *str) { pthread_mutex_lock(ps->drdead); if (!*ps->dead) + { + pthread_mutex_unlock(ps->drdead); printf("%lld %d %s\n", ft_time() - *ps->strtt, ps->id, str); - pthread_mutex_unlock(ps->drdead); + } + else + pthread_mutex_unlock(ps->drdead); }