Create outline of the solution
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 26 Mar 2024 08:13:48 +0000 (09:13 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Tue, 26 Mar 2024 08:13:48 +0000 (09:13 +0100)
philo/main.c [new file with mode: 0644]
philo/philo.h [new file with mode: 0644]

diff --git a/philo/main.c b/philo/main.c
new file mode 100644 (file)
index 0000000..ee3eba9
--- /dev/null
@@ -0,0 +1,59 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   main.c                                             :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/22 11:19:48 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/03/26 09:13:38 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "philo.h"
+#include <stddef.h>
+#include <pthread.h>
+#include <sys/time.h>
+
+int    parse_input(t_settings *settings, int argc, char **argv)
+{
+       if (argc != 5 && argc != 6)
+               return (1);
+       settings->end = 0;
+       settings->should_check_hunger = 0;
+       if (parse_arg(&settings->philo_count, argv[1])
+               || parse_arg(&settings->time_to_die, argv[2])
+               || parse_arg(&settings->time_to_eat, argv[3])
+               || parse_arg(&settings->time_to_sleep, argv[4]))
+       {
+               return (1);
+       }
+       if (argc == 6)
+       {
+               if (parse_arg(&settings->min_eat_num, argv[5]))
+                       return (1);
+               settings->should_check_hunger = 1;
+       }
+       return (0);
+}
+
+int    main(int argc, char **argv)
+{
+       t_diner         diner;
+       pthread_t       *threads;
+
+       if (parse_input(&diner.setting, argc, argv))
+       {
+               return (1);
+       }
+       if (init(&diner, threads))
+       {
+               cleanup(&diner, threads);
+               return (2);
+       }
+       seat_philosophers(&diner, threads);
+       wait_till_death_possible(diner.setting.time_to_die, diner.setting.start);
+       report_end(watch_philosophers(&diner));
+       cleanup(&diner, threads);
+       return (0);
+}
diff --git a/philo/philo.h b/philo/philo.h
new file mode 100644 (file)
index 0000000..a618cd5
--- /dev/null
@@ -0,0 +1,51 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   philo.h                                            :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/22 11:10:17 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/03/26 09:13:38 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef PHILO_H
+# define PHILO_H
+
+# include <stddef.h>
+# include <pthread.h>
+# include <sys/time.h>
+
+typedef struct s_settings
+{
+       int                             end;
+       int                             should_check_hunger;
+       size_t                  philo_count;
+       suseconds_t             time_to_die;
+       suseconds_t             time_to_eat;
+       suseconds_t             time_to_sleep;
+       suseconds_t             start;
+       unsigned int    min_eat_num;
+       pthread_mutex_t terminal_lock;
+       pthread_mutex_t end_lock;
+}                                      t_settings;
+
+typedef struct s_philosopher
+{
+       int                             num_eaten;
+       size_t                  id;
+       suseconds_t             last_eaten;
+       t_settings              *settings;
+       pthread_mutex_t *forks[2];
+       pthread_mutex_t philo_lock;
+}                                      t_philo;
+
+typedef struct s_diner
+{
+       t_settings              setting;
+       t_philo                 *philos;
+       pthread_mutex_t *forks;
+}                                      t_diner;
+
+#endif //PHILO_H