/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 09:13:40 by ljiriste #+# #+# */
-/* Updated: 2024/05/10 12:11:52 by ljiriste ### ########.fr */
+/* Updated: 2024/05/12 21:54:46 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
void print_timestamp(t_philo *philo, const char *str)
{
sem_wait(philo->semaphores[term]);
- mutex_lock(&philo->settings->terminal_lock);
- printf("%u %lu %s\n", usecs_since_start(philo->settings.start)
+ printf("%u %u %s\n", usecs_since_start(philo->settings.start)
/ 1000, philo->id, str);
sem_post(philo->semaphores[term]);
return ;
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 11:19:48 by ljiriste #+# #+# */
-/* Updated: 2024/05/10 11:48:05 by ljiriste ### ########.fr */
+/* Updated: 2024/05/12 21:48:59 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
#include <stddef.h>
+#include <stdlib.h>
#include <pthread.h>
#include <limits.h>
#include <stdio.h>
+#include <sys/types.h>
+#include <signal.h>
static int seat_philosophers(t_settings *settings, pid_t *philo_pids)
{
gettimeofday(&settings->start, NULL);
philo.settings = *settings;
philo.id = 1;
- while (philo.id <= setting->philo_count)
+ while (philo.id <= settings->philo_count)
{
philo_pids[philo.id - 1] = fork();
if (philo_pids[philo.id - 1] == 0)
{
free(philo_pids);
- be_a_philosopher(*philo);
+ be_a_philosopher(&philo);
}
else if (philo_pids[philo.id - 1] < 0)
return (1);
cleanup(philo_pids, semaphores);
return (2);
}
- if (seat_philosophers(*settings, philo_pids))
+ if (seat_philosophers(&settings, philo_pids))
{
- kill_philosophers(setings.philo_count, philo_pids);
+ kill_philosophers(settings.philo_count, philo_pids);
cleanup(philo_pids, semaphores);
return (3);
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 09:39:55 by ljiriste #+# #+# */
-/* Updated: 2024/05/10 11:25:43 by ljiriste ### ########.fr */
+/* Updated: 2024/05/12 21:54:03 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include <sys/stat.h>
#include <semaphore.h>
-static int open_semaphores(sem_t *semaphores[sem_num])
+static int open_semaphores(unsigned int count, sem_t *semaphores[sem_num])
{
size_t i;
semaphores[forks] = sem_open(FORKS_SEM, O_CREAT | O_EXCL,
- S_IRUSR | S_IWUSR, setings->philo_count);
+ S_IRUSR | S_IWUSR, count);
semaphores[fed] = sem_open(WELL_FED_SEM, O_CREAT | O_EXCL,
- S_IRUSR | S_IWUSR, setings->philo_count);
+ S_IRUSR | S_IWUSR, count);
semaphores[death] = sem_open(DEATH_SEM, O_CREAT | O_EXCL,
- S_IRUSR | S_IWUSR, setings->philo_count);
+ S_IRUSR | S_IWUSR, count);
semaphores[term] = sem_open(TERM_SEM, O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR, 1);
i = 0;
- while (i < SEM_NUM)
+ while (i < sem_num)
if (semaphores[i++] == SEM_FAILED)
return (1);
return (0);
int init(unsigned int count, pid_t **philo_pids, sem_t *semaphores[sem_num])
{
+ size_t i;
+
*philo_pids = malloc(count * sizeof(**philo_pids));
if (!*philo_pids)
return (1);
- if (open_semaphores(semaphores))
+ if (open_semaphores(count, semaphores))
return (1);
i = 0;
while (i < count)
size_t i;
i = 0;
- while (i < SEM_NUM)
+ while (i < sem_num)
{
if (semaphores[i] != SEM_FAILED)
sem_close(semaphores[i]);
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 08:45:21 by ljiriste #+# #+# */
-/* Updated: 2024/05/10 12:12:26 by ljiriste ### ########.fr */
+/* Updated: 2024/05/12 21:52:05 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
if (philo->settings.should_check_hunger &&
philo->num_eaten == philo->settings.min_eat_num)
sem_post(philo->semaphores[fed]);
- usleep(philo->settings->time_to_eat);
+ usleep(philo->settings.time_to_eat);
sem_post(philo->semaphores[forks]);
sem_post(philo->semaphores[forks]);
return ;
static void philo_sleep(t_philo *philo)
{
print_timestamp(philo, "is sleeping");
- usleep(philo->settings->time_to_sleep);
+ usleep(philo->settings.time_to_sleep);
return ;
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 11:10:17 by ljiriste #+# #+# */
-/* Updated: 2024/05/10 12:12:26 by ljiriste ### ########.fr */
+/* Updated: 2024/05/12 21:55:26 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
# define PHILO_H
# include <stddef.h>
-# include <pthread.h>
+# include <semaphore.h>
# include <sys/time.h>
# include <unistd.h>
death,
term,
sem_num,
-} t_my_sems
+} t_my_sems;
// The .start member holds the result of gettimeofday
// Every other member that holds time info counts
t_settings settings;
} t_philo;
-enum e_end
-{
- death,
- well_fed,
-};
-
int parse_input(t_settings *settings, int argc, char **argv);
int init(unsigned int count, pid_t **philo_pids,
int report(t_philo *philo, const char *str);
void print_timestamp(t_philo *philo, const char *str);
-void *be_a_philosopher(void *philo);
+void be_a_philosopher(t_philo *philo);
#endif //PHILO_H