Giter Site home page Giter Site logo

akamlah / 42-philosophers Goto Github PK

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

Didactic project from Ecole-42. Two different solutions for an extended version of the famous 'Dining Philosophers' problem, the first as a multithreaded program ruled by mutexes, the second featuring multiple processes synchronised by semaphores.

Makefile 20.00% C 80.00%
multithreading pthreads-api mutex-locks unix-process c synchronization mutexes semaphores

42-philosophers's Introduction

42-Philosophers

Didactic project from Ecole-42. Two different solutions for an extended version of the famous 'Dining Philosophers' problem, the first as a multithreaded program ruled by mutexes, the second featuring multiple processes synchronised by semaphores.

Building

Version 1 - posix threads

at the root of the folder "philo":

$ make
$ ./philo number_of_philosophers time_to_die time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]

The last argument is optional. The maximum number of philosophers the program is tested for is 200 (i.e. 200 concurrent threads)

Version 2 - unix processes

at the root of the folder "philo_bonus":

$ make
$ ./philo number_of_philosophers time_to_die time_to_eat time_to_sleep [number_of_times_each_philosopher_must_eat]

The last argument is optional. The maximum number of philosophers the program is tested for is 200 (i.e. 200 concurrent processes)

The simulation

One or more philosophers sit 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.

The user gets a log printed on the terminal that informs whenever a philosopher has taken one of the following actions:

  • taking a fork
  • started eating
  • started thining
  • started sleeping
  • died

Preceeded by a timestamp corresponding to the elapsed milliseconds from the start of the simulation. The main goal in this program was to optimize it in a way that as little delay as possible is accumulated, so that even for large numbers of threads/processes, if the parameters would ensure that the simulation runs infinitely (i.e. time to eat shorter that time to die for example), the implementation actually does run as long as possible, and is stopped only by system limitations. Also another key point is the management of shared memory resources, and avoiding race conditions between threads.

42-philosophers's People

Contributors

akamlah 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.