Coder Social home page Coder Social logo

aggyx / philo Goto Github PK

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

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 6.86% C 93.14%

philo's Introduction

Philosophers

The Dining philosophers problem

This project shows the notions of execution threads in a program, and the sharing of resources between different threads using mutexes. Deadlock and data race problems will have to be dealt with.


To create this version of Edsger Dijkstra's problem I used two structures, One for the Main thread where the forks are located (variables shared by different Threads) and one structure for the scope of each routine executed by pthread_create() inside the main thread. Once the variables have been initialised by the parameter input, the pair philos will have a delay in coordinating the taking of the forks. So the odd philosophers will try to pick up the forks, eat, put the forks down, sleep and then think. Concurrently (thanks to the mutex), the even-numbered philosophers will do the same. The main thread (which launched all the philosophers) continues execution until it reaches pthread_join, except that this instance will be used to monitor the philosophers, so before the pthread_join the main thread will be in a continuous loop checking (with the mutex since the philo is also accessing its variables) the death variables, the number of times a philo has eaten and whether the philo is starving by looking at the time variable.

Installation

git clone [email protected]:Aggyx/philo
cd philo/
make

Usage

./philo philo_number time_to_die time_to_eat time_to_sleep [number_of_meals]

Note

All times passed as arguments should be in milliseconds, that way you won't have to wait ages to see the output :). In the example below, No one shoud die (see below):

./philo 2 401 200 200 [5]

Thanks !

Thanks to @PGCL1 to answer every question i had. Thanks to CodeVault for his amazing C channel.

philo's People

Contributors

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