Add create_philo_sem_name and supporting functions
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 24 May 2024 12:25:46 +0000 (14:25 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 24 May 2024 12:25:46 +0000 (14:25 +0200)
philo_bonus/Makefile
philo_bonus/helpers.c
philo_bonus/libft_helpers.c [new file with mode: 0644]
philo_bonus/philo.c
philo_bonus/philo.h

index 67db9ebc70ed5eac25714bab687433c24ea9af68..ebd147c77289a7ebf3b8efb069b881145fb28e2b 100644 (file)
@@ -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)
index bfed896b128a791ad06d9014a2ade9bc8b57bfa8..46bddffae3793cf21d391c76e6a43fd21166d133 100644 (file)
@@ -6,13 +6,14 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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 <sys/time.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 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 (file)
index 0000000..fc59d1f
--- /dev/null
@@ -0,0 +1,85 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   libft_helpers.c                                    :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/09/05 09:46:11 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/05/24 14:03:59 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "philo.h"
+#include <stdlib.h>    //malloc
+#include <stdint.h>    //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);
+}
index b9ce802c1b6d7d5ff5c8f7c64c57ee6c4dfb94ff..92211f9d26bf4844e18075ebdd983d318c9b927b 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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 <stdio.h>
 #include <stdlib.h>
 
-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);
 }
index 65af18edfa166c786790ad76c2baa328ba735aea..1916549c7c81f53953c3e1f8d3da1578c99ab497 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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 <semaphore.h>
 # include <sys/time.h>
 # include <unistd.h>
+# include <stdint.h>
 
 # 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