GithubHelp home page GithubHelp logo

karadnz / philosophers Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 67 KB

My solution to dining philosophers problem using threads, mutexes, processes and semaphores.

Makefile 3.35% C 96.65%
42-school 42philosophers 42projects dining-philosophers philosophers philosophers42

philosophers's Introduction

THE CODE IS NOT FINISHED

Dining philosophers problem

The Dining Philosopher Problem is a classic computer science problem that illustrates the challenges of synchronizing multiple threads or processes that compete for shared resources. The problem involves a group of philosophers sitting around a circular table, each with a plate of spaghetti and a fork. There are as many forks as there are philosophers, but philosophers needs two forks to be able to eat spaghetti, meaning that two adjacent philosophers must share theirs forks to eat. The goal is for each philosopher to eat and think without deadlocking or starving to death. [Add img]

Ecole 42 version of the problem

  • One or more philosophers sits at a round table. There is a large bowl of spaghetti in the middle of the table.
  • The philosophers alternatively eat, think, or sleep. While they are eating, they are not thinking nor sleeping; while thinking, they are not eating nor sleeping; and, of course, while sleeping, they are not eating nor thinking.
  • There are also forks on the table. There are as many forks as philosophers.
  • Because serving and eating spaghetti with only one fork is very inconvenient, a philosopher takes their right and their left forks to eat, one in each hand.
  • When a philosopher has finished eating, they put their forks back on the table and start sleeping. Once awake, they start thinking again. The simulation stops when a philosopher dies of starvation.
  • Every philosopher needs to eat and should never starve.
  • Philosophers don’t speak with each other.
  • Philosophers don’t know if another philosopher is about to die.
  • No need to say that philosophers should avoid dying!

You can check the subject file here.

Solution with threads and mutexes

To solve the Dining Philosopher Problem, we need to ensure that the philosophers can eat without deadlocking or starving. One way to solve the problem is to use mutexes and threads. A mutex is a type of lock that is used to ensure that only one thread can access a shared resource at a time.

To use mutexes to solve the Dining Philosopher Problem, we can represent each fork as a mutex and each philosopher as a struct that contains a thread id and last time of meal. When a philosopher wants to eat, they must first acquire the mutexes for both forks they need. If the philosopher cannot acquire both mutexes, they must wait until they can. Once a philosopher has both mutexes, they can eat. After eating, the philosopher releases the mutexes for the forks, allowing other philosophers to use them. There should be another thread that checks if a philosopher died or not using their last meal date. Don't forget to create a mutex to print the events too.

Solution with processes and semaphores

Useful stuff

The Youtube playlist that i created for the project:

philosophers's People

Contributors

karadnz avatar

Stargazers

 avatar

Watchers

 avatar

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.