/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 11:19:48 by ljiriste #+# #+# */
-/* Updated: 2024/05/30 12:54:56 by ljiriste ### ########.fr */
+/* Updated: 2024/05/30 13:16:05 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include <sys/types.h>
#include <signal.h>
-static int seat_philosophers(t_settings *settings, pid_t *philo_pids, t_sems *semaphores)
+static int seat_philosophers(t_settings *settings, t_sems *semaphores)
{
t_philo philo;
+ pid_t pid;
philo.num_eaten = 0;
gettimeofday(&settings->start, NULL);
philo.last_eaten = 0;
while (philo.id <= settings->philo_count)
{
- philo_pids[philo.id - 1] = fork();
- if (philo_pids[philo.id - 1] == 0)
+ pid = fork();
+ if (pid == 0)
{
close_semaphores(semaphores);
- free(philo_pids);
be_a_philosopher(&philo);
}
- else if (philo_pids[philo.id - 1] < 0)
+ else if (pid < 0)
return (1);
++philo.id;
}
return (0);
}
-void kill_philosophers(unsigned int count, pid_t *philo_pids)
+void kill_philosophers(t_sems *semaphores)
{
- while (count > 0)
- {
- --count;
- if (philo_pids[count] != 0)
- kill(philo_pids[count], SIGTERM);
- }
+ sem_post(semaphores->death);
+ return ;
}
void *check_fed(void *input)
int main(int argc, char **argv)
{
t_settings settings;
- pid_t *philo_pids;
t_sems semaphores;
if (parse_input(&settings, argc, argv))
{
return (1);
}
- if (init(settings.philo_count, &philo_pids, &semaphores))
+ if (init(settings.philo_count, &semaphores))
{
- cleanup(philo_pids, &semaphores);
+ cleanup(&semaphores);
return (2);
}
- if (seat_philosophers(&settings, philo_pids, &semaphores))
+ if (seat_philosophers(&settings, &semaphores))
{
- kill_philosophers(settings.philo_count, philo_pids);
- cleanup(philo_pids, &semaphores);
+ kill_philosophers(&semaphores);
+ cleanup(&semaphores);
return (3);
}
sem_post(semaphores.term);
wait_for_end(settings.philo_count, &semaphores);
- cleanup(philo_pids, &semaphores);
+ cleanup(&semaphores);
return (0);
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 09:39:55 by ljiriste #+# #+# */
-/* Updated: 2024/05/30 13:01:59 by ljiriste ### ########.fr */
+/* Updated: 2024/05/30 13:17:33 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
return (0);
}
-int init(unsigned int count, pid_t **philo_pids, t_sems *semaphores)
+int init(unsigned int count, t_sems *semaphores)
{
- *philo_pids = malloc(count * sizeof(**philo_pids));
- if (!*philo_pids)
- return (1);
- if (open_semaphores(count, semaphores))
- return (1);
- return (0);
+ return (open_semaphores(count, semaphores));
}
void close_semaphores(t_sems *semaphores)
return ;
}
-void cleanup(pid_t *philo_pids, t_sems *semaphores)
+void cleanup(t_sems *semaphores)
{
close_semaphores(semaphores);
sem_unlink(FORKS_SEM);
sem_unlink(TERM_SEM);
sem_unlink(END_SEM);
sem_unlink(CHECK_END_SEM);
- free(philo_pids);
return ;
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 11:10:17 by ljiriste #+# #+# */
-/* Updated: 2024/05/30 12:18:20 by ljiriste ### ########.fr */
+/* Updated: 2024/05/30 13:15:08 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
int parse_input(t_settings *settings, int argc, char **argv);
-int init(unsigned int count, pid_t **philo_pids,
- t_sems *semaphores);
-void cleanup(pid_t *philo_pids, t_sems *semaphores);
+int init(unsigned int count, t_sems *semaphores);
+void cleanup(t_sems *semaphores);
void close_semaphores(t_sems *semaphores);
useconds_t usecs_since_start(struct timeval start);