Fix most of compilation issues.
authorLukas Jiriste <ljiriste@student.42prague.com>
Sun, 12 May 2024 19:56:51 +0000 (21:56 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sun, 12 May 2024 19:56:51 +0000 (21:56 +0200)
I didn't want to make all the changes in a single commit so I split the
work. The project could not be built because of the partial changes
hence I didn't think about trying to make.
I've tried to build today and found many minor problems with the code
produced since commit 60f1edc (the copy to bonus).

I don't have much time and strength today but I wanted to be productive
so I've at least done this.

philo_bonus/helpers.c
philo_bonus/main.c
philo_bonus/mem_management.c
philo_bonus/philo.c
philo_bonus/philo.h

index 1c250bdd57ae344c3e50b303054524ac6ed26a55..fd44d42c165bba2d8f910e16c5095ddb11a2c91b 100644 (file)
@@ -6,7 +6,7 @@
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -26,8 +26,7 @@ useconds_t    usecs_since_start(struct timeval start)
 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 ;
index 1b72eaaaec0c51d8a54de0e2c2e10e672503ae83..36e1cd290a37692b5cc8974f9e85a3283d391eaf 100644 (file)
@@ -6,15 +6,18 @@
 /*   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)
 {
@@ -24,13 +27,13 @@ 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);
@@ -64,9 +67,9 @@ int   main(int argc, char **argv)
                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);
        }
index 0ade198180b65e927c5abe7b9e0f0db299b3fa21..25d5e28b0b5fd74a5c2b4df138656948f5e38bb7 100644 (file)
@@ -6,7 +6,7 @@
 /*   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);
@@ -38,10 +38,12 @@ static int  open_semaphores(sem_t *semaphores[sem_num])
 
 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)
@@ -60,7 +62,7 @@ void  cleanup(pid_t *philo_pids, sem_t *semaphores[sem_num])
        size_t  i;
 
        i = 0;
-       while (i < SEM_NUM)
+       while (i < sem_num)
        {
                if (semaphores[i] != SEM_FAILED)
                        sem_close(semaphores[i]);
index 7a7877eb8920639536cb3be8f2fd8675a6b5fa24..5acbb9ef33146d0a7c7f20defbb6f6389ebab810 100644 (file)
@@ -6,7 +6,7 @@
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -44,7 +44,7 @@ static void   philo_eat(t_philo *philo)
        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 ;
@@ -53,7 +53,7 @@ static void   philo_eat(t_philo *philo)
 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 ;
 }
 
index c1df3c5ffcedd655cc58acfd0fcfb097a767ffeb..7f1e43e3edd7a81664db6b0b509d917acc9659c6 100644 (file)
@@ -6,7 +6,7 @@
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -14,7 +14,7 @@
 # define PHILO_H
 
 # include <stddef.h>
-# include <pthread.h>
+# include <semaphore.h>
 # include <sys/time.h>
 # include <unistd.h>
 
@@ -30,7 +30,7 @@ typedef enum e_semaphores
        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
@@ -63,12 +63,6 @@ typedef struct s_philosopher
        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,
@@ -79,6 +73,6 @@ useconds_t    usecs_since_start(struct timeval start);
 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