]> git.dkaiser.de - 42/Philosophers.git/commitdiff
Add structs and init
authorDominik Kaiser <dkaiser@student.42heilbronn.de>
Sat, 18 Jan 2025 10:21:35 +0000 (11:21 +0100)
committerDominik Kaiser <dkaiser@student.42heilbronn.de>
Sat, 18 Jan 2025 10:21:35 +0000 (11:21 +0100)
philo/include/philo.h
philo/src/main.c

index 1ec9c3d522c695704c00e4cfd59630b10be78ccd..cfc832ee6a3f6bebe0f40bbad6a28ab149ea8147 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/01/17 10:23:19 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/17 12:01:01 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/01/18 11:20:26 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 # include "ft_utils.h"
 
 # define ERR_USAGE "Usage: <nbr_of_philos> <ttd> <tte> <tts> [times_must_eat]"
+# define ERR_MALLOC "Memory allocation failed"
+
+typedef struct s_fork
+{
+    int available;
+    pthread_mutex_t mutex;
+} t_fork;
 
 typedef struct s_phdata
 {
@@ -29,6 +36,17 @@ typedef struct s_phdata
     int time_to_eat;
     int time_to_sleep;
     int times_must_eat;
+    int simulation_running;
+    t_fork *forks;
 } t_phdata;
 
+typedef struct s_philo
+{
+    int id;
+    int times_eaten;
+    int is_alive;
+    pthread_t thread;
+    t_phdata *data;
+} t_philo;
+
 #endif
index e3b90ab7970aa64f485b09bee8c9a7b9b28c1bde..104f902ba5a8bd72aa338db008e9e64ec28606e8 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/01/14 17:13:30 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/17 12:40:05 by dkaiser          ###   ########.fr       */
+/*   Updated: 2025/01/18 11:20:47 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -22,7 +22,7 @@ int load_data(t_phdata *data, int argc, char *argv[])
     if (argc == 6)
         data->times_must_eat = ft_atoi(argv[2]);
     else
-        data->times_must_eat = 0;
+        data->times_must_eat = -1;
     if (data->nbr_of_philos <= 0)
         return (ft_err("Must have at least one philosopher"));
     if (data->time_to_die < 0)
@@ -31,14 +31,40 @@ int load_data(t_phdata *data, int argc, char *argv[])
         return (ft_err("tte can't be negative"));
     if (data->time_to_sleep < 0)
         return (ft_err("tts can't be negative"));
-    if (data->times_must_eat < 0)
-        data->times_must_eat = 0;
+    return (EXIT_SUCCESS);
+}
+
+int init(t_philo **philos, t_phdata *data)
+{
+    int i;
+
+    *philos = (t_philo *)malloc(sizeof(t_philo) * data->nbr_of_philos);
+    if (*philos == NULL)
+        return (ft_err(ERR_MALLOC));
+    data->forks = (t_fork *)malloc(sizeof(int) * data->nbr_of_philos);
+    if (data->forks == NULL)
+    {
+        free(*philos);
+        return (ft_err(ERR_MALLOC));
+    }
+    i = 0;
+    while (i < data->nbr_of_philos)
+    {
+        (*philos)[i].id = i + 1;
+        (*philos)[i].is_alive = 1;
+        (*philos)[i].times_eaten = 0;
+        (*philos)[i].data = data;
+        data->forks[i].available = 1;
+        pthread_mutex_init(&(data->forks[i].mutex), NULL);
+        i++;
+    }
     return (EXIT_SUCCESS);
 }
 
 int main(int argc, char *argv[])
 {
     t_phdata data;
+    t_philo *philos;
     int result;
 
     if (argc != 5 && argc != 6)
@@ -46,5 +72,10 @@ int main(int argc, char *argv[])
     result = load_data(&data, argc, argv);
     if (result != EXIT_SUCCESS)
         return (result);
-    return (EXIT_SUCCESS);
+    result = init(&philos, &data);
+    if (result != EXIT_SUCCESS)
+        return (result);
+    free(philos);
+    free(data.forks);
+    return (result);
 }