/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 09:13:40 by ljiriste #+# #+# */
-/* Updated: 2024/05/24 14:08:05 by ljiriste ### ########.fr */
+/* Updated: 2024/05/24 15:32:49 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
{
if (is_alive(philo))
{
- sem_wait(philo->semaphores[term]);
+ sem_wait(philo->semaphores.term);
print_timestamp(philo, str);
- sem_post(philo->semaphores[term]);
+ sem_post(philo->semaphores.term);
}
return ;
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 08:49:46 by ljiriste #+# #+# */
-/* Updated: 2024/05/24 10:33:27 by ljiriste ### ########.fr */
+/* Updated: 2024/05/24 15:33:10 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
time = time_till_death(philo);
}
philo->alive = 0;
- sem_post(philo->semaphores[death]);
+ sem_post(philo->semaphores.death);
sem_post(philo->philo_sem);
return (NULL);
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 11:19:48 by ljiriste #+# #+# */
-/* Updated: 2024/05/24 09:21:34 by ljiriste ### ########.fr */
+/* Updated: 2024/05/24 15:21:33 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
{
t_settings settings;
pid_t *philo_pids;
- sem_t *semaphores[sem_num];
+ t_sems semaphores;
if (parse_input(&settings, argc, argv))
{
return (1);
}
- if (init(settings.philo_count, &philo_pids, semaphores))
+ if (init(settings.philo_count, &philo_pids, &semaphores))
{
- cleanup(philo_pids, semaphores);
+ cleanup(philo_pids, &semaphores);
return (2);
}
if (seat_philosophers(&settings, philo_pids))
{
kill_philosophers(settings.philo_count, philo_pids);
- cleanup(philo_pids, semaphores);
+ cleanup(philo_pids, &semaphores);
return (3);
}
- sem_post(semaphores[term]);
- handle_philosophers(semaphores);
- cleanup(philo_pids, semaphores);
+ sem_post(semaphores.term);
+ handle_philosophers(&semaphores);
+ cleanup(philo_pids, &semaphores);
return (0);
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 09:39:55 by ljiriste #+# #+# */
-/* Updated: 2024/05/12 21:54:03 by ljiriste ### ########.fr */
+/* Updated: 2024/05/24 15:22:19 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include <sys/stat.h>
#include <semaphore.h>
-static int open_semaphores(unsigned int count, sem_t *semaphores[sem_num])
+static int open_semaphores(unsigned int count, t_sems *semaphores)
{
- size_t i;
-
- semaphores[forks] = sem_open(FORKS_SEM, O_CREAT | O_EXCL,
+ semaphores->forks = sem_open(FORKS_SEM, O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR, count);
- semaphores[fed] = sem_open(WELL_FED_SEM, O_CREAT | O_EXCL,
+ semaphores->fed = sem_open(WELL_FED_SEM, O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR, count);
- semaphores[death] = sem_open(DEATH_SEM, O_CREAT | O_EXCL,
+ semaphores->death = sem_open(DEATH_SEM, O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR, count);
- semaphores[term] = sem_open(TERM_SEM, O_CREAT | O_EXCL,
+ semaphores->term = sem_open(TERM_SEM, O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR, 1);
- i = 0;
- while (i < sem_num)
- if (semaphores[i++] == SEM_FAILED)
- return (1);
+ if (semaphores->forks == SEM_FAILED || semaphores->fed == SEM_FAILED
+ || semaphores->death == SEM_FAILED || semaphores->term == SEM_FAILED)
+ return (1);
return (0);
}
-int init(unsigned int count, pid_t **philo_pids, sem_t *semaphores[sem_num])
+int init(unsigned int count, pid_t **philo_pids, t_sems *semaphores)
{
size_t i;
while (i < count)
{
(*philo_pids)[i] = 0;
- sem_wait(semaphores[death]);
- sem_wait(semaphores[fed]);
+ sem_wait(semaphores->death);
+ sem_wait(semaphores->fed);
++i;
}
- sem_wait(semaphores[term]);
+ sem_wait(semaphores->term);
return (0);
}
-void cleanup(pid_t *philo_pids, sem_t *semaphores[sem_num])
+void close_semaphores(t_sems *semaphores)
{
- size_t i;
+ sem_close(semaphores->forks);
+ sem_close(semaphores->fed);
+ sem_close(semaphores->death);
+ sem_close(semaphores->term);
+ return ;
+}
- i = 0;
- while (i < sem_num)
- sem_close(semaphores[i++]);
+void cleanup(pid_t *philo_pids, t_sems *semaphores)
+{
+ close_semaphores(semaphores);
sem_unlink(FORKS_SEM);
sem_unlink(WELL_FED_SEM);
sem_unlink(DEATH_SEM);
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 08:45:21 by ljiriste #+# #+# */
-/* Updated: 2024/05/24 14:08:23 by ljiriste ### ########.fr */
+/* Updated: 2024/05/24 15:22:54 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
{
char *sem_name;
- philo->semaphores[forks] = sem_open(FORKS_SEM, 0);
- philo->semaphores[fed] = sem_open(WELL_FED_SEM, 0);
- philo->semaphores[death] = sem_open(DEATH_SEM, 0);
- philo->semaphores[term] = sem_open(TERM_SEM, 0);
+ philo->semaphores.forks = sem_open(FORKS_SEM, 0);
+ philo->semaphores.fed = sem_open(WELL_FED_SEM, 0);
+ 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);
if (philo->philo_sem == SEM_FAILED)
{
- sem_wait(philo->semaphores[term]);
+ sem_wait(philo->semaphores.term);
printf("An error has occured.\n");
- sem_wait(philo->semaphores[death]);
+ sem_wait(philo->semaphores.death);
free(sem_name);
return (NULL);
}
static void philo_eat(t_philo *philo)
{
- sem_wait(philo->semaphores[forks]);
+ sem_wait(philo->semaphores.forks);
report(philo, "has taken a fork");
- sem_wait(philo->semaphores[forks]);
+ sem_wait(philo->semaphores.forks);
report(philo, "has taken a fork");
sem_wait(philo->philo_sem);
philo->last_eaten = usecs_since_start(philo->settings.start);
report(philo, "is eating");
if (philo->settings.should_check_hunger &&
philo->num_eaten == philo->settings.min_eat_num)
- sem_post(philo->semaphores[fed]);
+ sem_post(philo->semaphores.fed);
usleep(philo->settings.time_to_eat);
- sem_post(philo->semaphores[forks]);
- sem_post(philo->semaphores[forks]);
+ sem_post(philo->semaphores.forks);
+ sem_post(philo->semaphores.forks);
return ;
}
philo_sleep(philo);
}
destroy_hunger_watcher(hunger_watcher);
- close_semaphores(philo);
+ close_semaphores(&philo->semaphores);
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 14:08:05 by ljiriste ### ########.fr */
+/* Updated: 2024/05/24 15:23:21 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
# define DEATH_SEM "philosophers_death"
# define TERM_SEM "philosophers_terminal"
-typedef enum e_semaphores
-{
- forks,
- fed,
- death,
- term,
- sem_num,
-} t_my_sems;
-
// The .start member holds the result of gettimeofday
// Every other member that holds time info counts
// microseconds from that point (in other structures also)
struct timeval start;
} t_settings;
+typedef struct s_important_semaphores
+{
+ sem_t *forks;
+ sem_t *fed;
+ sem_t *death;
+ sem_t *term;
+} t_sems;
+
// As mutex is not available in bonus, the idea is to have
// a semaphore with value 1 to for the access to terminal.
// The well_fed_sem(aphore) and death_sem(aphore) will be taken
unsigned int id;
int alive;
useconds_t last_eaten;
- sem_t *semaphores[sem_num];
+ t_sems semaphores;
sem_t *philo_sem;
t_settings settings;
} t_philo;
int parse_input(t_settings *settings, int argc, char **argv);
int init(unsigned int count, pid_t **philo_pids,
- sem_t *semaphores[sem_num]);
-void cleanup(pid_t *philo_pids, sem_t *semaphores[sem_num]);
+ t_sems *semaphores);
+void cleanup(pid_t *philo_pids, t_sems *semaphores);
+void close_semaphores(t_sems *semaphores);
useconds_t usecs_since_start(struct timeval start);
int is_alive(t_philo *philo);