diff --git a/philo/Makefile b/philo/Makefile index 7e76b66..d9cd37c 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -4,7 +4,7 @@ SRCS = philo_threads.c philo_launcher.c philo_utils.c OBJS = ${SRCS:.c=.o} CC = clang -CFLAGS = -Werror -Wextra -Wall -g -fsanitize=thread -D_REENTRANT +CFLAGS = -Werror -Wextra -Wall -g -D_REENTRANT RM = rm -rf .c.o: diff --git a/philo/philo_launcher.c b/philo/philo_launcher.c index 596671d..7397eae 100644 --- a/philo/philo_launcher.c +++ b/philo/philo_launcher.c @@ -6,16 +6,15 @@ /* 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 */ /* */ /* ************************************************************************** */ #include "philosophers.h" -void init_param(t_room *room, char **argv); +void init_param(t_room *room, char **argv); static t_room *init_room(char **argv); -static int clean_memory(t_room *room); -static int mini_atoi(char *str); +static int clean_memory(t_room *room); int main(int argc, char **argv) { @@ -29,7 +28,7 @@ int main(int argc, char **argv) pthread_create(room->philos[room->i]->thd, NULL, \ philos_birth, (void *)room); pthread_detach(*room->philos[room->i]->thd); - usleep(50); + usleep(100); pthread_mutex_lock(room->lock); if ((room->i % 2) == 0) { @@ -45,7 +44,7 @@ int main(int argc, char **argv) usleep(CLOCK_TWO); pthread_mutex_lock(room->lock); room->running = 0; - pthread_mutex_lock(room->lock); + pthread_mutex_unlock(room->lock); sleep(2); return (clean_memory(room)); } @@ -119,16 +118,3 @@ static int clean_memory(t_room *room) free(room); return (0); } - -int mini_atoi(char *nbr) -{ - int ret; - - ret = 0; - while (*nbr >= '0' && *nbr <= '9') - { - ret = (*nbr - '0') + (10 * ret); - nbr++; - } -return (ret); -} diff --git a/philo/philo_threads.c b/philo/philo_threads.c index 6f9aea1..d2e05a8 100644 --- a/philo/philo_threads.c +++ b/philo/philo_threads.c @@ -6,7 +6,7 @@ /* 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; 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]; diff --git a/philo/philo_utils.c b/philo/philo_utils.c new file mode 100644 index 0000000..7c6adde --- /dev/null +++ b/philo/philo_utils.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* philo_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud@student.42nice.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); +} diff --git a/philo/philosophers.h b/philo/philosophers.h index a3d9d71..e98c901 100644 --- a/philo/philosophers.h +++ b/philo/philosophers.h @@ -6,7 +6,7 @@ /* 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_wait(t_room *room, int start, int duration); int room_clock(); +int mini_atoi(char *nbr); #endif