Skip to content

Philosophers Dining Problem with threads / semaphores

Notifications You must be signed in to change notification settings

hanmpark/philosophers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

70 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Philosophers

An introduction to threads, processes, and concurrency in C.


Repository created at Repository code size Mostly used language

๐Ÿ“– Description

The Philosophers project explores multithreading, synchronization, and concurrent programming in C. It is based on the Dining Philosophers Problem, a classical concurrency challenge where philosophers alternate between eating, sleeping, and thinking. The goal is to implement a solution that avoids deadlocks and race conditions, ensuring all philosophers can eat without blocking each other.

๐Ÿ› ๏ธ Features

  • Two implementations:
    • Mandatory: threads + mutexes
    • Bonus: processes + semaphores
  • Correct handling of:
    • Shared memory
    • Resource allocation
    • Synchronization problems (deadlocks, race conditions)
  • Simulation of philosophersโ€™ life cycle: eating, sleeping, thinking

๐Ÿงฉ Code Structure

./philo (threads + mutexes)

philo structure

./philo_bonus (processes + semaphores)

philo_bonus structure

๐Ÿง  Key Concepts

Threads

A thread is an independent sequence of execution within a process.
Threads share the same memory and resources, making them lightweight but requiring careful synchronization.

Race Conditions ๐ŸŽ

A race condition occurs when multiple threads/processes access or modify shared data simultaneously, leading to unpredictable outcomes.
Mutexes (locks) can prevent simultaneous access and ensure safe operations.

Deadlocks ๐Ÿ”’

A deadlock happens when two or more threads wait indefinitely for resources locked by each other.
Proper resource ordering and synchronization design prevent deadlocks.

๐Ÿ“ฆ Installation

Clone the repository and compile:

git clone https://github.com/hanmpark/philosophers.git
cd philosophers
make

๐Ÿš€ Usage

Mandatory (threads + mutexes)

./philo number_of_philosophers time_to_die time_to_eat time_to_slee [number_of_times_each_philosopher_must_eat]

Bonus (processes + semaphores)

./philo_bonus number_of_philosophers time_to_die time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]

๐Ÿ‘ค Authors

About

Philosophers Dining Problem with threads / semaphores

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published