diff --git a/.vscode/launch.json b/.vscode/launch.json index b114f30..27c4e51 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,8 +8,8 @@ "type": "lldb", "request": "launch", "name": "Debug", - "program": "${workspaceFolder}/philo_one/philo_one", - "args": ["5", "1", "1", "1", "1"], + "program": "${workspaceFolder}/philo_two/philo_two", + "args": ["5", "800", "200", "200"], "cwd": "${workspaceFolder}" } ] diff --git a/philo_two/includes/philo_header.h b/philo_two/includes/philo_header.h index 3fdbb1e..d280ce8 100644 --- a/philo_two/includes/philo_header.h +++ b/philo_two/includes/philo_header.h @@ -13,14 +13,6 @@ # define _SEM_NAME_FORKS "pSemForks" # define _SEM_WRITE_LOCK "write" -#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 @@ -32,7 +24,7 @@ typedef struct s_all int number_of_times_each_philosopher_must_eat; time_t start_time; t_philo_data *data_philo; - sem_t write; + sem_t *write; int smert; int opt; } t_all; @@ -82,6 +74,10 @@ void my_usleep(long time); void ft_exit(t_all *all); char *ft_itoa(int n); char *ft_strjoin(char const *s1, char const *s2); -char *spread_name_and_number(char *str, int i); sem_t *reopen_sem(const char *name, int value); +char *sem_set_name(char *str, int i); +size_t ft_strlcpy(char *dst, const char *src, size_t size); +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); #endif diff --git a/philo_two/srcs/logic.c b/philo_two/srcs/logic.c index 4057c47..413c1c7 100644 --- a/philo_two/srcs/logic.c +++ b/philo_two/srcs/logic.c @@ -27,25 +27,27 @@ void take_forks(t_philo *philo) { t_all *all; all = philo->all_struct; - pthread_mutex_lock(&all->data_philo->mutex[philo->less_fork]); // TODO передать на на семафор + sem_wait(all->data_philo->sems); messages(philo->all_struct, philo, "has taken a fork"); - pthread_mutex_lock(&all->data_philo->mutex[philo->more_fork]); + sem_wait(all->data_philo->sems); messages(philo->all_struct, philo, "has taken a fork"); } void eating(t_philo *philo) { + sem_wait(philo->eat_sem); 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++; + sem_post(philo->eat_sem); } 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]); + sem_post(all->data_philo->sems); + sem_post(all->data_philo->sems); ft_msleep(philo->all_struct->time_to_sleep); } @@ -67,6 +69,7 @@ int run_observer(t_all *all) i = 0; while(i <= all->number_of_philosophers) { + sem_wait(all->data_philo->philors[i].eat_sem); if (all->smert != 42) { time_eat = (ft_get_time() - all->start_time) - all->data_philo->philors[i].last_eat; @@ -78,6 +81,7 @@ int run_observer(t_all *all) } else return(2); + sem_post(all->data_philo->philors[i].eat_sem); i++; } } diff --git a/philo_two/srcs/prepare_simulation.c b/philo_two/srcs/prepare_simulation.c index f54e994..7e18904 100644 --- a/philo_two/srcs/prepare_simulation.c +++ b/philo_two/srcs/prepare_simulation.c @@ -32,7 +32,7 @@ void run_simulation(t_all *all) } } -void get_fork_for_philo(t_all *all) +void get_data_for_philo(t_all *all) { int i; @@ -64,6 +64,7 @@ int prepare_resources(t_all *all) if (all->write == SEM_FAILED) return(1); all->data_philo->philors = (t_philo *)ft_calloc(sizeof(t_philo), all->number_of_philosophers); + get_data_for_philo(all); return (0); } diff --git a/philo_two/srcs/utilc.c b/philo_two/srcs/utilc.c index f517bde..0b25c8e 100644 --- a/philo_two/srcs/utilc.c +++ b/philo_two/srcs/utilc.c @@ -13,6 +13,68 @@ t_all *structalloc_and_bzero(void) return(all); } +size_t ft_strlcpy(char *dst, const char *src, size_t size) +{ + unsigned int i; + unsigned int count; + + count = 0; + if (!dst && !src) + return (0); + while (src[count] != '\0') + { + ++count; + } + i = 0; + if (size > 0) + { + while (src[i] != '\0' && i < (size - 1) && src[i]) + { + dst[i] = src[i]; + ++i; + } + dst[i] = '\0'; + } + return (count); +} + +size_t ft_strlcat(char *dst, const char *src, size_t size) +{ + unsigned int i; + unsigned int len; + + len = 0; + if (!dst && !src) + return (0); + len = ft_strlen(dst); + i = 0; + if (size < len) + return (ft_strlen((char *)src) + size); + if (size) + { + while (src[i] && len + 1 < size) + dst[len++] = src[i++]; + dst[len] = '\0'; + } + while (src[i]) + { + i++; + len++; + } + return (len); +} + + +int ft_strlen(char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return(i); +} + int return_big(int a, int b) { if (a > b)