Skip to content

C implementation of the Dining Philosophers problem using threads, mutexes, and semaphores. Demonstrates synchronization, deadlock avoidance, and process/thread management for 42 school.

Notifications You must be signed in to change notification settings

artidek/42_philosophers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

28 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧠 Philosophers - 42 Project

Welcome to the Dining Philosophers simulation β€” a classic concurrency problem solved using POSIX threads, mutexes, and optionally processes and semaphores for bonus points.

This project focuses on multithreaded and multiprocess synchronization, avoiding deadlocks, and ensuring thread-safe access to shared resources (forks). Implemented in pure C without any global variables.


πŸ“ Project Structure

philo/
β”œβ”€β”€ Makefile
β”œβ”€β”€ philo.h
β”œβ”€β”€ *.c

philo_bonus/
β”œβ”€β”€ Makefile
β”œβ”€β”€ philo_bonus.h
β”œβ”€β”€ *.c

πŸš€ Getting Started

βœ… Mandatory Part (philo/)

  • Each philosopher is a thread.
  • Forks are protected with mutexes to ensure exclusive access.
make -C philo
./philo number_of_philosophers time_to_die time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]

βœ… Bonus Part (philo_bonus/)

  • Each philosopher is a process.
  • Fork access is managed via POSIX semaphores.
make -C philo_bonus
./philo_bonus number_of_philosophers time_to_die time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]

🧠 Problem Overview

  • Philosophers are seated at a round table with a fork between each pair.
  • To eat, a philosopher needs two forks β€” one on the left and one on the right.
  • Philosophers alternate between:
    • 🧠 Thinking
    • 🍝 Eating
    • 😴 Sleeping
  • If a philosopher does not eat within time_to_die milliseconds, they die.
  • The simulation ends when:
    • A philosopher dies of starvation, or
    • (Optional) All philosophers have eaten at least number_of_times_each_philosopher_must_eat times.

πŸ›  Allowed Functions

Mandatory Part

  • pthread_create
  • pthread_join
  • pthread_detach
  • pthread_mutex_init
  • pthread_mutex_destroy
  • pthread_mutex_lock
  • pthread_mutex_unlock
  • gettimeofday
  • usleep
  • write
  • malloc
  • free
  • printf

πŸ“ Logging Format

Output is timestamped and thread-safe:

timestamp_in_ms X has taken a fork
timestamp_in_ms X is eating
timestamp_in_ms X is sleeping
timestamp_in_ms X is thinking
timestamp_in_ms X died
  • X is the philosopher number (starting from 1).
  • timestamp_in_ms is the time elapsed since the simulation start in milliseconds.
  • The message for a philosopher's death must be printed within 10 ms of their actual time of death.

πŸ’‘ Key Concepts & Requirements

  • 🧡 Threads (mandatory): One thread per philosopher, with one mutex per fork.
  • πŸ§ͺ Race Conditions: Must be avoided through proper mutex locking.
  • πŸ’€ Starvation: Philosophers must not starve (unless it ends the simulation).
  • πŸ”’ Thread-Safety: Logs and shared data must be synchronized.
  • βš™οΈ Semaphores (bonus): A single semaphore manages all forks.
  • πŸ“¦ No global variables allowed.

βœ… Makefile Targets

  • make / make all β€” Compile the program.
  • make clean β€” Remove object (.o) files.
  • make fclean β€” Remove all binaries and object files.
  • make re β€” Clean and rebuild the project.

πŸ“š Resources


πŸ”’ License

This project is part of the 42 Curriculum and is provided for educational purposes only. Please do not plagiarize.


✨ Author

Built by a 42 student A. Obshatko.

About

C implementation of the Dining Philosophers problem using threads, mutexes, and semaphores. Demonstrates synchronization, deadlock avoidance, and process/thread management for 42 school.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published