This commit is contained in:
Danny Jonker 2023-05-21 23:43:48 +02:00
parent df7c2f7bf3
commit 0cbe5aa014
5 changed files with 33 additions and 23 deletions

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */
/* (| | )|_| |_| |> < */
/* Created: 2023/03/15 02:01:41 by houtworm /'\_ _/`\__|\__,_/_/\_\ */
/* Updated: 2023/05/20 10:04:39 by djonker \___)=(___/ */
/* Updated: 2023/05/21 23:43:36 by djonker \___)=(___/ */
/* */
/* ************************************************************************** */
@ -91,10 +91,3 @@ 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);
}
/*• Test 1 800 200 200. The philosopher should not eat and should die.*/
/*• Test 5 800 200 200. No philosopher should die.*/
/*• Test 5 800 200 200 7. No philosopher should die and the simulation should stop when every philosopher has eaten at least 7 times.*/
/*• Test 4 410 200 200. No philosopher should die.*/
/*• Test 4 310 200 100. One philosopher should die.*/

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */
/* (| | )|_| |_| |> < */
/* Created: 2023/03/11 07:19:18 by houtworm /'\_ _/`\__|\__,_/_/\_\ */
/* Updated: 2023/05/14 08:55:03 by djonker \___)=(___/ */
/* Updated: 2023/05/21 22:38:50 by djonker \___)=(___/ */
/* */
/* ************************************************************************** */
@ -65,7 +65,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 ft_releasetheminds(t_strct *strct, int m);
int ft_cyclecounter(t_strct *strct);
int ft_waitingfordeath(t_strct *strct);
int ft_cleanup(t_strct *strct);

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */
/* (| | )|_| |_| |> < */
/* Created: 2023/03/15 02:00:19 by houtworm /'\_ _/`\__|\__,_/_/\_\ */
/* Updated: 2023/05/14 08:55:41 by djonker \___)=(___/ */
/* Updated: 2023/05/21 23:42:42 by djonker \___)=(___/ */
/* */
/* ************************************************************************** */
@ -64,11 +64,12 @@ t_strct *ft_parseandvalidateinput(int argc, char **argv)
int ft_initstructandmutex(t_strct *strct, int i)
{
strct->dead = sem_open("dead", O_CREAT, 0666, 0);
strct->done = sem_open("done", O_CREAT, 0666, 0);
strct->hold = sem_open("hold", O_CREAT, 0666, 0);
strct->print = sem_open("print", O_CREAT, 0666, 1);
strct->forks = sem_open("forks", O_CREAT, 0666, strct->philos);
ft_releasetheminds(strct, 0);
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);
while (i <= strct->philos)
{
strct->philo[i].id = i + 1;
@ -90,15 +91,31 @@ int ft_initstructandmutex(t_strct *strct, int i)
return (0);
}
int ft_releasetheminds(t_strct *strct)
int ft_releasetheminds(t_strct *strct, int m)
{
int i;
i = 0;
while (i < strct->philos)
if (m)
{
sem_post(strct->hold);
i++;
while (i < strct->philos)
{
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");
}
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */
/* (| | )|_| |_| |> < */
/* Created: 2023/03/11 06:42:31 by houtworm /'\_ _/`\__|\__,_/_/\_\ */
/* Updated: 2023/05/14 09:24:42 by djonker \___)=(___/ */
/* Updated: 2023/05/21 23:42:45 by djonker \___)=(___/ */
/* */
/* ************************************************************************** */
@ -140,7 +140,7 @@ int main(int argc, char **argv)
strct->pid[502] = fork();
if (strct->pid[502] == 0)
ft_cyclecounter(strct);
ft_releasetheminds(strct);
ft_releasetheminds(strct, 1);
while (i != strct->pid[501] && i != strct->pid[502])
i = waitpid(-1, NULL, 0);
ft_cleanup(strct);

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */
/* (| | )|_| |_| |> < */
/* Created: 2023/03/15 02:01:41 by houtworm /'\_ _/`\__|\__,_/_/\_\ */
/* Updated: 2023/05/14 09:13:53 by djonker \___)=(___/ */
/* Updated: 2023/05/21 22:11:28 by djonker \___)=(___/ */
/* */
/* ************************************************************************** */