This commit is contained in:
Danny Jonker 2023-05-20 06:56:40 +02:00
parent 824f2392c9
commit 0197401222
5 changed files with 70 additions and 60 deletions

View File

@ -6,7 +6,7 @@
# By: djonker <djonker@student.codam.nl> // \ \ __| | | \ \/ / # # By: djonker <djonker@student.codam.nl> // \ \ __| | | \ \/ / #
# (| | )|_| |_| |> < # # (| | )|_| |_| |> < #
# Created: 2021/08/19 15:20:20 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 \___)=(___/ #
# # # #
# **************************************************************************** # # **************************************************************************** #

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */ /* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */
/* (| | )|_| |_| |> < */ /* (| | )|_| |_| |> < */
/* Created: 2023/03/11 07:19:18 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 id;
int cycles; int cycles;
pthread_mutex_t *forks; 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 *philos;
int lfork; int lfork;
int rfork; int rfork;
@ -42,8 +45,11 @@ typedef struct s_philo
typedef struct s_strct typedef struct s_strct
{ {
pthread_mutex_t forks[512]; pthread_mutex_t forks[512];
pthread_mutex_t datarace[3];
t_philo philo[512]; t_philo philo[512];
pthread_mutex_t drdead;
pthread_mutex_t drdone;
pthread_mutex_t drcycle;
pthread_mutex_t drlastfood;
int philos; int philos;
long long dietime; long long dietime;
long long eattime; long long eattime;
@ -65,6 +71,6 @@ long long ft_time(void);
int philo_atoi(char *str); int philo_atoi(char *str);
int ft_printreturn(char *reason, int code); int ft_printreturn(char *reason, int code);
int ft_charactercheck(int argc, char **argv); int ft_charactercheck(int argc, char **argv);
int ft_alivecheck(t_philo *ps); void ft_aliveprint(t_philo *ps, char *str);
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */ /* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */
/* (| | )|_| |_| |> < */ /* (| | )|_| |_| |> < */
/* Created: 2023/03/15 02:00:19 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].sleeptime = &strct->sleeptime;
strct->philo[i].strtt = &strct->strtt; strct->philo[i].strtt = &strct->strtt;
strct->philo[i].forks = strct->forks; 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].lfork = i;
strct->philo[i].rfork = (i + 1) % strct->philos; strct->philo[i].rfork = (i + 1) % strct->philos;
strct->philo[i].philos = &strct->philos; strct->philo[i].philos = &strct->philos;

View File

@ -6,39 +6,39 @@
/* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */ /* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */
/* (| | )|_| |_| |> < */ /* (| | )|_| |_| |> < */
/* Created: 2023/03/11 06:42:31 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" #include "../philo.h"
int ft_eatsleepthink(t_philo *ps) void ft_eatsleepthink(t_philo *ps)
{ {
pthread_mutex_lock(&ps->forks[ps->lfork]); pthread_mutex_lock(&ps->forks[ps->lfork]);
if (ft_alivecheck(ps)) ft_aliveprint(ps, "has taken a fork");
printf("%lld %d has taken a fork\n", ft_time() - *ps->strtt, ps->id);
if (*ps->philos == 1) if (*ps->philos == 1)
return (1); return ;
pthread_mutex_lock(&ps->forks[ps->rfork]); pthread_mutex_lock(&ps->forks[ps->rfork]);
if (ft_alivecheck(ps)) ft_aliveprint(ps, "has taken a fork");
printf("%lld %d has taken a fork\n", ft_time() - *ps->strtt, ps->id); pthread_mutex_lock(ps->drlastfood);
pthread_mutex_lock(&ps->datarace[0]);
ps->lastfood = ft_time(); ps->lastfood = ft_time();
pthread_mutex_unlock(&ps->datarace[0]); pthread_mutex_unlock(ps->drlastfood);
if (ft_alivecheck(ps)) ft_aliveprint(ps, "is eating");
printf("%lld %d is eating\n", ft_time() - *ps->strtt, ps->id);
usleep(*ps->eattime * 1000); usleep(*ps->eattime * 1000);
pthread_mutex_unlock(&ps->forks[ps->lfork]); pthread_mutex_unlock(&ps->forks[ps->lfork]);
pthread_mutex_unlock(&ps->forks[ps->rfork]); pthread_mutex_unlock(&ps->forks[ps->rfork]);
if (ft_alivecheck(ps)) ft_aliveprint(ps, "is sleeping");
printf("%lld %d is sleeping\n", ft_time() - *ps->strtt, ps->id);
usleep(*ps->sleeptime * 1000); usleep(*ps->sleeptime * 1000);
if (ft_alivecheck(ps)) ft_aliveprint(ps, "is thinking");
printf("%lld %d is thinking\n", ft_time() - *ps->strtt, ps->id); pthread_mutex_lock(ps->drcycle);
pthread_mutex_lock(&ps->datarace[2]);
ps->cycles++; ps->cycles++;
pthread_mutex_unlock(&ps->datarace[2]); pthread_mutex_unlock(ps->drcycle);
return (0); 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) void *ft_cycle(void *pointer)
@ -48,45 +48,48 @@ void *ft_cycle(void *pointer)
philo = pointer; philo = pointer;
if ((philo->id - 1) % 2) if ((philo->id - 1) % 2)
usleep(*philo->eattime * 1000); usleep(*philo->eattime * 1000);
pthread_mutex_lock(&philo->datarace[2]); pthread_mutex_lock(philo->drdead);
while (philo->cycles < *philo->target && *philo->dead == 0) while (*philo->dead == 0)
{ {
pthread_mutex_unlock(&philo->datarace[2]); pthread_mutex_unlock(philo->drdead);
if (ft_eatsleepthink(philo)) ft_eatsleepthink(philo);
return (NULL); if (*philo->philos == 1)
pthread_mutex_lock(&philo->datarace[2]); 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); return (NULL);
} }
int ft_liveordie(t_strct *stct, int i) int ft_liveordie(t_strct *stct, int i)
{ {
pthread_mutex_lock(&stct->datarace[1]); pthread_mutex_lock(&stct->drdone);
while (stct->done < stct->philos) while (stct->done < stct->philos)
{ {
pthread_mutex_unlock(&stct->datarace[1]); pthread_mutex_unlock(&stct->drdone);
if (i == stct->philos) if (i == stct->philos)
i = 0; i = 0;
pthread_mutex_lock(&stct->datarace[0]); pthread_mutex_lock(&stct->drlastfood);
if (ft_time() - stct->philo[i].lastfood > stct->dietime) 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); 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; stct->dead = 1;
pthread_mutex_unlock(&stct->datarace[2]); pthread_mutex_unlock(&stct->drdead);
pthread_mutex_unlock(&stct->datarace[0]);
return (1); return (1);
} }
pthread_mutex_unlock(&stct->datarace[0]);
usleep(100); usleep(100);
i++; 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("All %d philosophers have", stct->philos);
printf(" finished all %d cycles\n", stct->target); printf(" finished all %d cycles\n", stct->target);
return (0); return (0);
@ -123,9 +126,10 @@ int main(int argc, char **argv)
} }
strct->done = 0; strct->done = 0;
strct->dead = 0; strct->dead = 0;
pthread_mutex_init(&strct->datarace[0], NULL); pthread_mutex_init(&strct->drdone, NULL);
pthread_mutex_init(&strct->datarace[1], NULL); pthread_mutex_init(&strct->drdead, NULL);
pthread_mutex_init(&strct->datarace[2], NULL); pthread_mutex_init(&strct->drcycle, NULL);
pthread_mutex_init(&strct->drlastfood, NULL);
ft_initstructandmutex(strct); ft_initstructandmutex(strct);
ft_startcycle(strct); ft_startcycle(strct);
ft_cleanup(strct); ft_cleanup(strct);

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */ /* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */
/* (| | )|_| |_| |> < */ /* (| | )|_| |_| |> < */
/* Created: 2023/03/15 02:01:41 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]); pthread_mutex_destroy(&strct->forks[i]);
i++; i++;
} }
pthread_mutex_destroy(&strct->datarace[0]); pthread_mutex_destroy(&strct->drdone);
pthread_mutex_destroy(&strct->datarace[1]); pthread_mutex_destroy(&strct->drdead);
pthread_mutex_destroy(&strct->datarace[2]); pthread_mutex_destroy(&strct->drcycle);
pthread_mutex_destroy(&strct->drlastfood);
free(strct); free(strct);
return (0); return (0);
} }
int ft_alivecheck(t_philo *ps) void ft_aliveprint(t_philo *ps, char *str)
{ {
int i; pthread_mutex_lock(ps->drdead);
if (!*ps->dead)
i = 1; printf("%lld %d %s\n", ft_time() - *ps->strtt, ps->id, str);
pthread_mutex_lock(&ps->datarace[2]); pthread_mutex_unlock(ps->drdead);
if (*ps->dead)
i = 0;
pthread_mutex_unlock(&ps->datarace[2]);
return (i);
} }