Giter Site home page Giter Site logo

barimehdi77 / philosophers Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 0.0 71 KB

In this project, you will learn the basics of threading a process. You will learn how to make threads. You will discover the mutex.

C 78.30% Makefile 21.70%
philosophers 42 1337 mutex semaphore

philosophers's Introduction

๐Ÿค” Description

Philosophers (represented by parts of a program) sit around a table, with a big plate of spaghetti at its center. They each have a fork, but they need two forks to eat ; so they need to borrow their neighbor's fork to eat.

Of course, if they borrow a neighbor's fork, the neighbor can't use the fork. The processes / threads must communicate, to know the state of the forks (and of the other philosophers) at all times. There must also be a parent process that monitors everything.

๐Ÿ“™ Philosophers

This project I have to write two programes:

  • The First Program (Mandatory Part):
    • Each philosopher should be a thread.
    • One fork between each philosopher, therefore if they are multiple philosophers, there will be a fork at the right and the left of each philosopher.
    • To avoid philosophers duplicating forks, I should protect the forks state with a mutex for each of them.
  • The Second Program (Bonus Part):
    • All the forks are in the Middle of the table.
    • They have no states in memory but the number of available forks is represented by a semaphore.
    • Each philosopher should be a process and the main process should not be a philosopher.

โš™๏ธ Compilation

  • Compiling The First Program (Mandatory Part): make -C philo/
  • Compiling The Second Program (Bonus Part) : make -C philo_bonus/

๐Ÿ”‘ Execute

  • The Two Programs Take The Following Arguments:
    • number_of_philosophers: is the number of philosophers and also the number of forks.
    • time_to_die: is in milliseconds, if a philosopher doesnโ€™t start eating โ€™time_to_dieโ€™ milliseconds after starting their last meal or the beginning of the simulation, it dies.
    • time_to_eat: is in milliseconds and is the time it takes for a philosopher to eat. During that time they will need to keep the two forks.
    • time_to_sleep: is in milliseconds and is the time the philosopher will spend sleeping.
    • number_of_times_each_philosopher_must_eat: argument is optional, if all philosophers eat at least โ€™number_of_times_each_philosopher_must_eatโ€™ the simulation will stop. If not specified, the simulation will stop only at the death of a philosopher.

Example: Without number_of_times_each_philosopher_must_eat Argument ./philo 4 500 200 200
Example: With number_of_times_each_philosopher_must_eat Argument ./philo 7 310 100 100 7

๐Ÿ‘จโ€๐Ÿ’ป Ressources


"Buy Me A Coffee" ko-fi paypal

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.