diff --git a/philo/Makefile b/philo/Makefile index dc77960..72879b9 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -1,4 +1,4 @@ -SRCS = main.c parsing.c threads.c manage_threads.c utils_threads.c utils_manage_threads.c +SRCS = main.c parsing.c threads.c manage_threads.c utils_threads.c utils_manage_threads.c utils_threads2.c OBJS = ${SRCS:.c=.o} UTILS = utils/utils.a CC = clang @@ -132,6 +132,6 @@ coffee: bozo : @wget -q -O bozo.gif https://i.kym-cdn.com/photos/images/newsfeed/002/322/200/e51.gif @xdg-open bozo.gif - @@sleep 4.26 + @@sleep 2.13 @pkill eog @rm bozo.gif diff --git a/philo/parsing.c b/philo/parsing.c index c41bb2a..9686242 100644 --- a/philo/parsing.c +++ b/philo/parsing.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/08 14:59:29 by erey-bet #+# #+# */ -/* Updated: 2023/03/31 15:13:30 by erey-bet ### ########.fr */ +/* Updated: 2023/04/09 18:20:49 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,9 @@ int get_argv(char **argv, long *conf, char *message, int multiplier) if (ft_atoi_check(*argv)) { *conf = (long)ft_atoi(*argv) * multiplier; - if (*conf < 1) + if (*conf < 0) + return (error("Wrong number of ", message)); + else if (*conf < 1 && multiplier == 1) return (error("Wrong number of ", message)); return (0); } diff --git a/philo/philo.h b/philo/philo.h index 492d268..8e153a7 100644 --- a/philo/philo.h +++ b/philo/philo.h @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/08 14:51:16 by erey-bet #+# #+# */ -/* Updated: 2023/04/08 19:38:07 by erey-bet ### ########.fr */ +/* Updated: 2023/04/09 19:29:44 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -67,6 +67,10 @@ void message(t_philo *philo, char *msg); int init_mutex(pthread_mutex_t *mutex, int nbr); void destroy_mutex(pthread_mutex_t *mutex, int nbr); int free_all(pthread_t *threads, t_same *same, t_philo **philo); +int get_fork(t_philo *philo, int id_fork); +void take_fork(t_philo *philo, int id_fork); +void drop_fork(t_philo *philo, int id_fork, int next_id_fork); +void ft_sleep(long long time_to_sleep); /* THREADS */ void *philosopher(t_philo *philo); diff --git a/philo/threads.c b/philo/threads.c index dfc0fc0..d5c6714 100644 --- a/philo/threads.c +++ b/philo/threads.c @@ -6,42 +6,12 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/08 20:08:33 by erey-bet #+# #+# */ -/* Updated: 2023/04/07 15:19:34 by erey-bet ### ########.fr */ +/* Updated: 2023/04/09 19:45:30 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" -void take_fork(t_philo *philo, int id_fork) -{ - pthread_mutex_lock(&philo->same->mutex[id_fork]); - philo->same->forks[id_fork] = 0; - pthread_mutex_unlock(&philo->same->mutex[id_fork]); -} - -void drop_fork(t_philo *philo, int id_fork, int next_id_fork) -{ - pthread_mutex_lock(&philo->same->mutex[id_fork]); - philo->same->forks[id_fork] = 1; - pthread_mutex_unlock(&philo->same->mutex[id_fork]); - if (next_id_fork >= 0) - { - pthread_mutex_lock(&philo->same->mutex[next_id_fork]); - philo->same->forks[next_id_fork] = 1; - pthread_mutex_unlock(&philo->same->mutex[next_id_fork]); - } -} - -int get_fork(t_philo *philo, int id_fork) -{ - int fork; - - pthread_mutex_lock(&philo->same->mutex[id_fork]); - fork = philo->same->forks[id_fork]; - pthread_mutex_unlock(&philo->same->mutex[id_fork]); - return (fork); -} - int finish(t_philo *philo, long long last_eat_time) { int dead; @@ -77,9 +47,9 @@ void init_philo(t_philo *philo, t_only *only, message(philo, "is thinking\n"); if (get_utime() - only->last_eat_time + config->time_eat >= config->time_die) - usleep(config->time_die - (get_utime() - only->last_eat_time)); + ft_sleep(config->time_die - (get_utime() - only->last_eat_time)); else - usleep(config->time_eat / (only->id % 2 + 1)); + ft_sleep(config->time_eat / (only->id % 2 + 1)); } } @@ -89,7 +59,7 @@ int in_loop_eat_sleep(t_philo *philo, t_only *only, only->last_eat_time = get_utime(); if (!verif_die_eating(philo, only, config)) return (0); - usleep(config->time_eat); + ft_sleep(config->time_eat); if (verif_finish(philo, only)) return (0); if (config->must_eat > -1 && only->eat < config->must_eat) @@ -104,11 +74,11 @@ int in_loop_eat_sleep(t_philo *philo, t_only *only, if (get_utime() - only->last_eat_time + config->time_sleep >= config->time_die) { - usleep(config->time_die - (get_utime() - only->last_eat_time)); + ft_sleep(config->time_die - (get_utime() - only->last_eat_time)); return (0); } else - usleep(config->time_sleep); + ft_sleep(config->time_sleep); return (1); } @@ -117,23 +87,29 @@ void loop_philosopher(t_philo *philo, t_only *only, { while (!finish(philo, only->last_eat_time)) { - while (!finish(philo, only->last_eat_time) && get_fork(philo, only->id)) + while (!finish(philo, only->last_eat_time) && !get_fork(philo, only->id)) + ; + if (get_fork(philo, only->id)) { take_fork(philo, only->id); message(philo, "has taken a fork\n"); while (only->id == only->next_id && !finish(philo, only->last_eat_time)) ; - while (!finish(philo, only->last_eat_time) && get_fork(philo, only->next_id)) + while (!finish(philo, only->last_eat_time) && !get_fork(philo, only->next_id)) + ; + if (get_fork(philo, only->next_id)) { if (verif_finish(philo, only)) break ; + take_fork(philo, only->next_id); message(philo, "has taken a fork\n"); message(philo, "is eating\n"); if (!in_loop_eat_sleep(philo, only, config, nbr_philo)) break ; } - drop_fork(philo, only->id, -1); + else + drop_fork(philo, only->id, -1); } if (!finish(philo, only->last_eat_time)) message(philo, "is thinking\n"); diff --git a/philo/utils_manage_threads.c b/philo/utils_manage_threads.c index be29ccf..cffbe89 100644 --- a/philo/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/04/08 16:10:05 by erey-bet ### ########.fr */ +/* Updated: 2023/04/09 18:09:18 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,6 +55,8 @@ int free_all(pthread_t *threads, t_same *same, t_philo **philo) free(threads); if (same && same->mutex) free(same->mutex); + if (same && same->forks) + free(same->forks); if (same) free(same); if (philo) diff --git a/philo/utils_threads.c b/philo/utils_threads.c index 1d5b8b8..cd5db4e 100644 --- a/philo/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/08 16:05:23 by erey-bet ### ########.fr */ +/* Updated: 2023/04/09 19:29:54 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,8 +61,7 @@ int verif_finish(t_philo *philo, t_only *only) { if (finish(philo, only->last_eat_time)) { - pthread_mutex_unlock(&philo->same->mutex[only->next_id]); - pthread_mutex_unlock(&philo->same->mutex[only->id]); + drop_fork(philo, only->id, only->next_id); return (1); } return (0); @@ -74,8 +73,7 @@ int verif_die_eating(t_philo *philo, t_only *only, t_config *config) >= config->time_die) { usleep(config->time_die - (get_utime() - only->last_eat_time)); - pthread_mutex_unlock(&philo->same->mutex[only->next_id]); - pthread_mutex_unlock(&philo->same->mutex[only->id]); + drop_fork(philo, only->id, only->next_id); return (0); } return (1); diff --git a/philo/utils_threads2.c b/philo/utils_threads2.c new file mode 100644 index 0000000..c218069 --- /dev/null +++ b/philo/utils_threads2.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* forks_threads.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/09 20:49:44 by erey-bet #+# #+# */ +/* Updated: 2023/04/09 20:49:47 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo.h" + +void take_fork(t_philo *philo, int id_fork) +{ + pthread_mutex_lock(&philo->same->mutex[id_fork]); + philo->same->forks[id_fork] = 0; + pthread_mutex_unlock(&philo->same->mutex[id_fork]); +} + +void drop_fork(t_philo *philo, int id_fork, int next_id_fork) +{ + pthread_mutex_lock(&philo->same->mutex[id_fork]); + philo->same->forks[id_fork] = 1; + pthread_mutex_unlock(&philo->same->mutex[id_fork]); + if (next_id_fork >= 0) + { + pthread_mutex_lock(&philo->same->mutex[next_id_fork]); + philo->same->forks[next_id_fork] = 1; + pthread_mutex_unlock(&philo->same->mutex[next_id_fork]); + } +} + +int get_fork(t_philo *philo, int id_fork) +{ + int fork; + + pthread_mutex_lock(&philo->same->mutex[id_fork]); + fork = philo->same->forks[id_fork]; + pthread_mutex_unlock(&philo->same->mutex[id_fork]); + usleep(10); + return (fork); +} + +void ft_sleep(long long time_to_sleep) +{ + if (time_to_sleep < 0) + return ; + usleep(time_to_sleep); +}