Browse Source

no data race but lock order inversions

master
narnaud 3 years ago
parent
commit
22868fe55a
  1. 2
      philo/Makefile
  2. 20
      philo/philo_launcher.c
  3. 3
      philo/philo_threads.c
  4. 72
      philo/philo_utils.c
  5. 3
      philo/philosophers.h

2
philo/Makefile

@ -4,7 +4,7 @@ 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 -fsanitize=thread -D_REENTRANT CFLAGS = -Werror -Wextra -Wall -g -D_REENTRANT
RM = rm -rf RM = rm -rf
.c.o: .c.o:

20
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/18 18:25:38 by narnaud@stude ### ########.fr */ /* Updated: 2022/05/18 19:27:01 by narnaud@stude ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,6 @@
void init_param(t_room *room, char **argv); void init_param(t_room *room, char **argv);
static t_room *init_room(char **argv); static t_room *init_room(char **argv);
static int clean_memory(t_room *room); static int clean_memory(t_room *room);
static int mini_atoi(char *str);
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
@ -29,7 +28,7 @@ int main(int argc, char **argv)
pthread_create(room->philos[room->i]->thd, NULL, \ pthread_create(room->philos[room->i]->thd, NULL, \
philos_birth, (void *)room); philos_birth, (void *)room);
pthread_detach(*room->philos[room->i]->thd); pthread_detach(*room->philos[room->i]->thd);
usleep(50); usleep(100);
pthread_mutex_lock(room->lock); pthread_mutex_lock(room->lock);
if ((room->i % 2) == 0) if ((room->i % 2) == 0)
{ {
@ -45,7 +44,7 @@ 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); pthread_mutex_unlock(room->lock);
sleep(2); sleep(2);
return (clean_memory(room)); return (clean_memory(room));
} }
@ -119,16 +118,3 @@ static int clean_memory(t_room *room)
free(room); free(room);
return (0); return (0);
} }
int mini_atoi(char *nbr)
{
int ret;
ret = 0;
while (*nbr >= '0' && *nbr <= '9')
{
ret = (*nbr - '0') + (10 * ret);
nbr++;
}
return (ret);
}

3
philo/philo_threads.c

@ -6,7 +6,7 @@
/* 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/18 18:26:14 by narnaud@stude ### ########.fr */ /* Updated: 2022/05/18 19:22:57 by narnaud@stude ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -87,7 +87,6 @@ void *philos_birth(void *r)
param = room->param; param = room->param;
pthread_mutex_lock(room->lock); pthread_mutex_lock(room->lock);
id = room->i; id = room->i;
printf("Philo %d: \n", id);
philo = room->philos[id]; philo = room->philos[id];
philo->eat_time = room_clock(); philo->eat_time = room_clock();
id_p = (id + 1) % param[PHILO_AMOUNT]; id_p = (id + 1) % param[PHILO_AMOUNT];

72
philo/philo_utils.c

@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud@student.42nice.fr <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/18 19:10:41 by narnaud@stude #+# #+# */
/* Updated: 2022/05/18 19:27:30 by narnaud@stude ### ########.fr */
/* */
/* ************************************************************************** */
#include "philosophers.h"
int is_running(t_room *room)
{
int ret;
pthread_mutex_lock(room->lock);
ret = room->running;
pthread_mutex_unlock(room->lock);
return (ret);
}
int safe_print(char *str, t_room *room, int id)
{
int ret;
ret = is_running(room);
if (!ret)
return (0);
printf(str, room_clock(), id);
return (ret);
}
int safe_wait(t_room *room, int start, int duration)
{
int ret;
ret = is_running(room);
while (ret && (room_clock() < (start + duration)))
{
ret = is_running(room);
usleep(50);
}
return (ret);
}
int room_clock()
{
struct timeval time;
int ret;
static long int start_time = 0;
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);
}
int mini_atoi(char *nbr)
{
int ret;
ret = 0;
while (*nbr >= '0' && *nbr <= '9')
{
ret = (*nbr - '0') + (10 * ret);
nbr++;
}
return (ret);
}

3
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/05/18 17:56:38 by narnaud@stude ### ########.fr */ /* Updated: 2022/05/18 19:26:02 by narnaud@stude ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -57,5 +57,6 @@ void *philos_birth(void *r);
int safe_print(char *str, t_room *room, int id); int safe_print(char *str, t_room *room, int id);
int safe_wait(t_room *room, int start, int duration); int safe_wait(t_room *room, int start, int duration);
int room_clock(); int room_clock();
int mini_atoi(char *nbr);
#endif #endif

Loading…
Cancel
Save