Skip to main content
This page documents all public functions from philo.h, organized by category.

Initialization Functions

Functions responsible for setting up the simulation environment.
void init_data(t_table *table, t_philo *philos);
Initializes the main table structure with default values and mutexes.Parameters:
  • table - Pointer to the table structure to initialize
  • philos - Pointer to the array of philosopher structures
Behavior:
  • Sets dead_flag to 0 (simulation running)
  • Assigns the philos array to the table
  • Initializes three mutexes: lock_write, lock_dead, and lock_meal
Source: init_data.c:66-73
void init_philos(t_philo *philos, t_table *table, t_mtx *forks, char **argv);
Initializes each philosopher with their individual properties and mutex references.Parameters:
  • philos - Array of philosopher structures to initialize
  • table - Pointer to the main table structure
  • forks - Array of fork mutexes
  • argv - Command-line arguments containing simulation parameters
Behavior:
  • 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)
Source: init_data.c:27-52
void init_forks(t_mtx *forks, int philo_num);
Initializes all fork mutexes for the simulation.Parameters:
  • forks - Array of mutex structures representing forks
  • philo_num - Number of philosophers (and forks) to initialize
Return value: None (void)Source: init_data.c:54-64

Threading Functions

Functions that manage thread creation, execution, and synchronization.
int ft_pthread_create(t_table *table, t_mtx *forks);
Creates all philosopher threads and the monitor thread, then waits for them to complete.Parameters:
  • table - Pointer to the table structure containing philosophers
  • forks - Array of fork mutexes
Return value: Returns 0 on successBehavior:
  1. Creates the monitor thread first
  2. Creates a thread for each philosopher
  3. Waits for the monitor thread to complete
  4. Waits for all philosopher threads to complete
  5. Calls destroy_all on any thread creation or join error
Source: create_threads.c:40-65
void *philo_routine(void *pointer);
The main routine executed by each philosopher thread.Parameters:
  • pointer - Void pointer to the philosopher structure (cast to t_philo*)
Return value: Returns the input pointerBehavior:
  • Even-numbered philosophers start with a 1ms delay to prevent deadlock
  • Loops continuously until dead_flag is set
  • Executes the cycle: eat → sleep → think
  • Checks the dead flag before each cycle iteration
Source: create_threads.c:24-38
void *monitor(void *pointer);
Monitor thread that checks for simulation termination conditions.Parameters:
  • pointer - Void pointer to the philos array (cast to t_philo*)
Return value: Returns the input pointerBehavior:
  • 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_flag when termination condition is detected
Source: monitor.c:83-92

Philosopher Action Functions

Functions that implement the core philosopher behaviors.
void ft_eat(t_philo *philo);
Handles the eating action for a philosopher.Parameters:
  • philo - Pointer to the philosopher structure
Behavior:
  1. Locks right fork first
  2. Prints “has taken a fork” message
  3. Special case: if only one philosopher, waits for death and returns
  4. Locks left fork
  5. Sets eating flag to 1
  6. Prints “is eating” message
  7. Increments meal counter (protected by lock_meal)
  8. Updates last_meal_time
  9. Sleeps for time_to_eat duration
  10. Sets eating flag to 0
  11. Unlocks both forks
Source: philo_actions.c:26-48
void ft_sleep(t_philo *philo);
Handles the sleeping action for a philosopher.Parameters:
  • philo - Pointer to the philosopher structure
Behavior:
  • Prints “is sleeping” message
  • Sleeps for time_to_sleep duration using ft_usleep
Source: philo_actions.c:20-24
void ft_think(t_philo *philo);
Handles the thinking action for a philosopher.Parameters:
  • philo - Pointer to the philosopher structure
Behavior:
  • Prints “is thinking” message
  • No delay (philosophers think instantaneously)
Source: philo_actions.c:15-18

Utility Functions

Helper functions for parsing, time management, and error handling.
void parse_input(char **argv);
Validates command-line arguments before simulation begins.Parameters:
  • argv - Array of command-line argument strings
Validation rules:
  • 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)
Calls error_exit if validation failsSource: parse_input.c:30-57
size_t get_current_time(void);
Returns the current time in milliseconds.Return value: Current timestamp in milliseconds since epochImplementation:
  • Uses gettimeofday() system call
  • Converts seconds to milliseconds and adds microseconds converted to milliseconds
  • Formula: (tv_sec * 1000) + (tv_usec / 1000)
Source: time_utils.c:25-32
int ft_usleep(size_t milliseconds);
Custom sleep function with more precise timing than standard usleep.Parameters:
  • milliseconds - Duration to sleep in milliseconds
Return value: Returns 0Implementation:
  • Records start time
  • Loops with 500 microsecond sleeps until target duration is reached
  • More accurate than single long sleep due to frequent time checks
Source: time_utils.c:15-23
int ft_atoi(char *str);
Converts a string to an integer.Parameters:
  • str - String containing the number to convert
Return value: The integer value represented by the stringBehavior:
  • Skips whitespace and control characters
  • Handles ’+’ and ’-’ signs
  • Parses digits until non-digit character or end of string
Source: utils.c:21-44
int ft_strlen(char *str);
Calculates the length of a string.Parameters:
  • str - String to measure (can be NULL)
Return value: Number of characters in the string, or 0 if NULLSource: utils.c:66-76
void error_exit(char *error);
Prints an error message and exits the program.Parameters:
  • error - Error message string to display
Behavior:
  • Prints the error message to stdout
  • Exits with EXIT_FAILURE status
  • Does not return
Source: utils.c:15-19
void destroy_all(char *str, t_table *table, t_mtx *forks);
Cleans up all resources and destroys mutexes.Parameters:
  • str - Optional error message to print (can be NULL)
  • table - Pointer to the table structure
  • forks - Array of fork mutexes
Behavior:
  • Writes error message to stderr if provided
  • Destroys all table mutexes (write, meal, dead locks)
  • Destroys all fork mutexes
Source: utils.c:46-64

Monitoring Functions

Functions used by the monitor thread to check simulation state.
int dead_loop(t_philo *philo);
Thread-safe check for the dead flag.Parameters:
  • philo - Pointer to the philosopher structure
Return value: Returns 1 if a philosopher has died or simulation ended, 0 otherwiseBehavior:
  • Locks the lock_dead mutex
  • Checks the dead flag
  • Unlocks the mutex
  • Returns the flag value
Source: create_threads.c:15-22

Internal Helper Functions

These functions are used internally by the monitor thread and are not declared in the public header.
int philosopher_dead(t_philo *philo, size_t time_to_die);
Checks if a specific philosopher has died from starvation.Parameters:
  • philo - Pointer to the philosopher to check
  • time_to_die - Maximum time without eating before death
Return value: Returns 1 if philosopher is dead, 0 otherwiseDeath condition:
  • Current time - last meal time ≥ time_to_die
  • AND philosopher is not currently eating
Source: monitor.c:26-34
int check_if_dead(t_philo *philos);
Checks all philosophers for death and sets the dead flag if any died.Parameters:
  • philos - Array of all philosophers
Return value: Returns 1 if any philosopher died, 0 otherwiseBehavior:
  • Iterates through all philosophers
  • Checks each one using philosopher_dead
  • If death detected, prints death message and sets dead_flag to 1
Source: monitor.c:36-54
int check_if_all_ate(t_philo *philos);
Checks if all philosophers have eaten the required number of meals.Parameters:
  • philos - Array of all philosophers
Return value: Returns 1 if all philosophers finished eating, 0 otherwiseBehavior:
  • Returns 0 immediately if max_meal is -1 (no meal limit)
  • Counts philosophers who reached max_meal
  • If all philosophers finished, sets dead_flag to 1
Source: monitor.c:56-81
void init_input(t_philo *philo, char **argv);
Parses and assigns command-line arguments to a philosopher structure.Parameters:
  • philo - Pointer to the philosopher structure
  • argv - Command-line arguments
Assigned values:
  • nbr_philo from argv[1]
  • time_to_die from argv[2]
  • time_to_eat from argv[3]
  • time_to_sleep from argv[4]
  • max_meal from argv[5] (or -1 if not provided)
Source: init_data.c:15-25

Data Structures

t_philo

typedef struct s_philo
{
    pthread_t   thread;           // Philosopher's thread handle
    int         id;               // Philosopher ID (1-indexed)
    int         eating;           // 1 if currently eating, 0 otherwise
    int         meal_counter;     // Number of meals eaten
    int         max_meal;         // Required meals (-1 for unlimited)
    long        last_meal_time;   // Timestamp of last meal start
    long        start_time;       // Simulation start timestamp
    long        nbr_philo;        // Total number of philosophers
    long        time_to_die;      // Max milliseconds without eating
    long        time_to_eat;      // Eating duration in milliseconds
    long        time_to_sleep;    // Sleeping duration in milliseconds
    int         *dead;            // Pointer to shared dead flag
    t_mtx       *lock_dead;       // Mutex for dead flag access
    t_mtx       *lock_write;      // Mutex for console output
    t_mtx       *lock_meal;       // Mutex for meal counter
    t_mtx       *left_fork;       // Left fork mutex
    t_mtx       *right_fork;      // Right fork mutex
} t_philo;

t_table

typedef struct s_table
{
    int     dead_flag;      // 0=running, 1=simulation ended
    t_mtx   lock_dead;      // Protects dead_flag
    t_mtx   lock_write;     // Protects stdout
    t_mtx   lock_meal;      // Protects meal counters
    t_philo *philos;        // Array of philosophers
} t_table;

Build docs developers (and LLMs) love