diff --git a/.gitignore b/.gitignore deleted file mode 100644 index e43b0f9..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -.DS_Store diff --git a/.vscode/configurationCache.log b/.vscode/configurationCache.log deleted file mode 100644 index bab9054..0000000 --- a/.vscode/configurationCache.log +++ /dev/null @@ -1 +0,0 @@ -{"buildTargets":[],"launchTargets":[],"customConfigurationProvider":{"workspaceBrowse":{"browsePath":[],"compilerArgs":[]},"fileIndex":[]}} \ No newline at end of file diff --git a/.vscode/dryrun.log b/.vscode/dryrun.log deleted file mode 100644 index 978a716..0000000 --- a/.vscode/dryrun.log +++ /dev/null @@ -1,7 +0,0 @@ -make --dry-run --keep-going --print-directory -make: Entering directory `/Users/mdenys/Desktop/projects/philosophers' - -make: Leaving directory `/Users/mdenys/Desktop/projects/philosophers' - -make: *** No targets specified and no makefile found. Stop. - diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 3c6a9a7..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "type": "lldb", - "request": "launch", - "name": "Debug", - "program": "${workspaceFolder}/philo_three/philo_three", - "args": ["4", "310", "200", "200"], - "cwd": "${workspaceFolder}" - } - ] -} diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 6fa0e1e..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "makefile.extensionOutputFolder": "./.vscode", - "C_Cpp.default.configurationProvider": "ms-vscode.makefile-tools" -} \ No newline at end of file diff --git a/.vscode/targets.log b/.vscode/targets.log deleted file mode 100644 index 970a113..0000000 --- a/.vscode/targets.log +++ /dev/null @@ -1,228 +0,0 @@ -make all --print-data-base --no-builtin-variables --no-builtin-rules --question -# GNU Make 3.81 -# Copyright (C) 2006 Free Software Foundation, Inc. -# This is free software; see the source for copying conditions. -# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. - -# This program built for i386-apple-darwin11.3.0 - -make: *** No rule to make target `all'. Stop. - - -# Make data base, printed on Sun May 16 15:43:05 2021 - -# Variables - -# automatic - +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2021/05/20 12:13:10 by mdenys #+# #+# # -# Updated: 2021/05/20 20:45:52 by mdenys ### ########.fr # +# Updated: 2021/05/20 20:51:59 by mdenys ### ########.fr # # # # **************************************************************************** # @@ -14,7 +14,7 @@ SRCS = main.c utilc.c parser.c logic.c messages.c\ prepare_simulation.c old_utilc.c time.c ft_itoa.c\ - ft_strjoin.c utilc2.c observer.c ft_exit.c + ft_strjoin.c utilc2.c observer.c ft_exit.c pkill_all.c obs_for_phil.c SRC_DIR = ./srcs/ diff --git a/philo_three/includes/philo_header.h b/philo_three/includes/philo_header.h index a1fad0c..cb79e27 100644 --- a/philo_three/includes/philo_header.h +++ b/philo_three/includes/philo_header.h @@ -6,7 +6,7 @@ /* By: mdenys +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/05/20 12:12:33 by mdenys #+# #+# */ -/* Updated: 2021/05/20 14:01:48 by mdenys ### ########.fr */ +/* Updated: 2021/05/20 23:44:18 by mdenys ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,7 @@ # include # include # include +# include # include # define _SEM_NAME_FORKS "pSemForks" # define _SEM_WRITE_LOCK "write" @@ -36,9 +37,11 @@ typedef struct s_all time_t start_time; t_philo_data *data_philo; sem_t *write; + sem_t *bullet; + sem_t *bullets; int smert; int opt; - sem_t *bucket; + sem_t *bucket; } t_all; typedef struct s_philo @@ -55,6 +58,7 @@ typedef struct s_philo t_all *all_struct; int is_eat; sem_t *eat_sem; + pid_t pid; char *name; } t_philo; @@ -77,7 +81,7 @@ void *run_trhead(t_philo *philo); int prepare_resources(t_all *all); int check_all_run(t_all *all); void run_simulation(t_all *all); -void run_all_phils(t_all *all); +void *run_all_phils(t_all *all); void messages(t_all *all, t_philo *philo, char *str); void cycle(t_philo *philo); void ft_msleep(time_t interval_ms); @@ -92,4 +96,7 @@ size_t ft_strlcat(char *dst, const char *src, size_t size); int ft_strlen(char *str); void get_data_for_philo(t_all *all); int run_observer(t_all *all); +void pkill_all(t_all *all); +void *obs_phil(t_philo *philo); +int run_observer_return(t_all *all, int i); #endif diff --git a/philo_three/srcs/.DS_Store b/philo_three/srcs/.DS_Store new file mode 100644 index 0000000..a5a1cf6 Binary files /dev/null and b/philo_three/srcs/.DS_Store differ diff --git a/philo_three/srcs/logic.c b/philo_three/srcs/logic.c index ac8507b..2078e0f 100644 --- a/philo_three/srcs/logic.c +++ b/philo_three/srcs/logic.c @@ -6,7 +6,7 @@ /* By: mdenys +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/05/20 12:12:43 by mdenys #+# #+# */ -/* Updated: 2021/05/20 16:18:47 by mdenys ### ########.fr */ +/* Updated: 2021/05/20 22:29:36 by mdenys ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,13 +16,6 @@ void *run_trhead(t_philo *philo) { while (1) { - if (philo->all_struct->opt) - { - if (philo->count_eat == philo->all_struct->etarate_meal) - { - return (NULL); - } - } cycle(philo); } return (NULL); diff --git a/philo_three/srcs/obs_for_phil.c b/philo_three/srcs/obs_for_phil.c new file mode 100644 index 0000000..67077ff --- /dev/null +++ b/philo_three/srcs/obs_for_phil.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* obs_for_phil.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mdenys +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/20 12:12:48 by mdenys #+# #+# */ +/* Updated: 2021/05/20 23:46:13 by mdenys ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/philo_header.h" + +void *obs_phil(t_philo *philo) +{ + time_t time_eat; + + while (1) + { + sem_wait(philo->eat_sem); + time_eat = (ft_get_time() - \ + philo->all_struct->start_time) - philo->last_eat; + if (time_eat > philo->all_struct->time_to_die) + { + run_observer_return(philo->all_struct, philo->nbr_philo); + break ; + } + else if (philo->all_struct->opt) + { + if (philo->count_eat == philo->all_struct->etarate_meal) + { + sem_post(philo->all_struct->bullets); + while (1) + ; + } + } + sem_post(philo->eat_sem); + } + sem_post(philo->all_struct->bullet); + return (NULL); +} diff --git a/philo_three/srcs/observer.c b/philo_three/srcs/observer.c index fe642a7..2bd098c 100644 --- a/philo_three/srcs/observer.c +++ b/philo_three/srcs/observer.c @@ -6,7 +6,7 @@ /* By: mdenys +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/05/20 12:12:50 by mdenys #+# #+# */ -/* Updated: 2021/05/20 16:10:26 by mdenys ### ########.fr */ +/* Updated: 2021/05/20 21:12:17 by mdenys ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,8 +17,7 @@ int run_observer_return(t_all *all, int i) sem_wait(all->write); printf("%ld %d died\n", \ ft_get_time() - all->start_time, i + 1); - all->smert = 42; - return (2); + return (1); } int run_observer(t_all *all) @@ -51,7 +50,5 @@ int runner(t_all *all) prepare_resources(all); all->start_time = ft_get_time(); run_all_phils(all); - if (!run_observer(all)) - return (0); return (1); } diff --git a/philo_three/srcs/pkill_all.c b/philo_three/srcs/pkill_all.c new file mode 100644 index 0000000..146b7e4 --- /dev/null +++ b/philo_three/srcs/pkill_all.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pkill_all.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mdenys +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/20 12:12:48 by mdenys #+# #+# */ +/* Updated: 2021/05/20 23:04:26 by mdenys ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/philo_header.h" + +void pkill_all(t_all *all) +{ + int i; + + i = 0; + while (i < all->nbr_phils) + kill(all->data_philo->philors[i++].pid, SIGKILL); +} diff --git a/philo_three/srcs/prepare_simulation.c b/philo_three/srcs/prepare_simulation.c index 894bc9b..6bce011 100644 --- a/philo_three/srcs/prepare_simulation.c +++ b/philo_three/srcs/prepare_simulation.c @@ -6,7 +6,7 @@ /* By: mdenys +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/05/20 12:12:58 by mdenys #+# #+# */ -/* Updated: 2021/05/20 14:01:31 by mdenys ### ########.fr */ +/* Updated: 2021/05/21 00:44:52 by mdenys ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,7 @@ void get_data_f_philo(t_all *all) all->data_philo->philors[i].name = sem_set_name("philo", i); all->data_philo->philors[i].eat_sem = \ reopen_sem(all->data_philo->philors[i].name, 1); + sem_wait(all->bullets); i++; } } @@ -41,13 +42,39 @@ int prepare_resources(t_all *all) all->data_philo->sems = reopen_sem(_SEM_NAME_FORKS, all->nbr_phils); all->write = reopen_sem(_SEM_WRITE_LOCK, 1); all->bucket = reopen_sem("bucket", 1); + all->bullet = reopen_sem("bullet", 1); + all->bullets = reopen_sem("bullets", all->nbr_phils); + sem_wait(all->bullet); all->data_philo->philors = (t_philo *)ft_calloc(all->nbr_phils \ + 1, sizeof(t_philo)); get_data_f_philo(all); return (0); } -void run_all_phils(t_all *all) +int checker(t_all *all) +{ + int i; + + i = 0; + if (all->opt) + { + while (i < all->nbr_phils) + { + sem_wait(all->bullets); + i++; + } + pkill_all(all); + return (3); + } + else + { + sem_wait(all->bullet); + pkill_all(all); + } + return (0); +} + +void *run_all_phils(t_all *all) { void *philo; int i; @@ -55,12 +82,21 @@ void run_all_phils(t_all *all) i = 0; while (i < all->nbr_phils) { - philo = (void *)(&all->data_philo->philors[i]); - if (pthread_create(&all->data_philo->philors[i].thread, NULL, \ - (void *)run_trhead, philo) == -1) - printf("I can't create a thread t%d\n", i); + all->data_philo->philors[i].pid = fork(); + if (all->data_philo->philors[i].pid == 0) + { + philo = (void *)(&all->data_philo->philors[i]); + if (pthread_create(&all->data_philo->philors[i].thread, NULL, \ + (void *)obs_phil, philo) == -1) + printf("I can't create a thread t%d\n", i); + else + pthread_detach(all->data_philo->philors[i].thread); + run_trhead(philo); + } else - pthread_detach(all->data_philo->philors[i].thread); - i++; + i++; } + if (checker(all)) + return (0); + return (0); } diff --git a/philo_two/includes/philo_header.h b/philo_two/includes/philo_header.h index a1fad0c..157260e 100644 --- a/philo_two/includes/philo_header.h +++ b/philo_two/includes/philo_header.h @@ -6,7 +6,7 @@ /* By: mdenys +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/05/20 12:12:33 by mdenys #+# #+# */ -/* Updated: 2021/05/20 14:01:48 by mdenys ### ########.fr */ +/* Updated: 2021/05/21 00:23:40 by mdenys ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,9 +36,9 @@ typedef struct s_all time_t start_time; t_philo_data *data_philo; sem_t *write; - int smert; + int final; int opt; - sem_t *bucket; + sem_t *bucket; } t_all; typedef struct s_philo @@ -56,6 +56,8 @@ typedef struct s_philo int is_eat; sem_t *eat_sem; char *name; + char *end_meal_name; + sem_t *end_meal; } t_philo; typedef struct s_philo_data diff --git a/philo_two/srcs/.DS_Store b/philo_two/srcs/.DS_Store new file mode 100644 index 0000000..a5a1cf6 Binary files /dev/null and b/philo_two/srcs/.DS_Store differ diff --git a/philo_two/srcs/logic.c b/philo_two/srcs/logic.c index ac8507b..2f149c6 100644 --- a/philo_two/srcs/logic.c +++ b/philo_two/srcs/logic.c @@ -6,7 +6,7 @@ /* By: mdenys +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/05/20 12:12:43 by mdenys #+# #+# */ -/* Updated: 2021/05/20 16:18:47 by mdenys ### ########.fr */ +/* Updated: 2021/05/21 00:01:15 by mdenys ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,9 @@ void *run_trhead(t_philo *philo) { if (philo->count_eat == philo->all_struct->etarate_meal) { - return (NULL); + philo->died = 42; + while (1) + ; } } cycle(philo); diff --git a/philo_two/srcs/observer.c b/philo_two/srcs/observer.c index fe642a7..602195e 100644 --- a/philo_two/srcs/observer.c +++ b/philo_two/srcs/observer.c @@ -6,7 +6,7 @@ /* By: mdenys +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/05/20 12:12:50 by mdenys #+# #+# */ -/* Updated: 2021/05/20 16:10:26 by mdenys ### ########.fr */ +/* Updated: 2021/05/21 00:36:27 by mdenys ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,32 +17,43 @@ int run_observer_return(t_all *all, int i) sem_wait(all->write); printf("%ld %d died\n", \ ft_get_time() - all->start_time, i + 1); - all->smert = 42; + all->final = 42; return (2); } +int check_all_philors(t_all *all) +{ + int i; + int j; + time_t time_eat; + + j = 0; + i = 0; + while (i < all->nbr_phils) + { + sem_wait(all->data_philo->philors[i].eat_sem); + time_eat = (ft_get_time() - \ + all->start_time) - all->data_philo->philors[i].last_eat; + if (all->data_philo->philors[i].died == 42) + { + j++; + if (j == all->nbr_phils) + return (ft_exit(all, 2)); + } + if (time_eat > all->time_to_die) + return (run_observer_return(all, i)); + sem_post(all->data_philo->philors[i].eat_sem); + if (all->final == 42) + return (ft_exit(all, 2)); + i++; + } + return (0); +} + int run_observer(t_all *all) { - int i; - time_t time_eat; - - i = 0; - while (1) - { - i = 0; - while (i < all->nbr_phils) - { - sem_wait(all->data_philo->philors[i].eat_sem); - time_eat = (ft_get_time() - \ - all->start_time) - all->data_philo->philors[i].last_eat; - if (time_eat > all->time_to_die) - return (run_observer_return(all, i)); - sem_post(all->data_philo->philors[i].eat_sem); - if (all->smert == 42) - return (ft_exit(all, 2)); - i++; - } - } + while (check_all_philors(all) == 0) + ; return (0); } diff --git a/philo_two/srcs/parser.c b/philo_two/srcs/parser.c index c78b599..55e5907 100644 --- a/philo_two/srcs/parser.c +++ b/philo_two/srcs/parser.c @@ -6,7 +6,7 @@ /* By: mdenys +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/05/20 12:12:56 by mdenys #+# #+# */ -/* Updated: 2021/05/20 14:11:43 by mdenys ### ########.fr */ +/* Updated: 2021/05/21 00:23:40 by mdenys ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,7 +43,7 @@ int check_param(t_all *all, int optional) int parse_param(t_all *all, int optional, char **argv) { - all->smert = 0; + all->final = 0; if (optional) { all->nbr_phils = ft_atoi(argv[1]); diff --git a/philo_two/srcs/prepare_simulation.c b/philo_two/srcs/prepare_simulation.c index 894bc9b..893226b 100644 --- a/philo_two/srcs/prepare_simulation.c +++ b/philo_two/srcs/prepare_simulation.c @@ -6,7 +6,7 @@ /* By: mdenys +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/05/20 12:12:58 by mdenys #+# #+# */ -/* Updated: 2021/05/20 14:01:31 by mdenys ### ########.fr */ +/* Updated: 2021/05/20 23:24:32 by mdenys ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,8 +26,12 @@ void get_data_f_philo(t_all *all) all->data_philo->philors[i].all_struct = all; all->data_philo->philors[i].is_eat = 0; all->data_philo->philors[i].name = sem_set_name("philo", i); + all->data_philo->philors[i].end_meal_name = sem_set_name("end_meal", i); all->data_philo->philors[i].eat_sem = \ reopen_sem(all->data_philo->philors[i].name, 1); + all->data_philo->philors[i].end_meal = \ + reopen_sem(all->data_philo->philors[i].end_meal_name, 1); + sem_wait(all->data_philo->philors[i].end_meal); i++; } }