Overview
The Philosophers project uses two primary data structures to manage the simulation:t_philo for individual philosopher state and t_table for shared resources and global state.
The t_philo Structure
Thet_philo struct represents an individual philosopher and contains all the data needed for their execution.
Field Documentation
Thread Management
Thread Management
| Field | Type | Description |
|---|---|---|
thread | pthread_t | The POSIX thread handle for this philosopher |
id | int | Unique identifier for the philosopher (1-indexed) |
State Tracking
State Tracking
| Field | Type | Description |
|---|---|---|
eating | int | Flag indicating if philosopher is currently eating (1) or not (0) |
meal_counter | int | Number of meals this philosopher has completed |
max_meal | int | Maximum meals required (-1 if unlimited) |
Timing Information
Timing Information
| Field | Type | Description |
|---|---|---|
last_meal_time | long | Timestamp (ms) of when the philosopher last started eating |
start_time | long | Timestamp (ms) when the simulation began |
time_to_die | long | Maximum time (ms) a philosopher can go without eating |
time_to_eat | long | Duration (ms) it takes to eat |
time_to_sleep | long | Duration (ms) the philosopher sleeps |
Shared Resources
Shared Resources
Each philosopher holds pointers to shared mutexes rather than owning them directly. This allows multiple philosophers to coordinate access to shared resources.
The t_table Structure
Thet_table struct manages global state and shared resources for the entire simulation.
Field Documentation
| Field | Type | Description |
|---|---|---|
dead_flag | int | Global flag: 0 means simulation running, 1 means terminated |
lock_dead | t_mtx | Mutex protecting access to dead_flag |
lock_write | t_mtx | Mutex serializing console output to prevent interleaving |
lock_meal | t_mtx | Mutex protecting meal counters and eating state |
philos | t_philo* | Array of all philosopher structures |
The
t_table owns the actual mutex objects, while individual t_philo structs hold pointers to these mutexes.Initialization
Table Initialization
Theinit_data function initializes the table structure and its mutexes:
Philosopher Initialization
Theinit_philos function initializes each philosopher with their unique state and shared resource pointers:
Fork Assignment Logic
- Regular Philosophers (id > 1)
- First Philosopher (id == 1)
This circular fork assignment ensures that each philosopher shares their right fork with the philosopher to their right, creating the classic dining philosophers problem topology.