just the bonus

This commit is contained in:
Danny Jonker 2023-05-20 09:08:51 +02:00
parent 0197401222
commit c99ec1221d
5 changed files with 22 additions and 18 deletions

View File

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

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */
/* (| | )|_| |_| |> < */
/* 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);

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */
/* (| | )|_| |_| |> < */
/* 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;

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */
/* (| | )|_| |_| |> < */
/* 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);

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */
/* (| | )|_| |_| |> < */
/* 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);
}