inner_pars_arg.c \
mem_management.c \
hunger_watcher.c \
+ libft_helpers.c \
SRCS := $(addprefix $(SRCDIR)/, $(SRCS))
OBJS := $(SRCS:%.c=%.o)
/* 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)
{
}
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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* 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);
+}
/* 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 */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
-static int open_semaphores(t_philo *philo)
+static char *open_semaphores(t_philo *philo)
{
char *sem_name;
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)
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);
philo_sleep(philo);
}
destroy_hunger_watcher(hunger_watcher);
+ close_semaphores(philo);
+ sem_unlink(philo_sem_name);
+ free(philo_sem_name);
exit(0);
}
/* 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 */
/* */
/* ************************************************************************** */
# include <semaphore.h>
# include <sys/time.h>
# include <unistd.h>
+# include <stdint.h>
# define FORKS_SEM "philosophers_forks"
# define WELL_FED_SEM "philosophers_fed"
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