looking like it

This commit is contained in:
Houtworm 2023-05-22 08:14:22 +02:00
parent f18301f738
commit e9c96f26b9
8 changed files with 134 additions and 126 deletions

View File

@ -1,4 +1,5 @@
# Philosophers
## Philosophers
- Fix people dying too soon with many many philos
---
## Tester

View File

@ -1,12 +1,12 @@
# **************************************************************************** #
# #
# .--. _ #
# Makefile |o_o || | #
# Makefile :+: :+: :+: #
# |:_/ || |_ _ ___ __ #
# By: djonker <djonker@student.codam.nl> // \ \ __| | | \ \/ / #
# (| | )|_| |_| |> < #
# Created: 2021/08/19 15:20:20 by djonker /'\_ _/`\__|\__,_/_/\_\ #
# Updated: 2023/05/22 00:43:58 by djonker \___)=(___/ #
# Updated: 2023/05/22 07:23:22 by houtworm ### ########.fr #
# #
# **************************************************************************** #

View File

@ -1,22 +1,23 @@
# **************************************************************************** #
# #
# .--. _ #
# Makefile |o_o || | #
# Makefile :+: :+: :+: #
# |:_/ || |_ _ ___ __ #
# By: djonker <djonker@student.codam.nl> // \ \ __| | | \ \/ / #
# (| | )|_| |_| |> < #
# Created: 2021/08/19 15:20:20 by djonker /'\_ _/`\__|\__,_/_/\_\ #
# Updated: 2023/05/14 09:48:37 by djonker \___)=(___/ #
# Updated: 2023/05/22 07:37:03 by houtworm ### ########.fr #
# #
# **************************************************************************** #
NAME =philo_bonus
CC =gcc
CFLAGS =-Wall -Werror -Wextra# -g -fsanitize=address
CFLAGS =-Wall -Werror -Wextra -g -fsanitize=address
RM =rm -f
SRC =src/philo.c\
src/init.c\
src/util.c
src/util.c\
src/watch.c
OBJ =$(SRC:src/%.c=obj/%.o)
all: $(NAME)

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* .--. _ */
/* philo.h |o_o || | */
/* philo.h :+: :+: :+: */
/* |:_/ || |_ _ ___ __ */
/* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */
/* (| | )|_| |_| |> < */
/* Created: 2023/03/11 07:19:18 by houtworm /'\_ _/`\__|\__,_/_/\_\ */
/* Updated: 2023/05/22 04:46:06 by djonker \___)=(___/ */
/* Updated: 2023/05/22 07:37:35 by houtworm ### ########.fr */
/* */
/* ************************************************************************** */
@ -72,6 +72,7 @@ void ft_cycle(void *pointer);
int ft_releasetheminds(t_strct *strct, int m);
int ft_cyclecounter(t_strct *strct);
int ft_waitingfordeath(t_strct *strct);
void *ft_alive(void *pointer);
int ft_cleanup(t_strct *strct);
long long ft_time(void);
int philo_atoi(char *str);

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */
/* (| | )|_| |_| |> < */
/* Created: 2023/03/15 02:00:19 by houtworm /'\_ _/`\__|\__,_/_/\_\ */
/* Updated: 2023/05/22 07:13:59 by houtworm ### ########.fr */
/* Updated: 2023/05/22 07:35:35 by houtworm ### ########.fr */
/* */
/* ************************************************************************** */
@ -91,36 +91,6 @@ int ft_initstructandmutex(t_strct *strct, int i)
return (0);
}
int ft_releasetheminds(t_strct *strct, int m)
{
int i;
i = 0;
if (m)
{
strct->strtt = ft_time();
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);
}
void ft_safeprint(t_philo *ps, char *str)
{
sem_wait(ps->print);

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */
/* (| | )|_| |_| |> < */
/* Created: 2023/03/11 06:42:31 by houtworm /'\_ _/`\__|\__,_/_/\_\ */
/* Updated: 2023/05/22 07:12:17 by houtworm ### ########.fr */
/* Updated: 2023/05/22 08:10:48 by houtworm ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,11 +15,7 @@
int ft_eatsleepthink(t_philo *ps)
{
sem_wait(ps->forks);
/*if (ft_time() - ps->lastfood > *ps->dietime)*/
/*return (1);*/
ft_safeprint(ps, "has taken a fork");
/*if (*ps->philos == 1)*/
/*return (1);*/
sem_wait(ps->forks);
ft_safeprint(ps, "has taken a fork");
ps->lastfood = ft_time();
@ -40,20 +36,6 @@ int ft_eatsleepthink(t_philo *ps)
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)
{
t_philo *philo;
@ -75,68 +57,35 @@ void ft_cycle(void *pointer)
sem_post(philo->print);
}
int ft_startcycle(t_strct *strct)
int ft_startcycle(t_strct *st)
{
int i;
i = 0;
strct->strtt = ft_time();
while (i < strct->philos)
st->strtt = ft_time();
while (i < st->philos)
{
strct->pid[i] = fork();
strct->alive = 1;
if (strct->pid[i] == 0)
st->pid[i] = fork();
st->alive = 1;
if (st->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]);
sem_close(strct->dead);
sem_close(strct->done);
sem_close(strct->hold);
sem_close(strct->forks);
sem_close(strct->print);
/*pthread_join(strct->philo[i].thrd, NULL);*/
free(strct);
st->philo[i].alive = &st->alive;
pthread_create(&st->philo[i].thrd, NULL, ft_alive, &st->philo[i]);
st->philo[i].lastfood = ft_time();
ft_cycle(&st->philo[i]);
sem_close(st->dead);
sem_close(st->done);
sem_close(st->hold);
sem_close(st->forks);
sem_close(st->print);
free(st);
exit(0);
}
i++;
usleep(1000);
}
return (0);
}
int ft_cyclecounter(t_strct *strct)
{
int i;
sem_t *done;
sem_t *print;
int philos;
int target;
philos = strct->philos;
target = strct->target;
sem_close(strct->dead);
sem_close(strct->done);
sem_close(strct->hold);
sem_close(strct->forks);
sem_close(strct->print);
free(strct);
done = sem_open("done", 0);
print = sem_open("print", 0);
i = 0;
while (i < philos)
{
sem_wait(done);
i++;
}
sem_close(done);
sem_wait(print);
printf("All philosophers have finished all %d cycles\n", target);
sem_close(print);
exit (0);
}
int main(int argc, char **argv)
{
t_strct *strct;

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> // \ \ __| | | \ \/ / */
/* (| | )|_| |_| |> < */
/* Created: 2023/03/15 02:01:41 by houtworm /'\_ _/`\__|\__,_/_/\_\ */
/* Updated: 2023/05/22 07:15:25 by houtworm ### ########.fr */
/* Updated: 2023/05/22 07:36:43 by houtworm ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,22 +28,6 @@ long long ft_time(void)
return (r);
}
int ft_waitingfordeath(t_strct *strct)
{
sem_t *dead;
sem_close(strct->dead);
sem_close(strct->done);
sem_close(strct->hold);
sem_close(strct->forks);
sem_close(strct->print);
free(strct);
dead = sem_open("dead", 0);
sem_wait(dead);
sem_close(dead);
exit (0);
}
int philo_atoi(char *str)
{
unsigned int r;
@ -81,7 +65,6 @@ int ft_cleanup(t_strct *strct)
kill(strct->pid[502], 15);
while (i < strct->philos)
{
/*pthread_join(strct->philo[i].thrd, NULL);*/
kill(strct->pid[i], 15);
i++;
}

103
philo_bonus/src/watch.c Normal file
View File

@ -0,0 +1,103 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* watch.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: houtworm <codam@houtworm.net> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/22 07:34:59 by houtworm #+# #+# */
/* Updated: 2023/05/22 08:09:16 by houtworm ### ########.fr */
/* */
/* ************************************************************************** */
#include "../philo.h"
int ft_releasetheminds(t_strct *strct, int m)
{
int i;
i = 0;
if (m)
{
/*strct->strtt = ft_time();*/
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);
}
int ft_cyclecounter(t_strct *strct)
{
int i;
sem_t *done;
sem_t *print;
int philos;
int target;
philos = strct->philos;
target = strct->target;
sem_close(strct->dead);
sem_close(strct->hold);
sem_close(strct->forks);
free(strct);
done = sem_open("done", 0);
print = sem_open("print", 0);
i = 0;
while (i < philos)
{
sem_wait(done);
i++;
}
sem_close(done);
sem_wait(print);
printf("All philosophers have finished all %d cycles\n", target);
sem_close(print);
exit (0);
}
void *ft_alive(void *pointer)
{
t_philo *philo;
philo = pointer;
usleep(*philo->dietime);
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);
}
int ft_waitingfordeath(t_strct *strct)
{
sem_t *dead;
sem_close(strct->dead);
sem_close(strct->done);
sem_close(strct->hold);
sem_close(strct->forks);
sem_close(strct->print);
free(strct);
dead = sem_open("dead", 0);
sem_wait(dead);
sem_close(dead);
exit (0);
}