narnaud 3 years ago
parent
commit
9dc46e46ff
  1. 6
      philo/Makefile
  2. 9
      philo/philo_launcher.c
  3. 108
      philo/philo_threads.c
  4. 8
      philo/philosophers.h

6
philo/Makefile

@ -1,10 +1,10 @@
NAME = philosophers NAME = philosophers
SRCS = philo_threads.c philo_launcher.c SRCS = philo_threads.c philo_launcher.c philo_utils.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
CC = clang CC = clang
CFLAGS = -Werror -Wextra -Wall -g -D_REENTRANT CFLAGS = -Werror -Wextra -Wall -g -fsanitize=thread -D_REENTRANT
RM = rm -rf RM = rm -rf
.c.o: .c.o:
@ -13,7 +13,7 @@ RM = rm -rf
all: $(NAME) all: $(NAME)
$(NAME): $(OBJS) $(NAME): $(OBJS)
${CC} ${CFLAGS} -g -fsanitize=thread -lpthread ${OBJS} -o ${NAME} ${CC} ${CFLAGS} -lpthread ${OBJS} -o ${NAME}
clean: clean:
${RM} ${OBJS} ${RM} ${OBJS}

9
philo/philo_launcher.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/24 15:44:04 by narnaud #+# #+# */ /* Created: 2021/11/24 15:44:04 by narnaud #+# #+# */
/* Updated: 2022/05/16 22:42:20 by narnaud ### ########.fr */ /* Updated: 2022/05/18 18:25:38 by narnaud@stude ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -27,7 +27,7 @@ int main(int argc, char **argv)
while (room->i >= 0) while (room->i >= 0)
{ {
pthread_create(room->philos[room->i]->thd, NULL, \ pthread_create(room->philos[room->i]->thd, NULL, \
philos_life, (void *)room); philos_birth, (void *)room);
pthread_detach(*room->philos[room->i]->thd); pthread_detach(*room->philos[room->i]->thd);
usleep(50); usleep(50);
pthread_mutex_lock(room->lock); pthread_mutex_lock(room->lock);
@ -45,6 +45,8 @@ int main(int argc, char **argv)
usleep(CLOCK_TWO); usleep(CLOCK_TWO);
pthread_mutex_lock(room->lock); pthread_mutex_lock(room->lock);
room->running = 0; room->running = 0;
pthread_mutex_lock(room->lock);
sleep(2);
return (clean_memory(room)); return (clean_memory(room));
} }
@ -93,8 +95,8 @@ static int clean_memory(t_room *room)
int *param; int *param;
static int i = 0; static int i = 0;
pthread_mutex_lock(room->lock);
param = room->param; param = room->param;
sleep(1);
while (i < param[PHILO_AMOUNT]) while (i < param[PHILO_AMOUNT])
{ {
@ -110,7 +112,6 @@ static int clean_memory(t_room *room)
free(room->philos[i]); free(room->philos[i]);
i++; i++;
} }
pthread_mutex_unlock(room->lock); pthread_mutex_unlock(room->lock);
pthread_mutex_destroy(room->lock); pthread_mutex_destroy(room->lock);
free(room->lock); free(room->lock);

108
philo/philo_threads.c

@ -6,40 +6,12 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/26 07:43:48 by narnaud #+# #+# */ /* Created: 2021/11/26 07:43:48 by narnaud #+# #+# */
/* Updated: 2022/05/16 22:28:37 by narnaud ### ########.fr */ /* Updated: 2022/05/18 18:26:14 by narnaud@stude ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "philosophers.h" #include "philosophers.h"
static int safe_print(char *str, t_room *room, int id)
{
int ret;
if (!room->running)
return (0);
pthread_mutex_lock(room->lock);
printf(str, room_clock(), id);
ret = room->running;
pthread_mutex_unlock(room->lock);
return (ret);
}
int safe_wait(t_room *room, int start, int duration)
{
int ret;
ret = room->running;
while (ret && (room_clock() < (start + duration)))
{
pthread_mutex_lock(room->lock);
ret = room->running;
pthread_mutex_unlock(room->lock);
usleep(50);
}
return (ret);
}
int waiter(t_room *room) int waiter(t_room *room)
{ {
int *param; int *param;
@ -73,72 +45,58 @@ int waiter(t_room *room)
return (ret); return (ret);
} }
void *philos_life(void *r) static int philo_life(t_room *room, t_philo *philo, int id, int id_p, int *param)
{
t_room *room;
int *param;
t_philo *philo;
int id;
int id_p;
room = (t_room *)r;
param = room->param;
pthread_mutex_lock(room->lock);
id = room->i;
printf("Philo %d: \n", id);
philo = room->philos[id];
philo->eat_time = room_clock();
id_p = (id + 1) % param[PHILO_AMOUNT];
pthread_mutex_unlock(room->lock);
while (1)
{ {
pthread_mutex_lock(philo->fork); pthread_mutex_lock(philo->fork);
if (!safe_print("%d %d has taken a fork\n", room, id)) if (!safe_print("%d %d has taken a fork\n", room, id))
{ return (1);
pthread_mutex_unlock(philo->fork);
break ;
}
pthread_mutex_lock(room->philos[id_p]->fork); pthread_mutex_lock(room->philos[id_p]->fork);
safe_print("%d %d has taken a fork\n", room, id); safe_print("%d %d has taken a fork\n", room, id);
if(!safe_print("%d %d is eating\n", room, id)) if(!safe_print("%d %d is eating\n", room, id))
{ return (2);
pthread_mutex_unlock(philo->fork);
pthread_mutex_unlock(room->philos[id_p]->fork);
break;
}
pthread_mutex_lock(philo->lock); pthread_mutex_lock(philo->lock);
philo->eat_time = room_clock(); philo->eat_time = room_clock();
pthread_mutex_unlock(philo->lock); pthread_mutex_unlock(philo->lock);
if (!safe_wait(room, room_clock(), param[EAT_DURATION])) if (!safe_wait(room, room_clock(), param[EAT_DURATION]))
{ return (2);
pthread_mutex_unlock(philo->fork);
pthread_mutex_unlock(room->philos[id_p]->fork);
break;
}
pthread_mutex_unlock(philo->fork); pthread_mutex_unlock(philo->fork);
pthread_mutex_unlock(room->philos[id_p]->fork); pthread_mutex_unlock(room->philos[id_p]->fork);
if(!safe_print("%d %d is sleeping\n", room, id)) if(!safe_print("%d %d is sleeping\n", room, id))
break ; return (-1);
pthread_mutex_lock(philo->lock); pthread_mutex_lock(philo->lock);
philo->eat_amount++; philo->eat_amount++;
pthread_mutex_unlock(philo->lock); pthread_mutex_unlock(philo->lock);
if (!safe_wait(room, room_clock(), param[SLEEP_DURATION])) if (!safe_wait(room, room_clock(), param[SLEEP_DURATION]))
break ; return (-1);
if (!safe_print("%d %d is thinking\n", room, id)) if (!safe_print("%d %d is thinking\n", room, id))
break ; return (-1);
} return (0);
return (NULL);
} }
int room_clock() void *philos_birth(void *r)
{ {
struct timeval time; t_room *room;
int ret; int *param;
static long int start_time = 0; t_philo *philo;
int id;
int id_p;
int stop;
gettimeofday(&time, NULL); stop = 0;
if (!start_time) room = (t_room *)r;
start_time = time.tv_sec * 1000 + time.tv_usec / 1000; param = room->param;
ret = (int)(time.tv_sec * 1000 + time.tv_usec / 1000 - start_time); pthread_mutex_lock(room->lock);
return (ret); id = room->i;
printf("Philo %d: \n", id);
philo = room->philos[id];
philo->eat_time = room_clock();
id_p = (id + 1) % param[PHILO_AMOUNT];
pthread_mutex_unlock(room->lock);
while (!stop)
stop = philo_life(room, philo, id, id_p, param);
if (stop-- > 0)
pthread_mutex_unlock(philo->fork);
if (stop-- > 0)
pthread_mutex_unlock(room->philos[id_p]->fork);
return (NULL);
} }

8
philo/philosophers.h

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/24 15:44:06 by narnaud #+# #+# */ /* Created: 2021/11/24 15:44:06 by narnaud #+# #+# */
/* Updated: 2022/03/31 12:37:23 by narnaud ### ########.fr */ /* Updated: 2022/05/18 17:56:38 by narnaud@stude ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -51,7 +51,11 @@ typedef struct s_room
} t_room; } t_room;
int waiter(t_room *room); int waiter(t_room *room);
void *philos_life(void *r);
void *philos_birth(void *r);
int safe_print(char *str, t_room *room, int id);
int safe_wait(t_room *room, int start, int duration);
int room_clock(); int room_clock();
#endif #endif

Loading…
Cancel
Save