Skip to content
0x0alikacimiPublic

About

An implementation of the Dining Philosophers concurrency problem in C using threads and mutexes to manage synchronization and shared resources.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

17 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

philo-42

Dining Philosophers simulation in C — a small concurrent program that demonstrates thread synchronization, timing, and resource-sharing constraints. This project follows the typical 42-school-style requirements.

Overview

The Dining Philosophers problem models N philosophers sitting around a circular table. Each philosopher alternates between thinking and eating. Between every pair of philosophers sits a single fork. To eat, a philosopher needs to pick up both the left and the right fork (two shared resources). The challenge is to coordinate access to forks so the system avoids deadlock and starvation while respecting timing constraints.

Goals

  • Implement a correct, thread-based simulation using POSIX threads (pthread).
  • Avoid deadlock and minimize starvation.
  • Follow the specified time semantics (millisecond precision) for actions.

Concepts and design

  • Philosophers: represented by threads. Each thread cycles through thinking, trying to pick up forks, eating, and sleeping.
  • Forks: represented by mutexes (one mutex per fork). A philosopher must lock the two mutexes corresponding to their left and right forks before eating.
  • Monitor: a separate thread (or checking mechanism) verifies the timing constraints (e.g., whether a philosopher has died from starvation).
  • Timing: time_to_die, time_to_eat, and time_to_sleep are interpreted in milliseconds. Use a reliable gettimeofday or clock_gettime wrapper to measure elapsed time.

Common strategies implemented in this project

  • Ordered locking: to reduce the chance of classical deadlock, philosophers may pick up the lower-numbered fork first (or use an asymmetric policy where odd philosophers pick left first, even pick right first).
  • Single monitor thread: a central monitor periodically checks last-meal times and signals termination if a philosopher dies.
  • Optional meal limit: when the optional number_of_times_each_philosopher_must_eat is provided, the simulation ends when all philosophers have eaten that many times.

Requirements & constraints

  • The program uses pthreads and mutexes; compile with -pthread (the Makefile provided handles that).
  • Timing must be accurate to milliseconds.
  • The simulation must gracefully terminate when a philosopher dies or when the optional meal count is reached.

Build

Run:

make

This produces the philo executable.

Usage

./philo number_of_philosophers time_to_die time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]
  • number_of_philosophers — integer > 0
  • time_to_die, time_to_eat, time_to_sleep — milliseconds
  • optional number_of_times_each_philosopher_must_eat — integer > 0

Examples:

./philo 5 800 200 200
./philo 4 410 200 200 7

Implementation details (notes for maintainers)

  • main.c: argument parsing and initialization.
  • philo.h: shared types, structs for philosophers, and function prototypes.
  • simulation.c: philosopher thread routines and main loop.
  • monitor.c: monitor thread that checks for death conditions and termination.
  • tools1.c, tools2.c, tools3.c, gc_tools.c: utility functions including timing helpers, printing/logging, and any simple garbage-collection helpers.

When modifying synchronization logic, prefer small, well-tested changes. Adding extra locks or changing the order of locking can introduce subtle deadlocks.

Testing tips

  • Start with small numbers of philosophers and larger times to observe behavior.
  • Use scenarios that commonly reveal deadlock: many philosophers with small time_to_eat and time_to_sleep values.
  • Add debug prints in tools*.c to log when a mutex is locked/unlocked for deeper diagnosis (but remove or guard verbose logs for final submission).

Known limitations

  • Millisecond timing depends on system scheduling and usleep granularity; on heavy-load systems timing may be coarser.
  • This implementation uses mutexes; alternative implementations may use semaphores or other synchronization primitives.

Files

  • main.c — program entry
  • philo.h — headers and types
  • simulation.c, monitor.c — core logic and monitoring
  • tools1.c, tools2.c, tools3.c, gc_tools.c — helpers
  • Makefile — build rules

Notes

  • Requires pthreads (POSIX threads).
  • Tested on Linux.

If you'd like, I can also run make here and report the build output or add short examples of debug commands to help reproduce issues.

About

An implementation of the Dining Philosophers concurrency problem in C using threads and mutexes to manage synchronization and shared resources.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages