Giter VIP home page Giter VIP logo

philo's Introduction

philosophers Project note

Totally normal Will Smith eating spaghetti like a normal human being

Here is an AI generated gif of Will Smith eating spaghetti so you can feel the same way I felt while doing this project. You're welcome!

WARNING WARNING WARNING

This README is not done yet so do not bother reading it hahaha or do but whatever ok bye

Table of contents

Project overview

Why did the five philosophers choose a round table for their dinner?

Because they wanted to have a "well-rounded" discussion on whether it's possible to eat their meals without getting into a "forking" deadlock!

...

This project is 42's twist on 1965 Edsger Dijkstra's Dining Philosophers problem, a classic synchronization problem in computer science that illustrates the challenges of resource allocation and deadlock avoidance in multi-threaded systems.

The problem is defined as follows: There are five philosophers sitting around a circular table, and each philosopher alternates between thinking and eating. There is a bowl of rice placed between each pair of adjacent philsopher and their neighbooring bowl. Therefore, there are five bowls and five chopsticks in total. The behavior of each philosopher is represented by a concurrent process or thread. To eat, a philosopher needs to pick up the two chopsticks adjacent to them. However, a chopstick can only be held by one philosopher at a time. This creates potential conflict if all philosophers decide to eat simultaneously and try to pick up their left chopstick at the same time. The challenge is to design a solution that ensures the following conditions are satisfied:

  1. Mutual Exclusion: Only one philosopher can hold a chopstick at a time.
  2. Deadlock-free: The system should not enter a state where all philosophers are waiting for a chopstick indefinitely.
  3. Starvation-free: Every philosopher should get a chance to eat eventually.

In our case, the bowl of rice and chopsticks are replaced by Eminem's favorite meal, his mom's spaghetti, and forks. But the concept stays the same. Also, our simulation can contain a table holding from 1 to 200 philosophers, and they sleep after every meal. Each activity takes them an certain amount of time, and if they haven't eaten for a predefined "time to die", they will DIE. ๐Ÿ’€ We do not want that to happen.

It's also important to note that the philosophers cannot communicate with one another. They do not know if another philosopher is about to die, they are only focused on their own routine. However, if a philosopher dies, the simulation should not continue! This means we have another important notion to take into consideration: race conditions.

Instructions

Compilation

The compilation for this project is quite simple. All you need to do is use the make command in the cloned repository, then execture the program like this: <./philo> followed by the arguments, complying with the requirements as shown below.

Example:

./philo 2 800 200 200 5

Arguments requirements

Arg. number Description
1 Number of philosophers
2 Time to die
3 Time to eat
4 Time to sleep
5 Number of meals
  • The time_to_die countdown starts as soon as the philo has started his meal. If he has not eaten time_to_die ms after the beginning of his last meal, well you guessed it, he dies.
  • You should never test with more than 200 philosophers! (Do not worry though, I have made sure you don't ๐Ÿ˜‰)
  • You should always test with times higher than 60 ms.

Testing

https://nafuka11.github.io/philosophers-visualizer/

What I would improve

  • The structure of the code to make it faster (checking how much time has passed and seeing what action should be printed in consequence of that). Instead of using my destiny checker hack.
  • Protecting my mallocs.

Destiny checker

Do not take my code as an example! I hacked my way into succeeding this project and I wish I would have had more time to find out ways to make my code more efficient beforehand.

philo's People

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.