just the norm

This commit is contained in:
Danny Jonker 2023-05-22 05:51:23 +02:00
parent 5309cbd7fd
commit 8db8d2c8d4
4 changed files with 46 additions and 20 deletions

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/21 22:38:50 by djonker \___)=(___/ */ /* Updated: 2023/05/22 04:46:06 by djonker \___)=(___/ */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,6 +19,7 @@
# include <fcntl.h> # include <fcntl.h>
# include <signal.h> # include <signal.h>
# include <semaphore.h> # include <semaphore.h>
# include <pthread.h>
# include <sys/time.h> # include <sys/time.h>
# include <sys/wait.h> # include <sys/wait.h>
@ -36,11 +37,13 @@ typedef struct s_philo
long long *eattime; long long *eattime;
long long *sleeptime; long long *sleeptime;
long long *strtt; long long *strtt;
int *alive;
int *target; int *target;
sem_t *done; sem_t *done;
sem_t *dead; sem_t *dead;
sem_t *hold; sem_t *hold;
sem_t *print; sem_t *print;
pthread_t thrd;
} t_philo; } t_philo;
typedef struct s_strct typedef struct s_strct
@ -54,6 +57,7 @@ typedef struct s_strct
long long sleeptime; long long sleeptime;
long long strtt; long long strtt;
int target; int target;
int alive;
sem_t *done; sem_t *done;
sem_t *dead; sem_t *dead;
sem_t *hold; sem_t *hold;

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/21 23:42:42 by djonker \___)=(___/ */ /* Updated: 2023/05/22 05:47:07 by djonker \___)=(___/ */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -98,6 +98,7 @@ int ft_releasetheminds(t_strct *strct, int m)
i = 0; i = 0;
if (m) if (m)
{ {
strct->strtt = ft_time();
while (i < strct->philos) while (i < strct->philos)
{ {
sem_post(strct->hold); sem_post(strct->hold);
@ -123,6 +124,7 @@ int ft_releasetheminds(t_strct *strct, int m)
void ft_safeprint(t_philo *ps, char *str) void ft_safeprint(t_philo *ps, char *str)
{ {
sem_wait(ps->print); sem_wait(ps->print);
printf("%lld %d %s\n", ft_time() - *ps->strtt, ps->id, str); if (*ps->alive)
printf("%lld %d %s\n", ft_time() - *ps->strtt, ps->id, str);
sem_post(ps->print); sem_post(ps->print);
} }

View File

@ -6,7 +6,7 @@
/* 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/21 23:42:45 by djonker \___)=(___/ */ /* Updated: 2023/05/22 05:49:17 by djonker \___)=(___/ */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,11 +15,11 @@
int ft_eatsleepthink(t_philo *ps) int ft_eatsleepthink(t_philo *ps)
{ {
sem_wait(ps->forks); sem_wait(ps->forks);
if (ft_time() - ps->lastfood > *ps->dietime) /*if (ft_time() - ps->lastfood > *ps->dietime)*/
return (1); /*return (1);*/
ft_safeprint(ps, "has taken a fork"); ft_safeprint(ps, "has taken a fork");
if (*ps->philos == 1) /*if (*ps->philos == 1)*/
return (1); /*return (1);*/
sem_wait(ps->forks); sem_wait(ps->forks);
ft_safeprint(ps, "has taken a fork"); ft_safeprint(ps, "has taken a fork");
ps->lastfood = ft_time(); ps->lastfood = ft_time();
@ -36,10 +36,24 @@ int ft_eatsleepthink(t_philo *ps)
ft_safeprint(ps, "is thinking"); ft_safeprint(ps, "is thinking");
ps->cycles++; ps->cycles++;
if (ps->cycles == *ps->target) if (ps->cycles == *ps->target)
return (2); sem_post(ps->done);
return (0); 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) void ft_cycle(void *pointer)
{ {
t_philo *philo; t_philo *philo;
@ -51,17 +65,14 @@ void ft_cycle(void *pointer)
sem_wait(philo->hold); sem_wait(philo->hold);
philo->dead = sem_open("dead", 0); philo->dead = sem_open("dead", 0);
philo->done = sem_open("done", 0); philo->done = sem_open("done", 0);
philo->print = sem_open("print", 0);
if ((philo->id - 1) % 2) if ((philo->id - 1) % 2)
usleep(*philo->eattime * 1000); usleep(*philo->eattime * 1000);
while (!ret) while (!ret)
ret = ft_eatsleepthink(philo); ret = ft_eatsleepthink(philo);
if (ret == 1) sem_wait(philo->print);
{ *philo->alive = 0;
printf("%lld %d died\n", ft_time() - *philo->strtt, philo->id); sem_post(philo->print);
sem_post(philo->dead);
}
if (ret == 2)
sem_post(philo->done);
} }
int ft_startcycle(t_strct *strct) int ft_startcycle(t_strct *strct)
@ -72,20 +83,25 @@ int ft_startcycle(t_strct *strct)
strct->strtt = ft_time(); strct->strtt = ft_time();
while (i < strct->philos) while (i < strct->philos)
{ {
strct->philo[i].lastfood = ft_time();
strct->pid[i] = fork(); strct->pid[i] = fork();
strct->alive = 1;
if (strct->pid[i] == 0) if (strct->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]); ft_cycle(&strct->philo[i]);
sem_close(strct->dead); sem_close(strct->dead);
sem_close(strct->done); sem_close(strct->done);
sem_close(strct->hold); sem_close(strct->hold);
sem_close(strct->forks); sem_close(strct->forks);
sem_close(strct->print); sem_close(strct->print);
pthread_join(strct->philo[i].thrd, NULL);
free(strct); free(strct);
exit(0); exit(0);
} }
i++; i++;
usleep(1000);
} }
return (0); return (0);
} }
@ -94,6 +110,7 @@ int ft_cyclecounter(t_strct *strct)
{ {
int i; int i;
sem_t *done; sem_t *done;
sem_t *print;
int philos; int philos;
int target; int target;
@ -106,15 +123,17 @@ int ft_cyclecounter(t_strct *strct)
sem_close(strct->print); sem_close(strct->print);
free(strct); free(strct);
done = sem_open("done", 0); done = sem_open("done", 0);
print = sem_open("print", 0);
i = 0; i = 0;
while (i < philos) while (i < philos)
{ {
sem_wait(done); sem_wait(done);
i++; i++;
} }
printf("All %d philosophers have", philos);
printf(" finished all %d cycles\n", target);
sem_close(done); sem_close(done);
sem_wait(print);
printf("All philosophers have finished all %d cycles\n", target);
sem_close(print);
exit (0); exit (0);
} }

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/21 22:11:28 by djonker \___)=(___/ */ /* Updated: 2023/05/22 04:34:47 by djonker \___)=(___/ */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -81,6 +81,7 @@ int ft_cleanup(t_strct *strct)
kill(strct->pid[502], 15); kill(strct->pid[502], 15);
while (i < strct->philos) while (i < strct->philos)
{ {
pthread_join(strct->philo[i].thrd, NULL);
kill(strct->pid[i], 15); kill(strct->pid[i], 15);
i++; i++;
} }