Skip to content

Commit

Permalink
Merge pull request #10 from alx-sch/9-reinstate_monitor
Browse files Browse the repository at this point in the history
9 reinstate monitor
  • Loading branch information
alx-sch authored Oct 11, 2024
2 parents fad1cac + 8e7e004 commit d3b07ff
Show file tree
Hide file tree
Showing 17 changed files with 819 additions and 324 deletions.
12 changes: 8 additions & 4 deletions philo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: aschenk <aschenk@student.42berlin.de> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/03 14:29:17 by aschenk #+# #+# #
# Updated: 2024/10/08 11:07:13 by aschenk ### ########.fr #
# Updated: 2024/10/11 22:16:06 by aschenk ### ########.fr #
# #
# **************************************************************************** #

Expand All @@ -24,15 +24,19 @@ NAME := philo
SRCS_DIR := src
SRCS_FILES := main.c \
1_check_args.c \
2_init_philos.c \
2_init_sim.c \
3_run_sim.c \
dining_rules.c \
3_eat.c \
3_eat_sleep_think.c \
4_monitor.c \
5_run_sim_utils.c \
5_run_sim.c \
utils/0_libft.c \
utils/1_print_action.c \
utils/1_print_error.c \
utils/2_mutex.c \
utils/3_time.c \
utils/free.c \
utils/4_free.c \

SRCS := $(addprefix $(SRCS_DIR)/, $(SRCS_FILES))

Expand Down
17 changes: 14 additions & 3 deletions philo/include/philo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: aschenk <aschenk@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/14 19:21:56 by aschenk #+# #+# */
/* Updated: 2024/10/09 13:53:09 by aschenk ### ########.fr */
/* Updated: 2024/10/11 22:11:17 by aschenk ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -81,19 +81,30 @@ the risk of starvation among others while still prioritizing their own needs.
// 1_check_args.c

int check_args(int argc, char **argv);
int simulate_single_philo(t_sim *sim);

// 2_init_philos.c

int init_philos(t_sim *sim);

// 2_init_sim.c

int init_sim(t_sim **sim, int argc, char **argv);

// 3_eat.c

int pick_forks_and_log(t_philo *philo);
int eat_and_log(t_philo *philo, int t_eat);
int is_philo_full(t_philo *philo, int max_meals);

// 3_run_sim.c

int run_sim(t_sim *sim);

// X_dining.c

void *dining(void *arg);
void *monitoring(void *arg);
void *eat_sleep_think(void *arg);
void *monitor(void *arg);

// utils/0_libft.c

Expand Down
58 changes: 36 additions & 22 deletions philo/include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: aschenk <aschenk@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/19 17:19:12 by aschenk #+# #+# */
/* Updated: 2024/10/09 12:04:11 by aschenk ### ########.fr */
/* Updated: 2024/10/11 22:26:12 by aschenk ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -75,12 +75,13 @@ Structure representing a philosopher in the dining philosophers problem:
- sim: A pointer to the simulation structure, allowing access
to shared data.
- id: The philosopher's identifier.
- thread_id: The identifier for the philosopher's thread.
- odd: A flag indicating if a philo's ID is odd or not.
- thread_id: The identifier for the philosopher's thread.
- meals_eaten: Counter for the number of meals the philosopher has eaten.
- done_eating: Indicates whether the philosopher has finished eating.
- is_alive: A flag indicating whether the philosopher is still alive.
- t_last_meal: Timestamp of the philosopher's last meal in milliseconds.
- mtx_last_meal: Mutex for synchronizing access to `t_last_meal`.
- mtx_last_meal_init: Flag checking if `mtx_last_meal` has been initialized.
- timestamp_death: Timestamp in milliseconds for when the philosopher dies.
- left_fork: A pointer to the philosopher's left fork.
- right_fork: A pointer to the philosopher's right fork.
*/
Expand All @@ -92,6 +93,8 @@ typedef struct s_philo
pthread_t thread_id;
int meals_eaten;
t_ull t_last_meal;
t_mtx mtx_last_meal;
int mtx_last_meal_init;
t_ull timestamp_death;
t_fork *left_fork;
t_fork *right_fork;
Expand All @@ -102,7 +105,7 @@ Structure representing the overall simulation state:
- nr_philo: Total number of philosophers participating in the simulation.
- t_die: Time in milliseconds after which a philosopher dies if they
haven't start eating.
- t_eat: Time in ms a philosopher takes to eat.
- t_eat: Time in milliseconds a philosopher takes to eat.
- t_sleep: Time in milliseconds a philosopher sleeps after eating.
- t_think: Philosophers are altruistic in this simulation, meaning they delay
taking forks and eating for as long as possible without starving.
Expand All @@ -111,31 +114,42 @@ Structure representing the overall simulation state:
t_think = t_die - t_eat - t_sleep.
- max_meals: Max. number of meals a philosopher can eat before they stop
dining; `-1` means unlimited meals.
- full_philos: The number of philosophers who have eaten their maximum number
of meals (if specified) and have stopped eating.
- philo_dead: Flag indicating if a philosopher has died.
- t_start_sim: Timestamp for when the simulation started.
- t_start_sim: Timestamp in milliseconds for when the simulation started.
- forks: Array of forks available for the philosophers.
- philos: Array of philosophers participating in the simulation.
- monitor: Thread handle for the monitoring thread that observes the
state of the simulation.
- mtx_print: Mutex for synchronizing output to the console (printing).
- mtx_print_flagt: Flag checking if 'print' mutex has been initialized.
- mtx_print_init: Flag checking if 'print' mutex has been initialized.
- mtx_full_philos: Mutex for checking / increasing 'full_philos'.
- mtx_full_philos_init: Flag checking if 'full_philos' mutex has been
initialized.
- mtx_philo_dead: Mutex for synchronizing access to the `philo_dead` flag.
- mtx_philo_dead_flag: Flag checking if 'philo dead' mutex has been initialized.
- mtx_philo_dead_init: Flag checking if 'philo dead' mutex has been initialized.
*/
typedef struct s_sim
{
int nr_philo;
int t_die;
int t_eat;
int t_sleep;
int t_think;
int max_meals;
int philo_dead;
t_ull t_start_sim;
t_fork *forks;
t_philo *philos;
t_mtx mtx_print;
int mtx_print_flag;
t_mtx mtx_philo_dead;
int mtx_philo_dead_flag;
int nr_philo;
int t_die;
int t_eat;
int t_sleep;
int t_think;
int max_meals;
int full_philos;
int philo_dead;
t_ull t_start_sim;
t_fork *forks;
t_philo *philos;
pthread_t monitor;
t_mtx mtx_print;
int mtx_print_init;
t_mtx mtx_full_philos;
int mtx_full_philos_init;
t_mtx mtx_philo_dead;
int mtx_philo_dead_init;
} t_sim;

#endif
6 changes: 3 additions & 3 deletions philo/src/1_check_args.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
/* By: aschenk <aschenk@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/10 16:27:51 by aschenk #+# #+# */
/* Updated: 2024/10/08 10:32:44 by aschenk ### ########.fr */
/* Updated: 2024/10/11 22:07:13 by aschenk ### ########.fr */
/* */
/* ************************************************************************** */

/**
This file contains functions for validating command-line arguments passed
to the dining philosopher simulation.
Validating command-line arguments passed to the dining philosopher simulation.
If invalid arguments are found, the program prints an appropriate error message
and usage instructions.
Expand All @@ -23,6 +22,7 @@ and usage instructions.
// IN FILE:

int check_args(int argc, char **argv);
int simulate_lonely_philo(t_sim *sim);

/**
Converts a string to an integer and validates the input.
Expand Down
85 changes: 85 additions & 0 deletions philo/src/2_init_philos.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 2_init_philos.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aschenk <aschenk@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/11 17:43:15 by aschenk #+# #+# */
/* Updated: 2024/10/11 18:20:11 by aschenk ### ########.fr */
/* */
/* ************************************************************************** */

/**
Initialization of philosopher structures.
*/

#include "philo.h"

// IN FILE:

int init_philos(t_sim *sim);

/**
Initializes a philosopher's properties based on its index.
@param philo Pointer to the philosopher to initialize.
@param sim Pointer to the simulation structure.
@param index The index of the philosopher being initialized.
@return `0` on success;
`1` if there is an error initializing the mutex.
*/
static int initialize_philosopher(t_philo *philo, t_sim *sim, int index,
int nr_philo)
{
philo->sim = sim;
philo->id = index + 1;
philo->meals_eaten = 0;
philo->left_fork = &sim->forks[index];
philo->right_fork = &sim->forks[(index + 1) % nr_philo];
philo->odd = (philo->id % 2 == 1);
philo->mtx_last_meal_init = 0;
if (mtx_action(&philo->mtx_last_meal, INIT))
return (1);
philo->mtx_last_meal_init = 1;
return (0);
}

/**
Initializes the philosophers participating in the simulation by allocating
memory for their structs and setting their initial state, including the
assignment of forks.
@param sim Pointer to the simulation structure containing philosopher data.
@return `0` on success;
`1` if memory allocation or mutex initialization fails.
*/
int init_philos(t_sim *sim)
{
int nr_philo;
int i;

nr_philo = sim->nr_philo;
sim->philos = malloc(sizeof(t_philo) * nr_philo);
if (!sim->philos)
{
print_err_msg(ERR_MALLOC);
return (1);
}
i = 0;
while (i < nr_philo)
{
if (initialize_philosopher(&sim->philos[i], sim, i, nr_philo))
{
while (i > 0)
mtx_action(&sim->philos[--i].mtx_last_meal, DESTROY);
free(sim->philos);
sim->philos = NULL;
return (1);
}
i++;
}
return (0);
}
Loading

0 comments on commit d3b07ff

Please sign in to comment.