diff options
Diffstat (limited to 'philo_one')
| -rw-r--r-- | philo_one/Makefile | 16 | ||||
| -rw-r--r-- | philo_one/src/args.c (renamed from philo_one/args.c) | 4 | ||||
| -rw-r--r-- | philo_one/src/forks.c (renamed from philo_one/forks.c) | 0 | ||||
| -rw-r--r-- | philo_one/src/helper.c (renamed from philo_one/helper.c) | 2 | ||||
| -rw-r--r-- | philo_one/src/io.c (renamed from philo_one/io.c) | 10 | ||||
| -rw-r--r-- | philo_one/src/main.c (renamed from philo_one/main.c) | 8 | ||||
| -rw-r--r-- | philo_one/src/philo.c (renamed from philo_one/philo.c) | 4 | ||||
| -rw-r--r-- | philo_one/src/philo_one.h (renamed from philo_one/philo_one.h) | 2 | ||||
| -rw-r--r-- | philo_one/src/routine.c (renamed from philo_one/routine.c) | 5 |
9 files changed, 27 insertions, 24 deletions
diff --git a/philo_one/Makefile b/philo_one/Makefile index de41214..da3f82e 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/09/28 14:12:28 by cacharle ### ########.fr # +# Updated: 2020/09/29 11:30:07 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -18,17 +18,11 @@ LDFLAGS = -lpthread NAME = philo_one -SRC = main.c \ - philo.c \ - forks.c \ - routine.c \ - io.c \ - args.c \ - helper.c - +SRCDIR = src OBJDIR = obj -OBJ = $(SRC:%.c=$(OBJDIR)/%.o) +SRC = $(shell find $(SRCDIR) -type f -name '*.c') +OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o) all: prebuild $(NAME) @@ -38,7 +32,7 @@ prebuild: $(NAME): $(OBJ) $(CC) -o $@ $(OBJ) $(LDFLAGS) -$(OBJDIR)/%.o: %.c +$(OBJDIR)/%.o: $(SRCDIR)/%.c $(CC) $(CCFLAGS) -c -o $@ $< clean: diff --git a/philo_one/args.c b/philo_one/src/args.c index 9eb6da5..b7277dd 100644 --- a/philo_one/args.c +++ b/philo_one/src/args.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 23:12:55 by cacharle #+# #+# */ -/* Updated: 2020/09/28 14:24:53 by cacharle ### ########.fr */ +/* Updated: 2020/09/29 11:08:06 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ bool parse_args(t_philo_conf *philo_args, int argc, char **argv) { if (argc != 5 && argc != 6) - return h_err(false, "Usage: %s philosophers_num death_timeout eat_timeout sleep_timeout", argv[0]); + return h_err(false, "Usage: %s philosophers_num death_timeout eat_timeout sleep_timeout [meal_num]", argv[0]); if ((philo_args->philo_num = h_atou_strict(argv[1])) == -1 || (philo_args->timeout_death = h_atou_strict(argv[2])) == -1 || (philo_args->timeout_eat = h_atou_strict(argv[3])) == -1 diff --git a/philo_one/forks.c b/philo_one/src/forks.c index ebe5261..ebe5261 100644 --- a/philo_one/forks.c +++ b/philo_one/src/forks.c diff --git a/philo_one/helper.c b/philo_one/src/helper.c index 1caec00..fa4c7ff 100644 --- a/philo_one/helper.c +++ b/philo_one/src/helper.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/08 23:22:49 by cacharle #+# #+# */ -/* Updated: 2020/09/28 14:54:17 by cacharle ### ########.fr */ +/* Updated: 2020/09/29 14:15:50 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/philo_one/io.c b/philo_one/src/io.c index c91e9ab..96a8f07 100644 --- a/philo_one/io.c +++ b/philo_one/src/io.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 21:37:50 by cacharle #+# #+# */ -/* Updated: 2020/09/27 10:33:29 by charles ### ########.fr */ +/* Updated: 2020/09/29 14:55:21 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,11 +36,13 @@ void io_eat(t_routine_arg *arg) eat_counter = 0; while (eat_counter < arg->conf->meal_num) { + pthread_mutex_lock(&arg->conf->mutex_all_alive); if (!arg->conf->all_alive) return ; pthread_mutex_lock(&arg->conf->mutex_stdout); philo_put(arg->philo->id, EVENT_EAT); pthread_mutex_unlock(&arg->conf->mutex_stdout); + pthread_mutex_unlock(&arg->conf->mutex_all_alive); usleep(arg->conf->timeout_eat * 1000); eat_counter++; } @@ -48,21 +50,25 @@ void io_eat(t_routine_arg *arg) void io_think(t_routine_arg *arg) { + pthread_mutex_lock(&arg->conf->mutex_all_alive); if (!arg->conf->all_alive) return ; pthread_mutex_lock(&arg->conf->mutex_stdout); philo_put(arg->philo->id, EVENT_THINK); pthread_mutex_unlock(&arg->conf->mutex_stdout); + pthread_mutex_unlock(&arg->conf->mutex_all_alive); } void io_sleep(t_routine_arg *arg) { + pthread_mutex_lock(&arg->conf->mutex_all_alive); if (!arg->conf->all_alive) return ; pthread_mutex_lock(&arg->conf->mutex_stdout); philo_put(arg->philo->id, EVENT_SLEEP); pthread_mutex_unlock(&arg->conf->mutex_stdout); - usleep(arg->conf->timeout_sleep * 1000); + pthread_mutex_unlock(&arg->conf->mutex_all_alive); + /* usleep(arg->conf->timeout_sleep * 1000); */ } void io_die(t_routine_arg *arg) diff --git a/philo_one/main.c b/philo_one/src/main.c index 83b5564..511fdbe 100644 --- a/philo_one/main.c +++ b/philo_one/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 05:53:02 by cacharle #+# #+# */ -/* Updated: 2020/09/28 14:59:59 by cacharle ### ########.fr */ +/* Updated: 2020/09/29 13:26:07 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,8 +21,8 @@ int main(int argc, char **argv) if (!parse_args(&philo_args, argc, argv)) return (1); - if (philo_args.philo_num < 2) - return (h_err(1, "Error: there should be at least 2 philosophers")); + if (philo_args.philo_num == 0) + return (0); if ((forks = forks_new(philo_args.philo_num)) == NULL) return (1); if ((philos = philos_new(philo_args.philo_num)) == NULL) @@ -34,6 +34,8 @@ int main(int argc, char **argv) pthread_mutex_init(&philo_args.mutex_stdout, NULL); if (!philos_start(philos, routine_args, philo_args.philo_num)) return (1); + /* pthread_mutex_lock(philo_args.mutex_all_alive); */ + /* pthread_mutex_lock(philo_args.mutex_all_alive); */ while (philo_args.all_alive) ; philos_join(philos, philo_args.philo_num); diff --git a/philo_one/philo.c b/philo_one/src/philo.c index c32dc76..e0ecfec 100644 --- a/philo_one/philo.c +++ b/philo_one/src/philo.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/09 23:47:14 by cacharle #+# #+# */ -/* Updated: 2020/09/27 10:42:25 by charles ### ########.fr */ +/* Updated: 2020/09/29 13:29:49 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,7 +55,7 @@ void philos_join(t_philo *philos, int num) if (philos[i].alive) { philos[i].alive = false; - pthread_join(philos[i].thread, NULL); + pthread_detach(philos[i].thread); } } } diff --git a/philo_one/philo_one.h b/philo_one/src/philo_one.h index 0d6844c..e92fdcd 100644 --- a/philo_one/philo_one.h +++ b/philo_one/src/philo_one.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/24 06:11:16 by cacharle #+# #+# */ -/* Updated: 2020/09/28 14:22:57 by cacharle ### ########.fr */ +/* Updated: 2020/09/29 14:15:47 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/philo_one/routine.c b/philo_one/src/routine.c index d52fe2e..0115e91 100644 --- a/philo_one/routine.c +++ b/philo_one/src/routine.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 01:11:27 by cacharle #+# #+# */ -/* Updated: 2020/09/27 10:25:56 by charles ### ########.fr */ +/* Updated: 2020/09/29 14:30:06 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,9 +28,10 @@ void *routine_philo(t_routine_arg *arg) pthread_mutex_lock(arg->fork_right); arg->philo->time_last_eat = h_time_now(); io_eat(arg); + io_sleep(arg); pthread_mutex_unlock(arg->fork_right); pthread_mutex_unlock(arg->fork_left); - io_sleep(arg); + usleep(arg->conf->timeout_sleep * 1000); io_think(arg); } pthread_join(thread_death, NULL); |
