bonus_done
This commit is contained in:
parent
8b78ddb705
commit
d8f7f4ce0f
@ -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/20 09:47:03 by djonker \___)=(___/ */
|
/* Updated: 2023/05/24 01:08:23 by djonker \___)=(___/ */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -89,7 +89,7 @@ int ft_liveordie(t_strct *stct, int i)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
pthread_mutex_unlock(&stct->drlastfood);
|
pthread_mutex_unlock(&stct->drlastfood);
|
||||||
usleep(100);
|
usleep(1);
|
||||||
i++;
|
i++;
|
||||||
pthread_mutex_lock(&stct->drdone);
|
pthread_mutex_lock(&stct->drdone);
|
||||||
}
|
}
|
||||||
|
@ -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 23:43:36 by djonker \___)=(___/ */
|
/* Updated: 2023/05/24 01:09:41 by djonker \___)=(___/ */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -91,3 +91,4 @@ void ft_aliveprint(t_philo *ps, char *str)
|
|||||||
pthread_mutex_unlock(ps->drdead);
|
pthread_mutex_unlock(ps->drdead);
|
||||||
printf("%lld %d %s\n", ft_time() - *ps->strtt, ps->id, str);
|
printf("%lld %d %s\n", ft_time() - *ps->strtt, ps->id, str);
|
||||||
}
|
}
|
||||||
|
// write?
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* .--. _ */
|
/* .--. _ */
|
||||||
/* philo.h :+: :+: :+: */
|
/* philo.h |o_o || | */
|
||||||
/* |:_/ || |_ _ ___ __ */
|
/* |:_/ || |_ _ ___ __ */
|
||||||
/* 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/22 07:37:35 by houtworm ### ########.fr */
|
/* Updated: 2023/05/24 01:38:55 by djonker \___)=(___/ */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -69,7 +69,7 @@ t_strct *ft_parseandvalidateinput(int argc, char **argv);
|
|||||||
int ft_initstructandmutex(t_strct *strct, int i);
|
int ft_initstructandmutex(t_strct *strct, int i);
|
||||||
int ft_startcycle(t_strct *strct);
|
int ft_startcycle(t_strct *strct);
|
||||||
void ft_cycle(void *pointer);
|
void ft_cycle(void *pointer);
|
||||||
int ft_releasetheminds(t_strct *strct, int m);
|
int ft_releasetheminds(t_strct *strct);
|
||||||
int ft_cyclecounter(t_strct *strct);
|
int ft_cyclecounter(t_strct *strct);
|
||||||
int ft_waitingfordeath(t_strct *strct);
|
int ft_waitingfordeath(t_strct *strct);
|
||||||
void *ft_alive(void *pointer);
|
void *ft_alive(void *pointer);
|
||||||
|
@ -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/24 00:23:31 by djonker \___)=(___/ */
|
/* Updated: 2023/05/24 02:14:22 by djonker \___)=(___/ */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -62,14 +62,30 @@ t_strct *ft_parseandvalidateinput(int argc, char **argv)
|
|||||||
return (strct);
|
return (strct);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ft_initstructandmutex(t_strct *strct, int i)
|
int ft_restoresemaphore(t_strct *strct)
|
||||||
{
|
{
|
||||||
ft_releasetheminds(strct, 0);
|
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");
|
||||||
strct->dead = sem_open("dead", O_CREAT | O_EXCL, 0666, 0);
|
strct->dead = sem_open("dead", O_CREAT | O_EXCL, 0666, 0);
|
||||||
strct->done = sem_open("done", 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->hold = sem_open("hold", O_CREAT | O_EXCL, 0666, 0);
|
||||||
strct->print = sem_open("print", O_CREAT | O_EXCL, 0666, 1);
|
strct->print = sem_open("print", O_CREAT | O_EXCL, 0666, 1);
|
||||||
strct->forks = sem_open("forks", O_CREAT | O_EXCL, 0666, strct->philos);
|
strct->forks = sem_open("forks", O_CREAT | O_EXCL, 0666, strct->philos);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_initstructandmutex(t_strct *strct, int i)
|
||||||
|
{
|
||||||
|
ft_restoresemaphore(strct);
|
||||||
|
strct->alive = 1;
|
||||||
while (i <= strct->philos)
|
while (i <= strct->philos)
|
||||||
{
|
{
|
||||||
strct->philo[i].id = i + 1;
|
strct->philo[i].id = i + 1;
|
||||||
@ -80,6 +96,7 @@ int ft_initstructandmutex(t_strct *strct, int i)
|
|||||||
strct->philo[i].strtt = &strct->strtt;
|
strct->philo[i].strtt = &strct->strtt;
|
||||||
strct->philo[i].philos = &strct->philos;
|
strct->philo[i].philos = &strct->philos;
|
||||||
strct->philo[i].target = &strct->target;
|
strct->philo[i].target = &strct->target;
|
||||||
|
strct->philo[i].alive = &strct->alive;
|
||||||
strct->philo[i].forks = strct->forks;
|
strct->philo[i].forks = strct->forks;
|
||||||
strct->philo[i].done = strct->done;
|
strct->philo[i].done = strct->done;
|
||||||
strct->philo[i].dead = strct->dead;
|
strct->philo[i].dead = strct->dead;
|
||||||
|
@ -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/24 00:29:03 by djonker \___)=(___/ */
|
/* Updated: 2023/05/24 02:15:07 by djonker \___)=(___/ */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -63,15 +63,13 @@ int ft_startcycle(t_strct *st)
|
|||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
st->strtt = ft_time();
|
st->strtt = ft_time();
|
||||||
st->alive = 1;
|
|
||||||
while (i < st->philos)
|
while (i < st->philos)
|
||||||
{
|
{
|
||||||
st->pid[i] = fork();
|
st->pid[i] = fork();
|
||||||
if (st->pid[i] == 0)
|
if (st->pid[i] == 0)
|
||||||
{
|
{
|
||||||
st->philo[i].alive = &st->alive;
|
|
||||||
pthread_create(&st->philo[i].thrd, NULL, ft_alive, &st->philo[i]);
|
|
||||||
st->philo[i].lastfood = ft_time();
|
st->philo[i].lastfood = ft_time();
|
||||||
|
pthread_create(&st->philo[i].thrd, NULL, ft_alive, &st->philo[i]);
|
||||||
ft_cycle(&st->philo[i]);
|
ft_cycle(&st->philo[i]);
|
||||||
sem_close(st->dead);
|
sem_close(st->dead);
|
||||||
sem_close(st->done);
|
sem_close(st->done);
|
||||||
@ -108,7 +106,7 @@ int main(int argc, char **argv)
|
|||||||
strct->pid[502] = fork();
|
strct->pid[502] = fork();
|
||||||
if (strct->pid[502] == 0)
|
if (strct->pid[502] == 0)
|
||||||
ft_cyclecounter(strct);
|
ft_cyclecounter(strct);
|
||||||
ft_releasetheminds(strct, 1);
|
ft_releasetheminds(strct);
|
||||||
while (i != strct->pid[501] && i != strct->pid[502])
|
while (i != strct->pid[501] && i != strct->pid[502])
|
||||||
i = waitpid(-1, NULL, 0);
|
i = waitpid(-1, NULL, 0);
|
||||||
ft_cleanup(strct);
|
ft_cleanup(strct);
|
||||||
|
@ -6,38 +6,22 @@
|
|||||||
/* By: djonker <djonker@student.codam.nl> // \ \ __| | | \ \/ / */
|
/* By: djonker <djonker@student.codam.nl> // \ \ __| | | \ \/ / */
|
||||||
/* (| | )|_| |_| |> < */
|
/* (| | )|_| |_| |> < */
|
||||||
/* Created: 2023/05/24 00:23:22 by djonker /'\_ _/`\__|\__,_/_/\_\ */
|
/* Created: 2023/05/24 00:23:22 by djonker /'\_ _/`\__|\__,_/_/\_\ */
|
||||||
/* Updated: 2023/05/24 00:28:28 by djonker \___)=(___/ */
|
/* Updated: 2023/05/24 02:24:11 by djonker \___)=(___/ */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../philo.h"
|
#include "../philo.h"
|
||||||
|
|
||||||
int ft_releasetheminds(t_strct *strct, int m)
|
int ft_releasetheminds(t_strct *strct)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
if (m)
|
|
||||||
{
|
|
||||||
while (i < (strct->philos * 2))
|
while (i < (strct->philos * 2))
|
||||||
{
|
{
|
||||||
sem_post(strct->hold);
|
sem_post(strct->hold);
|
||||||
i++;
|
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);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,8 +57,14 @@ int ft_cyclecounter(t_strct *strct)
|
|||||||
void *ft_alive(void *pointer)
|
void *ft_alive(void *pointer)
|
||||||
{
|
{
|
||||||
t_philo *philo;
|
t_philo *philo;
|
||||||
|
sem_t *hold;
|
||||||
|
sem_t *print;
|
||||||
|
|
||||||
philo = pointer;
|
philo = pointer;
|
||||||
|
sem_close(philo->hold);
|
||||||
|
sem_close(philo->print);
|
||||||
|
hold = sem_open("hold", 0);
|
||||||
|
print = sem_open("print", 0);
|
||||||
sem_wait(philo->hold);
|
sem_wait(philo->hold);
|
||||||
while (ft_time() - philo->lastfood < *philo->dietime)
|
while (ft_time() - philo->lastfood < *philo->dietime)
|
||||||
usleep(500);
|
usleep(500);
|
||||||
|
Loading…
Reference in New Issue
Block a user