Coder Social home page Coder Social logo

palzap / 42-philosophers Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 2.21 MB

Eat, Sleep, Spaghetti, repeat. This project is about learning how threads work by precisely timing a group of philosophers on when to pick up forks and eat spaghetti without dying from hunger.

Makefile 7.62% C 92.38%

42-philosophers's Introduction

Philosophers

Overview

The problem

  • The problem involves a group of philosophers sitting at a round table.
  • There is a large bowl of spaghetti in the middle of the table, and there are as many forks as philosophers.
  • Philosophers alternate between eating, thinking, and sleeping. While eating, they cannot think or sleep; while thinking, they cannot eat or sleep; and while sleeping, they cannot eat or think.
  • Philosophers take their right and left forks to eat, one in each hand.
  • When a philosopher finishes eating, they put their forks back on the table and start sleeping. Once they wake up, they start thinking again.
  • The simulation ends when a philosopher dies of starvation.
  • Each philosopher needs to eat and should never starve.
  • Philosophers cannot communicate with each other.
  • Philosophers are not aware if another philosopher is about to die.

The rules

  • You need to write two programs: one for the mandatory part and another for the bonus part (if you decide to do it)
  • Global variables are forbidden
  • Both programs must take the following arguments:
    • number_of_philosophers: the number of philosophers and forks
    • time_to_die (in milliseconds): if a philosopher doesn't start eating within this time since their last meal or the start of the simulation, they die
    • time_to_eat (in milliseconds): the time it takes for a philosopher to eat (must hold two forks during this time)
    • time_to_sleep (in milliseconds): the time a philosopher spends sleeping
    • number_of_times_each_philosopher_must_eat (optional): if all philosophers have eaten this many times, the simulation stops; if not specified, the simulation stops when a philosopher dies
  • Each philosopher is assigned a number ranging from 1 to number_of_philosophers, and philosopher 1 sits next to philosopher number number_of_philosophers. Any other philosopher number N sits between philosopher number N - 1 and philosopher number N + 1
  • The logs of the program must include state changes of each philosopher formatted as follows:
    • 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 Replace timestamp_in_ms with the current timestamp in milliseconds and X with the philosopher number.
  • Each displayed state message should not be mixed up with another message
  • A message announcing a philosopher's death should be displayed no more than 10 ms after the actual death
  • Philosophers should avoid dying.

Evaluation

First submission

30/03/2023
125%

42-philosophers's People

Contributors

palzap avatar

Stargazers

Rafael Soares 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.