Skip to content

mtelek/philosophers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Philosophers

Build Status Code Size Project Score

A visualization of the Dining Philosophers problem.

📖 Project Description

This project solves the Dining Philosophers problem, a classic synchronization challenge where multiple philosophers (threads) share forks (mutexes) to eat, think, and sleep without deadlocks or starvation. The goal is to ensure threads safely access shared resources while logging their states in real-time.
For background on this classic CS problem: Dining Philosophers Problem

Key Features:

  • Thread-based simulation: Each philosopher is a thread, and forks are protected by mutexes.
  • Starvation prevention: Philosophers must eat within time_to_die or the program terminates.
  • Real-time logging: Timestamped output for all actions (eating, sleeping, thinking, etc.).
  • Configurable parameters: Adjust philosopher count, timings, and optional meal limits.

🛠 Installation

Prerequisites:

  • C Compiler (cc/gcc/clang)
  • Make (GNU Make)
  • Unix-based OS (Linux/macOS)

Steps:

Clone the repository:

git clone git@github.com:mtelek/philosophers.git
cd philosophers/philo
make

🚀 Usage

 ./philo <philos> <die> <eat> <sleep> [<meals>]

Parameters

Parameter Description Example Value
philos Number of philosophers 4
die Time until starvation 800
eat Time spent eating 200
sleep Time spent sleeping 200
meals Optional meal count to stop simulation 5

🌈 Visualizing the Simulation

The philosopher states can be visualized using this interactive tool created by nafuka11:
Philosophers Visualizer

How to Use:

  1. Run your program and redirect the output to a file:
    ./philo 4 800 200 200 > output.txt
  2. Paste your output.txt contents into the visualizer's input field

🧪 Automated Testing

For comprehensive testing, you can use the LazyPhilosophersTester created by MichelleJiam:

Test ran on the program:

image

About

Philosophers is a 42 School project that implements the dining philosophers problem using threads and mutexes in C, focusing on synchronization to prevent deadlocks and data races.

Topics

Resources

Stars

Watchers

Forks

Contributors