diff --git a/philo/Makefile b/philo/Makefile index 83e8566..5be19f0 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/14 09:08:22 by djonker \___)=(___/ # +# Updated: 2023/05/20 06:21:11 by djonker \___)=(___/ # # # # **************************************************************************** # diff --git a/philo/philo.h b/philo/philo.h index a3e47d7..26df9dd 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/14 06:08:32 by djonker \___)=(___/ */ +/* Updated: 2023/05/20 06:26:21 by djonker \___)=(___/ */ /* */ /* ************************************************************************** */ @@ -24,7 +24,10 @@ typedef struct s_philo int id; int cycles; pthread_mutex_t *forks; - pthread_mutex_t *datarace; + pthread_mutex_t *drdead; + pthread_mutex_t *drdone; + pthread_mutex_t *drcycle; + pthread_mutex_t *drlastfood; int *philos; int lfork; int rfork; @@ -42,8 +45,11 @@ typedef struct s_philo typedef struct s_strct { pthread_mutex_t forks[512]; - pthread_mutex_t datarace[3]; t_philo philo[512]; + pthread_mutex_t drdead; + pthread_mutex_t drdone; + pthread_mutex_t drcycle; + pthread_mutex_t drlastfood; int philos; long long dietime; long long eattime; @@ -65,6 +71,6 @@ long long ft_time(void); int philo_atoi(char *str); int ft_printreturn(char *reason, int code); int ft_charactercheck(int argc, char **argv); -int ft_alivecheck(t_philo *ps); +void ft_aliveprint(t_philo *ps, char *str); #endif diff --git a/philo/src/init.c b/philo/src/init.c index d36fa26..c98bc44 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/14 08:24:58 by djonker \___)=(___/ */ +/* Updated: 2023/05/20 06:28:45 by djonker \___)=(___/ */ /* */ /* ************************************************************************** */ @@ -77,7 +77,10 @@ int ft_initstructandmutex(t_strct *strct) strct->philo[i].sleeptime = &strct->sleeptime; strct->philo[i].strtt = &strct->strtt; strct->philo[i].forks = strct->forks; - strct->philo[i].datarace = strct->datarace; + strct->philo[i].drdone = &strct->drdone; + strct->philo[i].drdead = &strct->drdead; + strct->philo[i].drcycle = &strct->drcycle; + strct->philo[i].drlastfood = &strct->drlastfood; strct->philo[i].lfork = i; strct->philo[i].rfork = (i + 1) % strct->philos; strct->philo[i].philos = &strct->philos; diff --git a/philo/src/philo.c b/philo/src/philo.c index 7b120dd..3f1a3d3 100644 --- a/philo/src/philo.c +++ b/philo/src/philo.c @@ -6,39 +6,39 @@ /* By: houtworm // \ \ __| | | \ \/ / */ /* (| | )|_| |_| |> < */ /* Created: 2023/03/11 06:42:31 by houtworm /'\_ _/`\__|\__,_/_/\_\ */ -/* Updated: 2023/05/14 08:18:24 by djonker \___)=(___/ */ +/* Updated: 2023/05/20 06:31:00 by djonker \___)=(___/ */ /* */ /* ************************************************************************** */ #include "../philo.h" -int ft_eatsleepthink(t_philo *ps) +void ft_eatsleepthink(t_philo *ps) { pthread_mutex_lock(&ps->forks[ps->lfork]); - if (ft_alivecheck(ps)) - printf("%lld %d has taken a fork\n", ft_time() - *ps->strtt, ps->id); + ft_aliveprint(ps, "has taken a fork"); if (*ps->philos == 1) - return (1); + return ; pthread_mutex_lock(&ps->forks[ps->rfork]); - if (ft_alivecheck(ps)) - printf("%lld %d has taken a fork\n", ft_time() - *ps->strtt, ps->id); - pthread_mutex_lock(&ps->datarace[0]); + ft_aliveprint(ps, "has taken a fork"); + pthread_mutex_lock(ps->drlastfood); ps->lastfood = ft_time(); - pthread_mutex_unlock(&ps->datarace[0]); - if (ft_alivecheck(ps)) - printf("%lld %d is eating\n", ft_time() - *ps->strtt, ps->id); + pthread_mutex_unlock(ps->drlastfood); + ft_aliveprint(ps, "is eating"); usleep(*ps->eattime * 1000); pthread_mutex_unlock(&ps->forks[ps->lfork]); pthread_mutex_unlock(&ps->forks[ps->rfork]); - if (ft_alivecheck(ps)) - printf("%lld %d is sleeping\n", ft_time() - *ps->strtt, ps->id); + ft_aliveprint(ps, "is sleeping"); usleep(*ps->sleeptime * 1000); - if (ft_alivecheck(ps)) - printf("%lld %d is thinking\n", ft_time() - *ps->strtt, ps->id); - pthread_mutex_lock(&ps->datarace[2]); + ft_aliveprint(ps, "is thinking"); + pthread_mutex_lock(ps->drcycle); ps->cycles++; - pthread_mutex_unlock(&ps->datarace[2]); - return (0); + 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); + } } void *ft_cycle(void *pointer) @@ -48,45 +48,48 @@ void *ft_cycle(void *pointer) philo = pointer; if ((philo->id - 1) % 2) usleep(*philo->eattime * 1000); - pthread_mutex_lock(&philo->datarace[2]); - while (philo->cycles < *philo->target && *philo->dead == 0) + pthread_mutex_lock(philo->drdead); + while (*philo->dead == 0) { - pthread_mutex_unlock(&philo->datarace[2]); - if (ft_eatsleepthink(philo)) - return (NULL); - pthread_mutex_lock(&philo->datarace[2]); + pthread_mutex_unlock(philo->drdead); + ft_eatsleepthink(philo); + if (*philo->philos == 1) + break ; + 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->datarace[2]); - pthread_mutex_lock(&philo->datarace[1]); - *philo->done = *philo->done + 1; - pthread_mutex_unlock(&philo->datarace[1]); return (NULL); } int ft_liveordie(t_strct *stct, int i) { - pthread_mutex_lock(&stct->datarace[1]); + pthread_mutex_lock(&stct->drdone); while (stct->done < stct->philos) { - pthread_mutex_unlock(&stct->datarace[1]); + pthread_mutex_unlock(&stct->drdone); if (i == stct->philos) i = 0; - pthread_mutex_lock(&stct->datarace[0]); + pthread_mutex_lock(&stct->drlastfood); if (ft_time() - stct->philo[i].lastfood > stct->dietime) { + pthread_mutex_unlock(&stct->drlastfood); printf("%lld %d died\n", ft_time() - stct->strtt, stct->philo[i].id); - pthread_mutex_lock(&stct->datarace[2]); + pthread_mutex_lock(&stct->drdead); stct->dead = 1; - pthread_mutex_unlock(&stct->datarace[2]); - pthread_mutex_unlock(&stct->datarace[0]); + pthread_mutex_unlock(&stct->drdead); return (1); } - pthread_mutex_unlock(&stct->datarace[0]); usleep(100); i++; - pthread_mutex_lock(&stct->datarace[1]); + pthread_mutex_lock(&stct->drdone); } - pthread_mutex_unlock(&stct->datarace[1]); + pthread_mutex_unlock(&stct->drdone); printf("All %d philosophers have", stct->philos); printf(" finished all %d cycles\n", stct->target); return (0); @@ -123,9 +126,10 @@ int main(int argc, char **argv) } strct->done = 0; strct->dead = 0; - pthread_mutex_init(&strct->datarace[0], NULL); - pthread_mutex_init(&strct->datarace[1], NULL); - pthread_mutex_init(&strct->datarace[2], NULL); + pthread_mutex_init(&strct->drdone, NULL); + pthread_mutex_init(&strct->drdead, NULL); + pthread_mutex_init(&strct->drcycle, NULL); + pthread_mutex_init(&strct->drlastfood, NULL); ft_initstructandmutex(strct); ft_startcycle(strct); ft_cleanup(strct); diff --git a/philo/src/util.c b/philo/src/util.c index 0cee70e..0069148 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/14 06:09:40 by djonker \___)=(___/ */ +/* Updated: 2023/05/20 06:31:38 by djonker \___)=(___/ */ /* */ /* ************************************************************************** */ @@ -72,21 +72,18 @@ int ft_cleanup(t_strct *strct) pthread_mutex_destroy(&strct->forks[i]); i++; } - pthread_mutex_destroy(&strct->datarace[0]); - pthread_mutex_destroy(&strct->datarace[1]); - pthread_mutex_destroy(&strct->datarace[2]); + pthread_mutex_destroy(&strct->drdone); + pthread_mutex_destroy(&strct->drdead); + pthread_mutex_destroy(&strct->drcycle); + pthread_mutex_destroy(&strct->drlastfood); free(strct); return (0); } -int ft_alivecheck(t_philo *ps) +void ft_aliveprint(t_philo *ps, char *str) { - int i; - - i = 1; - pthread_mutex_lock(&ps->datarace[2]); - if (*ps->dead) - i = 0; - pthread_mutex_unlock(&ps->datarace[2]); - return (i); + pthread_mutex_lock(ps->drdead); + if (!*ps->dead) + printf("%lld %d %s\n", ft_time() - *ps->strtt, ps->id, str); + pthread_mutex_unlock(ps->drdead); }