Skip to content

A simulation of the Dining Philosophers problem in C, demonstrating concurrency control using threads, mutexes, and timing. Handles philosophers’ actions (thinking, eating, sleeping) while preventing deadlocks and starvation, providing a precise, robust multithreaded solution.

Notifications You must be signed in to change notification settings

Felipp3san/42-philosophers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Philosophers

A multithreaded simulation of the Dining Philosophers Problem in C, built as part of the 42 School curriculum. This project explores concurrency, synchronization, and deadlock prevention using threads and mutexes.

Features

  • Simulates multiple philosophers sitting at a table
  • Philosophers can think, eat, and sleep in cycles
  • Uses mutexes to control access to forks
  • Prevents deadlocks and starvation
  • Time tracking for precise action simulation
  • Command-line arguments for number of philosophers, times, and cycles

Tech Stack

  • C – Core language
  • POSIX Threads (pthread) – For multithreading
  • Mutexes – For synchronization
  • Makefile – Build and manage project

Installation

  1. Clone the repository:
git clone https://github.com/Felipp3san/42-philosophers
cd 42-philosophers
  1. Compile the project:
make
  1. Run the executable with arguments:
./philo number_of_philosophers time_to_die time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]

Example:

./philo 5 800 200 200 7

Usage

  • Arguments:

    • number_of_philosophers – total philosophers at the table
    • time_to_die – max time (ms) before a philosopher dies without eating
    • time_to_eat – time (ms) a philosopher spends eating
    • time_to_sleep – time (ms) a philosopher spends sleeping
    • number_of_times_each_philosopher_must_eat (optional) – how many times each philosopher must eat
  • Observe philosopher actions: thinking, taking forks, eating, sleeping

  • Program terminates when all philosophers have eaten enough or a philosopher dies

Compilation

  • Build the executable:
make
  • Remove object files:
make clean
  • Remove all binaries and rebuild:
make re

Project Structure

.
├── include/           # Header files
├── src/               # Source files
└── build/             # Object files (created after compilation)

License

Educational project under 42 School academic policy.

About

A simulation of the Dining Philosophers problem in C, demonstrating concurrency control using threads, mutexes, and timing. Handles philosophers’ actions (thinking, eating, sleeping) while preventing deadlocks and starvation, providing a precise, robust multithreaded solution.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors