From c4fa26b96d5b3e864479763698fc0a93265b40ad Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Sun, 12 May 2024 21:56:51 +0200 Subject: [PATCH] Fix most of compilation issues. 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 | 5 ++--- philo_bonus/main.c | 13 ++++++++----- philo_bonus/mem_management.c | 18 ++++++++++-------- philo_bonus/philo.c | 6 +++--- philo_bonus/philo.h | 14 ++++---------- 5 files changed, 27 insertions(+), 29 deletions(-) diff --git a/philo_bonus/helpers.c b/philo_bonus/helpers.c index 1c250bd..fd44d42 100644 --- a/philo_bonus/helpers.c +++ b/philo_bonus/helpers.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 ; diff --git a/philo_bonus/main.c b/philo_bonus/main.c index 1b72eaa..36e1cd2 100644 --- a/philo_bonus/main.c +++ b/philo_bonus/main.c @@ -6,15 +6,18 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 +#include #include #include #include +#include +#include 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); } diff --git a/philo_bonus/mem_management.c b/philo_bonus/mem_management.c index 0ade198..25d5e28 100644 --- a/philo_bonus/mem_management.c +++ b/philo_bonus/mem_management.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -17,20 +17,20 @@ #include #include -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]); diff --git a/philo_bonus/philo.c b/philo_bonus/philo.c index 7a7877e..5acbb9e 100644 --- a/philo_bonus/philo.c +++ b/philo_bonus/philo.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 ; } diff --git a/philo_bonus/philo.h b/philo_bonus/philo.h index c1df3c5..7f1e43e 100644 --- a/philo_bonus/philo.h +++ b/philo_bonus/philo.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 -# include +# include # include # include @@ -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 -- 2.30.2