diff options
| -rw-r--r-- | Makefile | 27 | ||||
| -rw-r--r-- | common/Makefile | 4 | ||||
| -rw-r--r-- | common/common.c | 4 | ||||
| -rw-r--r-- | common/common.h | 16 | ||||
| -rw-r--r-- | common/helper.c | 26 | ||||
| -rw-r--r-- | common/philo.c | 6 | ||||
| -rw-r--r-- | philo_one/Makefile | 6 | ||||
| -rw-r--r-- | philo_one/fork.c | 6 | ||||
| -rw-r--r-- | philo_one/main.c | 12 | ||||
| -rw-r--r-- | philo_one/philo.c | 9 | ||||
| -rw-r--r-- | philo_one/philo_one.h | 10 | ||||
| -rw-r--r-- | philo_one/routine.c | 48 |
12 files changed, 101 insertions, 73 deletions
@@ -6,31 +6,36 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/02/09 03:31:28 by cacharle #+# #+# # -# Updated: 2020/02/09 22:38:47 by cacharle ### ########.fr # +# Updated: 2020/02/14 00:48:59 by cacharle ### ########.fr # # # # **************************************************************************** # MAKE = make -MAKE_ARGS = -no-print-child +MAKE_ARGS = --no-print-directory +COMMON_DIR = common PHILO_ONE_DIR = philo_one PHILO_TWO_DIR = philo_two PHILO_THREE_DIR = philo_three help: - @echo "make common - build common lib" - @echo "make philo_one - compile philo_one" - @echo "make philo two - compile philo_one" - @echo "make philo three - compile philo_one" + @echo "make common - build common lib" + @echo "make philo_one - compile philo_one" + @echo "make philo_two - compile philo_one" + @echo "make philo_three - compile philo_one" +.PHONY: common common: - $(MAKE) $(MAKE_ARGS) $(COMMON_DIR) + $(MAKE) $(MAKE_ARGS) -C $(COMMON_DIR) +.PHONY: philo_one philo_one: common - $(MAKE) $(MAKE_ARGS) $(PHILO_ONE_DIR) + $(MAKE) $(MAKE_ARGS) -C $(PHILO_ONE_DIR) +.PHONY: philo_two philo_two: common - $(MAKE) $(MAKE_ARGS) $(PHILO_TWO_DIR) + $(MAKE) $(MAKE_ARGS) -C $(PHILO_TWO_DIR) -philo_two: common - $(MAKE) $(MAKE_ARGS) $(PHILO_THREE_DIR) +.PHONY: philo_three +philo_three: common + $(MAKE) $(MAKE_ARGS) -C $(PHILO_THREE_DIR) diff --git a/common/Makefile b/common/Makefile index cf3089c..082d66e 100644 --- a/common/Makefile +++ b/common/Makefile @@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/02/09 22:39:08 by cacharle #+# #+# # -# Updated: 2020/02/09 23:57:34 by cacharle ### ########.fr # +# Updated: 2020/02/14 00:49:18 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -14,7 +14,7 @@ LIB = ar rcs RM = rm -rf CC = gcc -CCFLAGS = -Wall -Wextra -Werror +CCFLAGS = -Wall -Wextra #-Werror NAME = libphilocommon.a diff --git a/common/common.c b/common/common.c index 03fc32f..0ec7f80 100644 --- a/common/common.c +++ b/common/common.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */ -/* Updated: 2020/02/09 23:57:20 by cacharle ### ########.fr */ +/* Updated: 2020/02/14 01:32:37 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,6 @@ t_bool parse_args(t_philo_args *philo_args, int argc, char **argv) { - if (philo_args == NULL) - return (FALSE); if (argc != 5 && argc != 6) return (FALSE); if ((philo_args->philo_num = h_strtoposint(argv[1])) == -1 || diff --git a/common/common.h b/common/common.h index def49cf..e314960 100644 --- a/common/common.h +++ b/common/common.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 22:58:35 by cacharle #+# #+# */ -/* Updated: 2020/02/09 23:56:48 by cacharle ### ########.fr */ +/* Updated: 2020/02/14 00:35:30 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ # define TRUE 1 typedef int t_bool; +typedef long int t_time; typedef enum { @@ -43,9 +44,9 @@ typedef enum typedef struct { int philo_num; - int timeout_death; - int timeout_eat; - int timeout_sleep; + t_time timeout_death; + t_time timeout_eat; + t_time timeout_sleep; int meal_num; } t_philo_args; @@ -62,8 +63,8 @@ void philo_put_state_change(int id, t_philo_event event); ** philo.c */ -void philo_eat(int id, int timeout); -void philo_sleep(int id, int timeout); +void philo_eat(int id, t_time timeout); +void philo_sleep(int id, t_time timeout); void philo_think(int id); void philo_die(int id); @@ -71,11 +72,12 @@ void philo_die(int id); ** helper.c */ -int h_strtoposint(char *s); +long int h_strtoposint(char *s); int h_strlen(char *s); void h_putnbr(unsigned long num); void h_putchar(char c); void h_putstr(char *s); void *h_calloc(int count, int size); +t_time h_timeval_to_time(struct timeval *tp); #endif diff --git a/common/helper.c b/common/helper.c index 590c415..afdb7f3 100644 --- a/common/helper.c +++ b/common/helper.c @@ -6,20 +6,20 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ -/* Updated: 2020/02/10 01:15:05 by cacharle ### ########.fr */ +/* Updated: 2020/02/14 01:37:31 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "common.h" -int h_strtoposint(char *s) +long int h_strtoposint(char *s) { - int num; + long int num; if (*s < '0' || *s > '9') return (-1); num = 0; - while (*s != '\0' && *s > '0' && *s < '9') + while (*s >= '0' && *s <= '9') { num *= 10; num += *s - '0'; @@ -40,12 +40,11 @@ int h_strlen(char *s) return (counter); } -void h_putnbr(unsigned long num) +void h_putnbr(unsigned long int num) { - if (num <= 0) - return ; - h_putnbr(num / 10); - h_putchar(num % 10 - '0'); + if (num > 9) + h_putnbr(num / 10); + h_putchar(num % 10 + '0'); } void h_putchar(char c) @@ -70,3 +69,12 @@ void *h_calloc(int count, int size) ((unsigned char*)ptr)[i] = 0x0; return (ptr); } + +t_time h_timeval_to_time(struct timeval *tp) +{ + t_time t; + + t = tp->tv_sec * 1000; + t += tp->tv_usec / 1000; + return (t); +} diff --git a/common/philo.c b/common/philo.c index 484257e..dfc9278 100644 --- a/common/philo.c +++ b/common/philo.c @@ -6,19 +6,19 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 01:54:53 by cacharle #+# #+# */ -/* Updated: 2020/02/09 23:56:26 by cacharle ### ########.fr */ +/* Updated: 2020/02/14 00:29:17 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "common.h" -void philo_eat(int id, int timeout) +void philo_eat(int id, t_time timeout) { philo_put_state_change(id, EVENT_EATING); usleep(timeout * 1000); } -void philo_sleep(int id, int timeout) +void philo_sleep(int id, t_time timeout) { philo_put_state_change(id, EVENT_SLEEPING); usleep(timeout * 1000); diff --git a/philo_one/Makefile b/philo_one/Makefile index e647316..b043ab5 100644 --- a/philo_one/Makefile +++ b/philo_one/Makefile @@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/11/24 05:50:15 by cacharle #+# #+# # -# Updated: 2020/02/10 00:16:03 by cacharle ### ########.fr # +# Updated: 2020/02/14 00:50:47 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -22,7 +22,9 @@ NAME = philo_one SRC = main.c \ philo.c \ - fork.c + fork.c \ + routine.c + OBJ = $(SRC:.c=.o) all: $(NAME) diff --git a/philo_one/fork.c b/philo_one/fork.c index d42510b..62e3355 100644 --- a/philo_one/fork.c +++ b/philo_one/fork.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 23:46:40 by cacharle #+# #+# */ -/* Updated: 2020/02/10 00:34:17 by cacharle ### ########.fr */ +/* Updated: 2020/02/14 01:29:26 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,7 +36,7 @@ void forks_destroy(t_fork *forks, int num) while (num-- > 0) { forks[num].used = TRUE; - pthread_mutex_destroy(&forks[num].mutex); + /* pthread_mutex_destroy(&forks[num].mutex); */ } } @@ -52,7 +52,7 @@ t_routine_arg *forks_dispatch(t_philo *philos, t_fork *forks, t_philo_args *args { routine_args[i].args = args; routine_args[i].philo = philos + i; - routine_args[i].fork_left = forks + i % args->philo_num; + routine_args[i].fork_right = forks + i % args->philo_num; routine_args[i].fork_left = forks + (i - 1) % args->philo_num; } return (routine_args); diff --git a/philo_one/main.c b/philo_one/main.c index ef00a0b..94ac0f7 100644 --- a/philo_one/main.c +++ b/philo_one/main.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */ -/* Updated: 2020/02/10 01:29:50 by cacharle ### ########.fr */ +/* Updated: 2020/02/14 01:41:21 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,10 +19,12 @@ int main(int argc, char **argv) t_philo *philos; t_fork *forks; t_routine_arg *routine_args; + /* mutex_t mutex_stdout; */ + /* pthread_mutex_init(mutex_stdout); */ if (!parse_args(&philo_args, argc, argv)) return (1); - if ((forks = forks_new(philo_args.philo_num - 1)) == NULL) + if ((forks = forks_new(philo_args.philo_num)) == NULL) return (1); if ((philos = philos_new(philo_args.philo_num)) == NULL) { @@ -31,8 +33,8 @@ int main(int argc, char **argv) } if ((routine_args = forks_dispatch(philos, forks, &philo_args)) == NULL) { - free(forks); free(philos); + free(forks); return (1); } if (!philos_start(philos, routine_args, philo_args.philo_num)) @@ -46,8 +48,8 @@ int main(int argc, char **argv) if (philos_starved(philos, philo_args.philo_num)) break; philos_join(philos, philo_args.philo_num); - free(philos); - free(forks); free(routine_args); + free(philos); + forks_destroy(forks, philo_args.philo_num); return (0); } diff --git a/philo_one/philo.c b/philo_one/philo.c index 7cc3ca4..c585e34 100644 --- a/philo_one/philo.c +++ b/philo_one/philo.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */ -/* Updated: 2020/02/10 01:29:25 by cacharle ### ########.fr */ +/* Updated: 2020/02/14 01:38:45 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,14 +40,13 @@ void philos_destroy(t_philo *philos, int num) t_bool philos_start(t_philo *philos, t_routine_arg *routine_args, int num) { - t_routine_arg *routine_arg; int i; i = -1; while (++i < num) { philos[i].alive = TRUE; - if (pthread_create(philos[i].thread, NULL, philo_one_routine, (void*)(routine_args + i)) == -1) + if (pthread_create(&philos[i].thread, NULL, &routine_philo, (void*)(routine_args + i)) == -1) return (FALSE); } return (TRUE); @@ -63,7 +62,7 @@ void philos_join(t_philo *philos, int num) if (philos[i].alive) { philos[i].alive = FALSE; - pthread_join(philos[i].thread, NULL); + /* pthread_join(philos[i].thread, NULL); */ } } } @@ -74,6 +73,7 @@ t_bool philos_starved(t_philo *philos, int num) i = -1; while (++i < num) + { if (!philos[i].alive) { i = -1; @@ -81,5 +81,6 @@ t_bool philos_starved(t_philo *philos, int num) philos[i].alive = FALSE; return (TRUE); } + } return (FALSE); } diff --git a/philo_one/philo_one.h b/philo_one/philo_one.h index bcac634..9c888ee 100644 --- a/philo_one/philo_one.h +++ b/philo_one/philo_one.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */ -/* Updated: 2020/02/10 01:14:27 by cacharle ### ########.fr */ +/* Updated: 2020/02/14 01:41:39 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,7 +44,7 @@ struct s_philo int id; t_bool alive; t_watchdog watchdog; - int time_last_eat; + t_time time_last_eat; t_philo_state state; pthread_t thread; }; @@ -55,8 +55,8 @@ typedef struct t_philo *philo; t_fork *fork_left; t_fork *fork_right; + pthread_mutex_t *mutex_stdout; } t_routine_arg; -tine_death_arg; /* ** fork.c @@ -80,7 +80,7 @@ t_bool philos_starved(t_philo *philos, int num); ** routine.c */ -void *routine_philo(t_routine_arg *arg); -void *routine_death(t_routine_arg *arg); +void *routine_philo(void *void_arg); +void *routine_death(void *void_arg); #endif diff --git a/philo_one/routine.c b/philo_one/routine.c index 48f3c0d..713e4ee 100644 --- a/philo_one/routine.c +++ b/philo_one/routine.c @@ -6,48 +6,58 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */ -/* Updated: 2020/02/10 01:19:13 by cacharle ### ########.fr */ +/* Updated: 2020/02/14 00:45:23 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo_one.h" -void *routine_philo(t_routine_arg *routine_arg) +void *routine_philo(void *void_arg) { - pthread_t thread_death; + t_routine_arg *arg; + pthread_t thread_death; - if (pthread_create(&thread_death, NULL, routine_death, routine_arg) != 0) + arg = (t_routine_arg*)void_arg; + if (pthread_create(&thread_death, NULL, routine_death, arg) != 0) return (NULL); - philo_think(routine_arg->philo->id); - while (routine_arg->philo->alive && !philo_starved(routine_arg->philo)) + philo_think(arg->philo->id); + while (arg->philo->alive) { - if (!routine_arg->fork_left->used && !routine_arg->fork_right->used) + if (!arg->fork_left->used && !arg->fork_right->used) { - pthread_mutex_lock(&routine_arg->fork_left->mutex); - pthread_mutex_lock(&routine_arg->fork_right->mutex); - philo_eat(routine_arg->philo->id, routine_arg->args->timeout_eat); - pthread_mutex_unlock(&routine_arg->fork_left->mutex); - pthread_mutex_unlock(&routine_arg->fork_right->mutex); - philo_sleep(routine_arg->philo->id, routine_arg->args->timeout_sleep); - philo_think(routine_arg->philo->id); + pthread_mutex_lock(&arg->fork_left->mutex); + pthread_mutex_lock(&arg->fork_right->mutex); + philo_eat(arg->philo->id, arg->args->timeout_eat); + pthread_mutex_unlock(&arg->fork_left->mutex); + pthread_mutex_unlock(&arg->fork_right->mutex); + philo_sleep(arg->philo->id, arg->args->timeout_sleep); + philo_think(arg->philo->id); } } - if (routine_arg->philo->alive) - philo_die(routine_arg->philo->id); - free(routine_arg); + if (arg->philo->alive) + philo_die(arg->philo->id); + /* pthread_join( */ + free(arg); return (NULL); } -void *routine_death(t_routine_arg *arg) +void *routine_death(void *void_arg) { + t_routine_arg *arg; + t_time current; struct timeval tv; + arg = (t_routine_arg*)void_arg; if (gettimeofday(&tv, NULL) == -1) return (NULL); + current = h_timeval_to_time(&tv); while (arg->philo->alive && - arg->philo->time_last_eat - tv.asd > arg->args->timeout_death) + current - arg->philo->time_last_eat < arg->args->timeout_death) + { if (gettimeofday(&tv, NULL) == -1) return (NULL); + current = h_timeval_to_time(&tv); + } arg->philo->alive = FALSE; return (NULL); } |
