From 8dff2b4a809a0ef6beb01a1958fe5500b222e313 Mon Sep 17 00:00:00 2001 From: Matthos Denys Date: Sun, 16 May 2021 15:42:09 +0300 Subject: [PATCH] create copys philo_one and mv philo_two --- .vscode/dryrun.log | 3 +- .vscode/targets.log | 25 ++++---- philo_two/Makefile | 45 +++++++++++++ philo_two/includes/philo_header.h | 82 ++++++++++++++++++++++++ philo_two/srcs/logic.c | 99 +++++++++++++++++++++++++++++ philo_two/srcs/main.c | 12 ++++ philo_two/srcs/messages.c | 12 ++++ philo_two/srcs/old_utilc.c | 57 +++++++++++++++++ philo_two/srcs/parser.c | 87 +++++++++++++++++++++++++ philo_two/srcs/prepare_simulation.c | 98 ++++++++++++++++++++++++++++ philo_two/srcs/time.c | 37 +++++++++++ philo_two/srcs/utilc.c | 53 +++++++++++++++ 12 files changed, 595 insertions(+), 15 deletions(-) create mode 100644 philo_two/Makefile create mode 100644 philo_two/includes/philo_header.h create mode 100644 philo_two/srcs/logic.c create mode 100644 philo_two/srcs/main.c create mode 100644 philo_two/srcs/messages.c create mode 100644 philo_two/srcs/old_utilc.c create mode 100644 philo_two/srcs/parser.c create mode 100644 philo_two/srcs/prepare_simulation.c create mode 100644 philo_two/srcs/time.c create mode 100644 philo_two/srcs/utilc.c diff --git a/.vscode/dryrun.log b/.vscode/dryrun.log index a76ff7a..2cd77d7 100644 --- a/.vscode/dryrun.log +++ b/.vscode/dryrun.log @@ -1,6 +1,5 @@ -make --dry-run --always-make --keep-going --print-directory +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/targets.log b/.vscode/targets.log index e99b150..053922b 100644 --- a/.vscode/targets.log +++ b/.vscode/targets.log @@ -7,8 +7,10 @@ make all --print-data-base --no-builtin-variables --no-builtin-rules --question # This program built for i386-apple-darwin11.3.0 +make: *** No rule to make target `all'. Stop. + -# Make data base, printed on Thu May 13 12:04:43 2021 +# Make data base, printed on Sun May 16 14:47:41 2021 # Variables @@ -16,9 +18,6 @@ make all --print-data-base --no-builtin-variables --no-builtin-rules --question +# include +# include +# include +# include +# include + + + +#define ANSI_COLOR_RED "\x1b[31m" +#define ANSI_COLOR_GREEN "\x1b[32m" +#define ANSI_COLOR_YELLOW "\x1b[33m" +#define ANSI_COLOR_BLUE "\x1b[34m" +#define ANSI_COLOR_MAGENTA "\x1b[35m" +#define ANSI_COLOR_CYAN "\x1b[36m" +#define ANSI_COLOR_RESET "\x1b[0m" + +typedef struct s_philo_data t_philo_data; + +typedef struct s_all +{ + int number_of_philosophers; + time_t time_to_die; + time_t time_to_eat; + time_t time_to_sleep; + int number_of_times_each_philosopher_must_eat; + time_t start_time; + t_philo_data *data_philo; + pthread_mutex_t write; + int smert; + int opt; +} t_all; + + +typedef struct s_philo +{ + unsigned worked:1; + unsigned can_run_simulation:1; + int more_fork; + int less_fork; + pthread_t thread; + int count_eat; + time_t time_to_die; + int died; + int nbr_philo; + time_t last_eat; + time_t limit; + t_all *all_struct; + int is_eat; + pthread_mutex_t eat_mutex; +} t_philo; + +typedef struct s_philo_data +{ + pthread_mutex_t *mutex; + t_philo *philors; +} t_philo_data; + +void *ft_calloc(size_t num, size_t size); +t_all *structalloc_and_bzero(void); +int parse_and_check_values(char **argv, int quantity); +int ft_atoi(const char *str); +int check_f(char ch); +int runner(t_all *all); +int return_big(int a, int b); +int return_less(int a, int b); +time_t ft_get_time(void); +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 messages(t_all *all, t_philo *philo, char *str); +void cycle(t_philo *philo); +void ft_msleep(time_t interval_ms); +void my_usleep(long time); +void ft_exit(t_all *all); +#endif diff --git a/philo_two/srcs/logic.c b/philo_two/srcs/logic.c new file mode 100644 index 0000000..aa62b1f --- /dev/null +++ b/philo_two/srcs/logic.c @@ -0,0 +1,99 @@ +#include "../includes/philo_header.h" + +void *run_trhead(t_philo *philo) +{ + philo->worked = 1; + while(1) + { + if (philo->can_run_simulation) + break; + } + if (philo->can_run_simulation) + { + while(philo->can_run_simulation && philo->all_struct->smert != 42) + { + if (philo->all_struct->opt) + { + if (philo->count_eat == philo->all_struct->number_of_times_each_philosopher_must_eat) + philo->all_struct->smert = 42; + } + cycle(philo); + } + } + return(NULL); +} + +void take_forks(t_philo *philo) +{ + t_all *all; + all = philo->all_struct; + pthread_mutex_lock(&all->data_philo->mutex[philo->less_fork]); + messages(philo->all_struct, philo, "has taken a fork"); + pthread_mutex_lock(&all->data_philo->mutex[philo->more_fork]); + messages(philo->all_struct, philo, "has taken a fork"); +} +void eating(t_philo *philo) +{ + messages(philo->all_struct, philo, "eating"); + philo->last_eat = ft_get_time() - philo->all_struct->start_time; + ft_msleep(philo->all_struct->time_to_eat); + philo->count_eat++; +} +void sleeping(t_philo *philo) +{ + t_all *all; + all = philo->all_struct; + messages(philo->all_struct, philo, "sleeping"); + pthread_mutex_unlock(&all->data_philo->mutex[philo->more_fork]); + pthread_mutex_unlock(&all->data_philo->mutex[philo->less_fork]); + ft_msleep(philo->all_struct->time_to_sleep); +} + +void cycle(t_philo *philo) +{ + messages(philo->all_struct, philo, "thinking"); + take_forks(philo); + eating(philo); + sleeping(philo); +} + +int run_observer(t_all *all) +{ + int i; + i = 0; + time_t time_eat; + while(1) + { + i = 0; + while(i <= all->number_of_philosophers) + { + if (all->smert != 42) + { + time_eat = (ft_get_time() - all->start_time) - all->data_philo->philors[i].last_eat; + if (time_eat > all->time_to_die) + { + printf("%ld %d died\n", ft_get_time() - all->start_time, i + 1); + return(2); + } + } + else + return(2); + i++; + } + } + return (0); +} + +int runner(t_all *all) +{ + prepare_resources(all); + run_all_phils(all); + if (check_all_run(all)) + { + all->start_time = ft_get_time(); + run_simulation(all); + } + if (!run_observer(all)) + return(0); + return(1); +} diff --git a/philo_two/srcs/main.c b/philo_two/srcs/main.c new file mode 100644 index 0000000..01b6bd3 --- /dev/null +++ b/philo_two/srcs/main.c @@ -0,0 +1,12 @@ +#include "../includes/philo_header.h" + +int main(int argc, char **argv) +{ + if (parse_and_check_values(argv, argc) == 2) + return(0); + else + { + return (1); + } + return(0); +} diff --git a/philo_two/srcs/messages.c b/philo_two/srcs/messages.c new file mode 100644 index 0000000..ebffc98 --- /dev/null +++ b/philo_two/srcs/messages.c @@ -0,0 +1,12 @@ +#include "../includes/philo_header.h" + +void messages(t_all *all, t_philo *philo, char *str) +{ + time_t timestemp = ft_get_time() - all->start_time; + if (philo->all_struct->smert != 42) + { + pthread_mutex_lock(&all->write); + printf("%ld %*s%d %s\n", timestemp, philo->nbr_philo * 30, "", philo->nbr_philo + 1, str); + pthread_mutex_unlock(&all->write); + } +} diff --git a/philo_two/srcs/old_utilc.c b/philo_two/srcs/old_utilc.c new file mode 100644 index 0000000..3d505cb --- /dev/null +++ b/philo_two/srcs/old_utilc.c @@ -0,0 +1,57 @@ +#include "../includes/philo_header.h" + +int check_f(char ch) +{ + if (ch == '\t' || ch == '\n' || ch == ' ' || \ + ch == '\v' || ch == '\f' || ch == '\r') + return (1); + else + return (0); +} + +int ft_atoi(const char *str) +{ + int i; + unsigned long int result; + int operator; + + i = 0; + operator = 1; + result = 0; + while (check_f(str[i])) + i++; + if (str[i] == '-' || str[i] == '+') + { + if (str[i] == '-') + operator *= -1; + i++; + } + while (str[i] >= '0' && str[i] <= '9') + { + if (result > 922337203685477580 && operator == -1) + return (0); + else if (result > 922337203685477580 && operator == 1) + return (-1); + result = result * 10 + str[i] - '0'; + i++; + } + return (result * operator); +} + +void *ft_calloc(size_t num, size_t size) +{ + void *addr; + unsigned int nbytes; + + nbytes = num * size; + addr = malloc(nbytes); + if (!size || !num || (size == 0 && num == 0)) + return ((void *)addr); + if (!addr) + return (NULL); + else + { + memset(addr, '\0', nbytes); + } + return ((void *)addr); +} diff --git a/philo_two/srcs/parser.c b/philo_two/srcs/parser.c new file mode 100644 index 0000000..fd9fa19 --- /dev/null +++ b/philo_two/srcs/parser.c @@ -0,0 +1,87 @@ +#include "../includes/philo_header.h" + +int check_param(t_all *all, int optional) +{ + if (optional) + { + if (all->number_of_philosophers <= 0) + return(1); + if (all->time_to_die <= 0) + return(1); + if (all->time_to_eat <= 0) + return(1); + if (all->time_to_sleep <= 0) + return(1); + if (all->number_of_times_each_philosopher_must_eat <= 0) + return(1); + } + else + { + if (all->number_of_philosophers <= 0) + return(1); + if (all->time_to_die <= 0) + return(1); + if (all->time_to_eat <= 0) + return(1); + if (all->time_to_sleep <= 0) + return(1); + } + return (0); +} + +int parse_param(t_all *all, int optional, char **argv) +{ + all->smert = 0; + if (optional) + { + all->number_of_philosophers = ft_atoi(argv[1]) - 1; + all->time_to_die = ft_atoi(argv[2]); + all->time_to_eat = ft_atoi(argv[3]); + all->time_to_sleep = ft_atoi(argv[4]); + all->number_of_times_each_philosopher_must_eat = ft_atoi(argv[5]); + all->opt = 1; + return (check_param(all, 1)); + } + else + { + all->number_of_philosophers = ft_atoi(argv[1]) - 1; + all->time_to_die = ft_atoi(argv[2]); + all->time_to_eat = ft_atoi(argv[3]); + all->time_to_sleep = ft_atoi(argv[4]); + all->number_of_times_each_philosopher_must_eat = 0; + all->opt = 0; + return (check_param(all, 0)); + } +} + +int parse_and_check_values(char **argv, int quantity) +{ + t_all *all; + int bool; + + if (quantity == 5) + bool = 0; + if (quantity == 6) + bool = 1; + all = structalloc_and_bzero(); + if (quantity == 5 || quantity == 6) + { + if (!parse_param(all, bool, argv)) + { + if (runner(all) == 2) + return (2); + return(1); + } + else + { + printf("bad arg\n"); + return (0); + } + } + else + { + printf("error please use argument 5 (optional) or 4 (standart)\n"); + free(all); + return(0); + } +} diff --git a/philo_two/srcs/prepare_simulation.c b/philo_two/srcs/prepare_simulation.c new file mode 100644 index 0000000..6fab9e1 --- /dev/null +++ b/philo_two/srcs/prepare_simulation.c @@ -0,0 +1,98 @@ +#include "../includes/philo_header.h" + +int check_all_run(t_all *all) +{ + int i; + + i = 0; + while (i <= all->number_of_philosophers) + { + if (all->data_philo->philors[i].worked) + { + i++; + } + } + printf("все запустились !\n"); + return (1); +} + + +void run_simulation(t_all *all) +{ + int i; + + i = 0; + while (i <= all->number_of_philosophers) + { + if (!all->data_philo->philors[i].can_run_simulation) + { + all->data_philo->philors[i].can_run_simulation = 1; + i++; + } + } +} + +void get_fork_for_philo(t_all *all) +{ + int i; + + i = 0; + + while(i <= all->number_of_philosophers) + { + if (i == 0) + { + all->data_philo->philors[i].more_fork = all->number_of_philosophers; + all->data_philo->philors[i].less_fork = 0; + } + else + { + all->data_philo->philors[i].more_fork = return_big(i, i + 1); + all->data_philo->philors[i].less_fork = return_less(i, i + 1); + } + all->data_philo->philors[i].died = 0; + all->data_philo->philors[i].nbr_philo = i; + all->data_philo->philors[i].worked = 0; + all->data_philo->philors[i].can_run_simulation = 0; + all->data_philo->philors[i].all_struct = all; + all->data_philo->philors[i].is_eat = 0; + i++; + } +} + +int prepare_resources(t_all *all) +{ + int i; + + i = 0; + all->data_philo = ft_calloc(sizeof(t_philo_data), 1); + all->data_philo->mutex = (pthread_mutex_t *)ft_calloc(sizeof(pthread_mutex_t), all->number_of_philosophers); + while (i <= all->number_of_philosophers) + { + pthread_mutex_init(&all->data_philo->mutex[i], NULL); + i++; + } + all->data_philo->philors = (t_philo *)ft_calloc(sizeof(t_philo), all->number_of_philosophers); + get_fork_for_philo(all); + return (0); +} + +void run_all_phils(t_all *all) +{ + int i; + i = 0; + void *philo; + while(i <= all->number_of_philosophers) + { + 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); + } + else + { + pthread_detach(all->data_philo->philors[i].thread); + } + i++; + } +} diff --git a/philo_two/srcs/time.c b/philo_two/srcs/time.c new file mode 100644 index 0000000..9bb4559 --- /dev/null +++ b/philo_two/srcs/time.c @@ -0,0 +1,37 @@ +#include "../includes/philo_header.h" + +time_t ft_get_time(void) +{ + time_t return_value; + struct timeval current_time; + gettimeofday(¤t_time, NULL); + return_value = (current_time.tv_sec * 1000) + (current_time.tv_usec / 1000); + return (return_value); +} + +void ft_msleep(time_t interval_ms) +{ + time_t end_ms = ft_get_time() + interval_ms; + time_t now_ms = ft_get_time(); + + while (now_ms < end_ms) + { + usleep(_SLEEP_STEP); + now_ms = ft_get_time(); + } +} + +long get_time(void) +{ + struct timeval time; + gettimeofday(&time, NULL); + return ((long)(time.tv_sec * 1000 + time.tv_usec / 1000)); +} + +void my_usleep(long time) +{ + long t; + t = get_time(); + while (get_time() - t < time) + usleep(1); +} diff --git a/philo_two/srcs/utilc.c b/philo_two/srcs/utilc.c new file mode 100644 index 0000000..ab94a2a --- /dev/null +++ b/philo_two/srcs/utilc.c @@ -0,0 +1,53 @@ +#include "../includes/philo_header.h" + +t_all *structalloc_and_bzero(void) +{ + t_all *all; + + all = ft_calloc(sizeof(t_all), 1); + all->number_of_philosophers = -1; + all->time_to_die = -1; + all->time_to_eat = -1; + all->time_to_sleep = -1; + all->number_of_times_each_philosopher_must_eat = -1; + return(all); +} + +int return_big(int a, int b) +{ + if (a > b) + { + return (a); + } + return (b); +} + +int return_less(int a, int b) +{ + if (a < b) + { + return (a); + } + return (b); +} + +void ft_exit(t_all *all) +{ + int i; + + i = 0; + all->smert = 42; + ft_msleep(200); + while (i <= all->number_of_philosophers) + { + pthread_mutex_destroy(&all->data_philo->philors[i].eat_mutex); + pthread_mutex_destroy(&all->data_philo->mutex[i]); + i++; + } + i = 0; + while (i < all->number_of_philosophers) + { + free(&all->data_philo->philors[i]); + i++; + } +}