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

6
philo/Makefile

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

9
philo/philo_launcher.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
{
pthread_create(room->philos[room->i]->thd, NULL, \
philos_life, (void *)room);
philos_birth, (void *)room);
pthread_detach(*room->philos[room->i]->thd);
usleep(50);
pthread_mutex_lock(room->lock);
@ -45,6 +45,8 @@ int main(int argc, char **argv)
usleep(CLOCK_TWO);
pthread_mutex_lock(room->lock);
room->running = 0;
pthread_mutex_lock(room->lock);
sleep(2);
return (clean_memory(room));
}
@ -93,8 +95,8 @@ static int clean_memory(t_room *room)
int *param;
static int i = 0;
pthread_mutex_lock(room->lock);
param = room->param;
sleep(1);
while (i < param[PHILO_AMOUNT])
{
@ -110,7 +112,6 @@ static int clean_memory(t_room *room)
free(room->philos[i]);
i++;
}
pthread_mutex_unlock(room->lock);
pthread_mutex_destroy(room->lock);
free(room->lock);

108
philo/philo_threads.c

@ -6,40 +6,12 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
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 *param;
@ -73,72 +45,58 @@ int waiter(t_room *room)
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);
if (!safe_print("%d %d has taken a fork\n", room, id))
{
pthread_mutex_unlock(philo->fork);
break ;
}
return (1);
pthread_mutex_lock(room->philos[id_p]->fork);
safe_print("%d %d has taken a fork\n", room, id);
if(!safe_print("%d %d is eating\n", room, id))
{
pthread_mutex_unlock(philo->fork);
pthread_mutex_unlock(room->philos[id_p]->fork);
break;
}
return (2);
pthread_mutex_lock(philo->lock);
philo->eat_time = room_clock();
pthread_mutex_unlock(philo->lock);
if (!safe_wait(room, room_clock(), param[EAT_DURATION]))
{
pthread_mutex_unlock(philo->fork);
pthread_mutex_unlock(room->philos[id_p]->fork);
break;
}
return (2);
pthread_mutex_unlock(philo->fork);
pthread_mutex_unlock(room->philos[id_p]->fork);
if(!safe_print("%d %d is sleeping\n", room, id))
break ;
return (-1);
pthread_mutex_lock(philo->lock);
philo->eat_amount++;
pthread_mutex_unlock(philo->lock);
if (!safe_wait(room, room_clock(), param[SLEEP_DURATION]))
break ;
return (-1);
if (!safe_print("%d %d is thinking\n", room, id))
break ;
}
return (NULL);
return (-1);
return (0);
}
int room_clock()
void *philos_birth(void *r)
{
struct timeval time;
int ret;
static long int start_time = 0;
t_room *room;
int *param;
t_philo *philo;
int id;
int id_p;
int stop;
gettimeofday(&time, NULL);
if (!start_time)
start_time = time.tv_sec * 1000 + time.tv_usec / 1000;
ret = (int)(time.tv_sec * 1000 + time.tv_usec / 1000 - start_time);
return (ret);
stop = 0;
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 (!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);
}

10
philo/philosophers.h

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
int waiter(t_room *room);
void *philos_life(void *r);
int room_clock();
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();
#endif

Loading…
Cancel
Save