Skip to content

nessrinemaalem/philosopher

Repository files navigation

philosopher

A solution to the classic Dining Philosophers problem, written in pure C using multithreading — part of the École 42 curriculum. Simulates N philosophers sitting at a table, alternating between thinking, eating, and sleeping, sharing forks with their neighbours without deadlocking or starving.

Features

  • Thread-per-philosopher simulation (pthread), with mutexes representing forks
  • Deadlock avoidance and fair fork acquisition order
  • Death detection: a philosopher who doesn't eat within the time limit is correctly detected and reported, with precise timestamps
  • Configurable via CLI arguments: number of philosophers, time to die, time to eat, time to sleep, optional max number of meals

Build & run

make
./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

Project structure

main.c                   argument parsing, setup
parsing.c / extract_arg.c   CLI argument validation
init_join.c               thread/mutex initialization and cleanup
routine_control.c / everyday_routine.c   the philosopher's think/eat/sleep loop
tread_management.c        thread lifecycle
time.c                    precise timing utilities

Constraints

Built with pthread and mutexes only — no semaphores in the mandatory part. The core challenge is avoiding both deadlock and race conditions while keeping timing precise (death must be detected within a few milliseconds of the deadline).

About

sloved the diying philosopher problem with a multi-thread program on top

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages