GithubHelp home page GithubHelp logo

whymami / philosophers Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 1.0 55 KB

In the 42 philosophers project, the goal is to develop a synchronization mechanism that enables a group of philosophers to share limited resources while avoiding issues like deadlock.

Makefile 2.41% C 97.59%

philosophers's Introduction

Dining Philosophers

Dining Philosophers Problem

The Philosophers Problem is a famous problem in computer science used to illustrate common problems in concurrent programming. It simulates a scenario in which a number of philosophers sit at a round table and eat, sleep and think respectively.

Rules

  • Philosophers can be in one of three states: eating, thinking, or sleeping.
  • Philosophers must eat with two forks, one for each hand.
  • Philosophers must never be starving, and every philosopher needs to eat.
  • Philosophers don't speak with each other and don't know when another philosopher is about to die.
  • The simulation stops when a philosopher dies.

Program Arguments

Each program must have the following options:

  • number_of_philosophers: The number of philosophers is also the number of forks.
  • time_to_die: Time in milliseconds. If a philosopher does not start eating within time_to_die milliseconds after starting his last meal or after the start of the simulation, he dies.
  • 'time_to_eat': Time in milliseconds. The time it takes for a philosopher to eat.
  • time_to_sleep: Time in milliseconds. The time it takes for a philosopher to sleep.
  • [number_of_times_each_philosopher_must_eat] (optional): If specified, the simulation will stop after all philosophers have eaten at least this many times. If not specified, the simulation will only stop on the death of a philosopher.

Things to Know About the Project

Mutex

A mutex is a synchronization policy used in concurrent programming to prevent multiple threads from simultaneously accessing shared resources (e.g. variables or data structures) that can lead to race conditions. In this project, mutexes are used to ensure that only one philosopher can access a fork at a time and to avoid conflicts.

Threads

A thread is a unit of execution within a process. Unlike processes, which have their own memory space, threads share the same memory space and resources with their parent process. This makes threads lighter than processes and allows them to communicate more efficiently. In this project, multiple threads are used to simulate philosophers.

The Difference Between Threads and Processes

Threads:

Share the same memory space. Lighter than processes. Faster to create and manage. They have less overhead for communication between them.

Processes:

Have their own memory space. They are heavier and more resource intensive. They have more overhead for communication between them.

Data Race

A data race occurs in concurrent programming when two or more threads or processes simultaneously access a shared resource (such as a variable) and at least one of them writes to it. This can lead to unpredictable behavior and errors. The use of mutexes in this project helps prevent data races by providing exclusive access to shared resources such as forks.

Usage

Clone the project

git clone https://github.com/whymami/philosophers.git

Compile the program

cd philosophers
cd philo
make

Run the program

./philo number_of_philosophers time_to_die time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]

philosophers's People

Contributors

whymami avatar aleynakoluk avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

aleynakoluk

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.