philo.h, organized by category.
Initialization Functions
Functions responsible for setting up the simulation environment.init_data
init_data
table- Pointer to the table structure to initializephilos- Pointer to the array of philosopher structures
- Sets
dead_flagto 0 (simulation running) - Assigns the philos array to the table
- Initializes three mutexes:
lock_write,lock_dead, andlock_meal
init_data.c:66-73init_philos
init_philos
philos- Array of philosopher structures to initializetable- Pointer to the main table structureforks- Array of fork mutexesargv- Command-line arguments containing simulation parameters
- Sets unique ID for each philosopher (1-indexed)
- Initializes meal counter and eating status
- Sets timing parameters from command-line arguments
- Records start time and last meal time
- Assigns mutex pointers (write, dead, meal locks)
- Assigns left and right fork mutexes (philosopher 0 gets special fork assignment)
init_data.c:27-52init_forks
init_forks
forks- Array of mutex structures representing forksphilo_num- Number of philosophers (and forks) to initialize
init_data.c:54-64Threading Functions
Functions that manage thread creation, execution, and synchronization.ft_pthread_create
ft_pthread_create
table- Pointer to the table structure containing philosophersforks- Array of fork mutexes
- Creates the monitor thread first
- Creates a thread for each philosopher
- Waits for the monitor thread to complete
- Waits for all philosopher threads to complete
- Calls
destroy_allon any thread creation or join error
create_threads.c:40-65philo_routine
philo_routine
pointer- Void pointer to the philosopher structure (cast tot_philo*)
- Even-numbered philosophers start with a 1ms delay to prevent deadlock
- Loops continuously until
dead_flagis set - Executes the cycle: eat → sleep → think
- Checks the dead flag before each cycle iteration
create_threads.c:24-38monitor
monitor
pointer- Void pointer to the philos array (cast tot_philo*)
- Continuously checks if any philosopher has died
- Continuously checks if all philosophers have eaten enough
- Breaks the loop when either condition is met
- Sets the
dead_flagwhen termination condition is detected
monitor.c:83-92Philosopher Action Functions
Functions that implement the core philosopher behaviors.ft_eat
ft_eat
philo- Pointer to the philosopher structure
- Locks right fork first
- Prints “has taken a fork” message
- Special case: if only one philosopher, waits for death and returns
- Locks left fork
- Sets
eatingflag to 1 - Prints “is eating” message
- Increments meal counter (protected by
lock_meal) - Updates
last_meal_time - Sleeps for
time_to_eatduration - Sets
eatingflag to 0 - Unlocks both forks
philo_actions.c:26-48ft_sleep
ft_sleep
philo- Pointer to the philosopher structure
- Prints “is sleeping” message
- Sleeps for
time_to_sleepduration usingft_usleep
philo_actions.c:20-24ft_think
ft_think
philo- Pointer to the philosopher structure
- Prints “is thinking” message
- No delay (philosophers think instantaneously)
philo_actions.c:15-18Utility Functions
Helper functions for parsing, time management, and error handling.parse_input
parse_input
argv- Array of command-line argument strings
- Arguments must be positive integers
- Arguments must not exceed 9 digits
- Number of philosophers must not exceed
PHILO_MAX(600) - Zero values are rejected
- Validates both required arguments (1-4) and optional argument (5)
error_exit if validation failsSource: parse_input.c:30-57get_current_time
get_current_time
- Uses
gettimeofday()system call - Converts seconds to milliseconds and adds microseconds converted to milliseconds
- Formula:
(tv_sec * 1000) + (tv_usec / 1000)
time_utils.c:25-32ft_usleep
ft_usleep
usleep.Parameters:milliseconds- Duration to sleep in milliseconds
- Records start time
- Loops with 500 microsecond sleeps until target duration is reached
- More accurate than single long sleep due to frequent time checks
time_utils.c:15-23ft_atoi
ft_atoi
str- String containing the number to convert
- Skips whitespace and control characters
- Handles ’+’ and ’-’ signs
- Parses digits until non-digit character or end of string
utils.c:21-44ft_strlen
ft_strlen
str- String to measure (can be NULL)
utils.c:66-76error_exit
error_exit
error- Error message string to display
- Prints the error message to stdout
- Exits with
EXIT_FAILUREstatus - Does not return
utils.c:15-19destroy_all
destroy_all
str- Optional error message to print (can be NULL)table- Pointer to the table structureforks- Array of fork mutexes
- Writes error message to stderr if provided
- Destroys all table mutexes (write, meal, dead locks)
- Destroys all fork mutexes
utils.c:46-64Monitoring Functions
Functions used by the monitor thread to check simulation state.dead_loop
dead_loop
philo- Pointer to the philosopher structure
- Locks the
lock_deadmutex - Checks the
deadflag - Unlocks the mutex
- Returns the flag value
create_threads.c:15-22print_message
print_message
str- Action message to printphilo- Pointer to the philosopher structureid- Philosopher ID
<timestamp_ms> <philosopher_id> <message>Behavior:- Locks the
lock_writemutex to prevent interleaved output - Calculates elapsed time since simulation start
- Only prints if no philosopher has died
- Unlocks the mutex after printing
monitor.c:15-24Internal Helper Functions
These functions are used internally by the monitor thread and are not declared in the public header.philosopher_dead
philosopher_dead
philo- Pointer to the philosopher to checktime_to_die- Maximum time without eating before death
- Current time - last meal time ≥ time_to_die
- AND philosopher is not currently eating
monitor.c:26-34check_if_dead
check_if_dead
philos- Array of all philosophers
- Iterates through all philosophers
- Checks each one using
philosopher_dead - If death detected, prints death message and sets
dead_flagto 1
monitor.c:36-54check_if_all_ate
check_if_all_ate
philos- Array of all philosophers
- Returns 0 immediately if
max_mealis -1 (no meal limit) - Counts philosophers who reached
max_meal - If all philosophers finished, sets
dead_flagto 1
monitor.c:56-81init_input
init_input
philo- Pointer to the philosopher structureargv- Command-line arguments
nbr_philofrom argv[1]time_to_diefrom argv[2]time_to_eatfrom argv[3]time_to_sleepfrom argv[4]max_mealfrom argv[5] (or -1 if not provided)
init_data.c:15-25