diff --git a/Makefile b/philo/Makefile similarity index 95% rename from Makefile rename to philo/Makefile index 4ecbcf9..dc77960 100644 --- a/Makefile +++ b/philo/Makefile @@ -1,7 +1,5 @@ -SRCS = mandatory/main.c mandatory/parsing.c mandatory/threads.c mandatory/manage_threads.c mandatory/utils_threads.c mandatory/utils_manage_threads.c -SRCS_BONUS = +SRCS = main.c parsing.c threads.c manage_threads.c utils_threads.c utils_manage_threads.c OBJS = ${SRCS:.c=.o} -OBJS_BONUS = ${SRCS_BONUS:.c=.o} UTILS = utils/utils.a CC = clang CFLAGS = -g -Wall -Wextra -Werror -pthread @@ -13,10 +11,6 @@ ${NAME}: ${OBJS} make -C utils ${CC} ${CFLAGS} -o ${NAME} ${OBJS} ${UTILS} -bonus: ${OBJS_BONUS} - make -C utils - ${CC} ${CFLAGS} -o ${NAME} ${OBJS_BONUS} ${UTILS} - clean: rm -f ${OBJS} rm -f ${OBJS_BONUS} diff --git a/mandatory/main.c b/philo/main.c similarity index 88% rename from mandatory/main.c rename to philo/main.c index 83afa32..715da00 100644 --- a/mandatory/main.c +++ b/philo/main.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/24 14:21:54 by erey-bet #+# #+# */ -/* Updated: 2023/03/30 00:07:31 by erey-bet ### ########.fr */ +/* Updated: 2023/04/06 12:32:25 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,9 +16,9 @@ int main(int argc, char *argv[]) { t_config config; - if (argc < 5) + if (argc < 5 || argc > 6) { - write(2, "Not enought argument\n", 21); + write(2, "Wrong number of argument\n", 25); return (1); } if (parsing(argv, &config)) diff --git a/mandatory/manage_threads.c b/philo/manage_threads.c similarity index 96% rename from mandatory/manage_threads.c rename to philo/manage_threads.c index 95c0aa7..27d0df6 100644 --- a/mandatory/manage_threads.c +++ b/philo/manage_threads.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/29 20:44:30 by erey-bet #+# #+# */ -/* Updated: 2023/03/31 17:52:22 by erey-bet ### ########.fr */ +/* Updated: 2023/04/06 13:54:03 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,6 +32,7 @@ static t_same *create_struct_same(t_config *config) if (!same->all_eat) return (NULL); *same->all_eat = 0; + same->time = get_utime(); return (same); } diff --git a/mandatory/parsing.c b/philo/parsing.c similarity index 100% rename from mandatory/parsing.c rename to philo/parsing.c diff --git a/mandatory/philo.h b/philo/philo.h similarity index 94% rename from mandatory/philo.h rename to philo/philo.h index fe0fdb2..6249365 100644 --- a/mandatory/philo.h +++ b/philo/philo.h @@ -6,14 +6,14 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/08 14:51:16 by erey-bet #+# #+# */ -/* Updated: 2023/04/01 11:58:47 by erey-bet ### ########.fr */ +/* Updated: 2023/04/06 13:59:01 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef PHILO_H # define PHILO_H -# include "../utils/utils.h" +# include "utils/utils.h" # include # include # include @@ -37,14 +37,13 @@ typedef struct s_same pthread_mutex_t *mutex; int *all_eat; int *death; - + long long time; } t_same; typedef struct s_only { int id; int next_id; - long long time; long long last_eat_time; int eat; } t_only; diff --git a/mandatory/threads.c b/philo/threads.c similarity index 97% rename from mandatory/threads.c rename to philo/threads.c index e0fd093..f9c6284 100644 --- a/mandatory/threads.c +++ b/philo/threads.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/08 20:08:33 by erey-bet #+# #+# */ -/* Updated: 2023/04/01 11:57:59 by erey-bet ### ########.fr */ +/* Updated: 2023/04/06 13:50:41 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,8 +40,7 @@ void init_philo(t_philo *philo, t_only *only, { *nbr_philo = philo->same->config->nbr_philo; only->next_id = (only->id + 1) % *nbr_philo; - only->time = get_utime(); - only->last_eat_time = only->time; + only->last_eat_time = get_utime(); if (only->id % 2 == 1 || (*nbr_philo % 2 == 1 && only->id == *nbr_philo - 1)) { @@ -57,10 +56,10 @@ void init_philo(t_philo *philo, t_only *only, int in_loop_eat_sleep(t_philo *philo, t_only *only, t_config *config, int nbr_philo) { + only->last_eat_time = get_utime(); if (!verif_die_eating(philo, only, config)) return (0); usleep(config->time_eat); - only->last_eat_time = get_utime(); if (verif_finish(philo, only)) return (0); if (config->must_eat > -1 && only->eat < config->must_eat) diff --git a/utils/Makefile b/philo/utils/Makefile similarity index 100% rename from utils/Makefile rename to philo/utils/Makefile diff --git a/utils/ft_atoi.c b/philo/utils/ft_atoi.c similarity index 100% rename from utils/ft_atoi.c rename to philo/utils/ft_atoi.c diff --git a/utils/ft_atoi_check.c b/philo/utils/ft_atoi_check.c similarity index 100% rename from utils/ft_atoi_check.c rename to philo/utils/ft_atoi_check.c diff --git a/utils/ft_bzero.c b/philo/utils/ft_bzero.c similarity index 100% rename from utils/ft_bzero.c rename to philo/utils/ft_bzero.c diff --git a/utils/ft_calloc.c b/philo/utils/ft_calloc.c similarity index 100% rename from utils/ft_calloc.c rename to philo/utils/ft_calloc.c diff --git a/utils/ft_itoa.c b/philo/utils/ft_itoa.c similarity index 100% rename from utils/ft_itoa.c rename to philo/utils/ft_itoa.c diff --git a/utils/ft_itos_.c b/philo/utils/ft_itos_.c similarity index 100% rename from utils/ft_itos_.c rename to philo/utils/ft_itos_.c diff --git a/utils/ft_lltos.c b/philo/utils/ft_lltos.c similarity index 100% rename from utils/ft_lltos.c rename to philo/utils/ft_lltos.c diff --git a/utils/ft_memset.c b/philo/utils/ft_memset.c similarity index 100% rename from utils/ft_memset.c rename to philo/utils/ft_memset.c diff --git a/utils/ft_putchar_fd.c b/philo/utils/ft_putchar_fd.c similarity index 100% rename from utils/ft_putchar_fd.c rename to philo/utils/ft_putchar_fd.c diff --git a/utils/ft_putnbr_fd.c b/philo/utils/ft_putnbr_fd.c similarity index 100% rename from utils/ft_putnbr_fd.c rename to philo/utils/ft_putnbr_fd.c diff --git a/utils/ft_putstr_fd.c b/philo/utils/ft_putstr_fd.c similarity index 100% rename from utils/ft_putstr_fd.c rename to philo/utils/ft_putstr_fd.c diff --git a/utils/ft_strcat.c b/philo/utils/ft_strcat.c similarity index 100% rename from utils/ft_strcat.c rename to philo/utils/ft_strcat.c diff --git a/utils/ft_strcpy.c b/philo/utils/ft_strcpy.c similarity index 100% rename from utils/ft_strcpy.c rename to philo/utils/ft_strcpy.c diff --git a/utils/ft_strlen.c b/philo/utils/ft_strlen.c similarity index 100% rename from utils/ft_strlen.c rename to philo/utils/ft_strlen.c diff --git a/utils/utils.h b/philo/utils/utils.h similarity index 100% rename from utils/utils.h rename to philo/utils/utils.h diff --git a/mandatory/utils_manage_threads.c b/philo/utils_manage_threads.c similarity index 71% rename from mandatory/utils_manage_threads.c rename to philo/utils_manage_threads.c index a77ad2c..8c7817b 100644 --- a/mandatory/utils_manage_threads.c +++ b/philo/utils_manage_threads.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/30 01:04:41 by erey-bet #+# #+# */ -/* Updated: 2023/03/31 17:51:30 by erey-bet ### ########.fr */ +/* Updated: 2023/04/06 13:57:40 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,17 +34,27 @@ void free_all(pthread_t *threads, t_same *same, t_philo **philo) { int i; - destroy_mutex(same->mutex, 3); - free(threads); - free(same->mutex); - free(same->death); - free(same->all_eat); - free(same); - i = -1; - while (philo[++i]) + destroy_mutex(same->mutex, same->config->nbr_philo + 2); + if (threads) + free(threads); + if (same && same->mutex) + free(same->mutex); + if (same && same->mutex) + free(same->death); + if (same && same->mutex) + free(same->all_eat); + if (same) + free(same); + if (philo) { - free(philo[i]->only); - free(philo[i]); + i = -1; + while (philo[++i]) + { + if (philo[i]->only) + free(philo[i]->only); + if (philo[i]) + free(philo[i]); + } + free(philo); } - free(philo); } diff --git a/mandatory/utils_threads.c b/philo/utils_threads.c similarity index 85% rename from mandatory/utils_threads.c rename to philo/utils_threads.c index 50df9d9..659681c 100644 --- a/mandatory/utils_threads.c +++ b/philo/utils_threads.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/29 20:44:51 by erey-bet #+# #+# */ -/* Updated: 2023/04/01 12:08:51 by erey-bet ### ########.fr */ +/* Updated: 2023/04/06 13:59:13 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,10 +22,12 @@ long long get_utime(void) void message_die(t_philo *philo, char *msg) { - char send[1024]; - int i; + t_config *config; + char send[1024]; + int i; - i = ft_lltos(send, (get_utime() - philo->only->time) / 1000); + config = philo->same->config; + i = ft_lltos(send, (get_utime() - philo->same->time) / 1000); send[i++] = ' '; i += ft_lltos(send + i, philo->only->id + 1); send[i++] = ' '; @@ -36,10 +38,12 @@ void message_die(t_philo *philo, char *msg) void message(t_philo *philo, char *msg) { - char send[1024]; - int i; + t_config *config; + char send[1024]; + int i; - i = ft_lltos(send, (get_utime() - philo->only->time) / 1000); + config = philo->same->config; + i = ft_lltos(send, (get_utime() - philo->same->time) / 1000); send[i++] = ' '; i += ft_lltos(send + i, philo->only->id + 1); send[i++] = ' ';