From: Lukas Jiriste Date: Fri, 24 May 2024 12:25:46 +0000 (+0200) Subject: Add create_philo_sem_name and supporting functions X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=46653b84f6eaae0163c0314b7114c3b99c20a384;p=42%2Fphilosophers.git Add create_philo_sem_name and supporting functions --- diff --git a/philo_bonus/Makefile b/philo_bonus/Makefile index 67db9eb..ebd147c 100644 --- a/philo_bonus/Makefile +++ b/philo_bonus/Makefile @@ -15,6 +15,7 @@ SRCS := main.c \ inner_pars_arg.c \ mem_management.c \ hunger_watcher.c \ + libft_helpers.c \ SRCS := $(addprefix $(SRCDIR)/, $(SRCS)) OBJS := $(SRCS:%.c=%.o) diff --git a/philo_bonus/helpers.c b/philo_bonus/helpers.c index bfed896..46bddff 100644 --- a/philo_bonus/helpers.c +++ b/philo_bonus/helpers.c @@ -6,13 +6,14 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 09:13:40 by ljiriste #+# #+# */ -/* Updated: 2024/05/24 10:26:10 by ljiriste ### ########.fr */ +/* Updated: 2024/05/24 14:08:05 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include "philo.h" #include #include +#include useconds_t usecs_since_start(struct timeval start) { @@ -50,3 +51,32 @@ void report(t_philo *philo, const char *str) } return ; } + +static const char *philo_sem_name_base = "philosophers_philo_"; + +char *create_philo_sem_name(unsigned int id) +{ + char *name; + size_t i; + size_t j; + char *id_str; + + id_str = ft_uitoa_base(id, "0123456789"); + name = malloc(ft_strlen(philo_sem_name_base) + ft_strlen(id_str) + 1); + if (!name) + return (NULL); + i = 0; + while(philo_sem_name_base[i]) + { + name[i] = philo_sem_name_base[i]; + ++i; + } + j = 0; + while (id_str[j]) + { + name[i + j] = id_str[j]; + ++j; + } + free(id_str); + return (name); +} diff --git a/philo_bonus/libft_helpers.c b/philo_bonus/libft_helpers.c new file mode 100644 index 0000000..fc59d1f --- /dev/null +++ b/philo_bonus/libft_helpers.c @@ -0,0 +1,85 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft_helpers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 09:46:11 by ljiriste #+# #+# */ +/* Updated: 2024/05/24 14:03:59 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "philo.h" +#include //malloc +#include //intmax_t + +static int size_needed(uintmax_t n, size_t base_len) +{ + int res; + + if (n == 0) + return (2); + res = 1; + while (n != 0) + { + ++res; + n /= base_len; + } + return (res); +} + +static int valid_base(const char *base) +{ + int i; + int j; + + if (ft_strlen(base) < 2) + return (0); + i = 0; + while (base[i]) + { + j = i + 1; + while (base[j]) + if (base[i] == base[j++]) + return (0); + ++i; + } + return (1); +} + +size_t ft_strlen(const char *str) +{ + size_t i; + + if (!str) + return (0); + i = 0; + while (str[i]) + ++i; + return (i); +} + +char *ft_uitoa_base(uintmax_t n, const char *base) +{ + int size; + int base_len; + char *res; + + if (!valid_base(base)) + return (NULL); + base_len = ft_strlen(base); + size = size_needed(n, base_len); + res = malloc(size); + if (res == NULL) + return (res); + res[--size] = '\0'; + if (n == 0) + res[0] = base[0]; + while (n != 0) + { + res[--size] = base[n % base_len]; + n /= base_len; + } + return (res); +} diff --git a/philo_bonus/philo.c b/philo_bonus/philo.c index b9ce802..92211f9 100644 --- a/philo_bonus/philo.c +++ b/philo_bonus/philo.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 08:45:21 by ljiriste #+# #+# */ -/* Updated: 2024/05/24 10:32:38 by ljiriste ### ########.fr */ +/* Updated: 2024/05/24 14:08:23 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ #include #include -static int open_semaphores(t_philo *philo) +static char *open_semaphores(t_philo *philo) { char *sem_name; @@ -25,16 +25,18 @@ static int open_semaphores(t_philo *philo) philo->semaphores[death] = sem_open(DEATH_SEM, 0); philo->semaphores[term] = sem_open(TERM_SEM, 0); sem_name = create_philo_sem_name(philo->id); + if (!sem_name) + return (NULL); philo->philo_sem = sem_open(sem_name, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 1); - free(sem_name); if (philo->philo_sem == SEM_FAILED) { sem_wait(philo->semaphores[term]); printf("An error has occured.\n"); sem_wait(philo->semaphores[death]); - return (1); + free(sem_name); + return (NULL); } - return (0); + return (sem_name); } static void philo_think(t_philo *philo) @@ -73,8 +75,10 @@ static void philo_sleep(t_philo *philo) void be_a_philosopher(t_philo *philo) { pthread_t hunger_watcher; + char *philo_sem_name; - if (open_semaphores(philo)) + philo_sem_name = open_semaphores(philo); + if (!philo_sem_name) exit(2); if (create_hunger_watcher(philo, &hunger_watcher)) exit(3); @@ -85,5 +89,8 @@ void be_a_philosopher(t_philo *philo) philo_sleep(philo); } destroy_hunger_watcher(hunger_watcher); + close_semaphores(philo); + sem_unlink(philo_sem_name); + free(philo_sem_name); exit(0); } diff --git a/philo_bonus/philo.h b/philo_bonus/philo.h index 65af18e..1916549 100644 --- a/philo_bonus/philo.h +++ b/philo_bonus/philo.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/22 11:10:17 by ljiriste #+# #+# */ -/* Updated: 2024/05/24 10:32:59 by ljiriste ### ########.fr */ +/* Updated: 2024/05/24 14:08:05 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ # include # include # include +# include # define FORKS_SEM "philosophers_forks" # define WELL_FED_SEM "philosophers_fed" @@ -75,10 +76,14 @@ useconds_t usecs_since_start(struct timeval start); int is_alive(t_philo *philo); void report(t_philo *philo, const char *str); void print_timestamp(t_philo *philo, const char *str); +char *create_philo_sem_name(unsigned int id); int create_hunger_watcher(t_philo *philo, pthread_t *hunger_watcher); void destroy_hunger_watcher(pthread_t hunger_watcher); +size_t ft_strlen(const char *str); +char *ft_uitoa_base(uintmax_t n, const char *base); + void be_a_philosopher(t_philo *philo); #endif //PHILO_H