Giter Site home page Giter Site logo

whoismtrx / 42_philosophers Goto Github PK

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

In the 42 philosophers project, the goal is to develop a synchronization mechanism that enables a group of philosophers to share limited resources while avoiding issues like deadlock.

C 87.44% Makefile 12.56%
1337 1337cursus 1337school 42 42cursus 42projects 42school c clang clanguage philosophers philosophers-42 philosophers-dinner-problem philosophers42 processes threads

42_philosophers's Introduction

Philosophers

Overview

This project is a simulation of the dining philosophers problem. The dining philosophers problem is a classic synchronization problem that models a situation where some philosophers are sitting at a table and they spend their time thinking and eating and sleeping. The philosophers share a common resources, table and forks and have to use them in a way that they don't starve and don't create a deadlock.

Key Features

The project is implemented in C and uses pthread library for thread management in Mandatory part and fork for process management in Bonus part. The project has the following key features:

  • Each philosopher is a separate thread/process depending on the part.
  • Each philosopher has a fork on the left and a fork on the right.
  • A philosopher can only eat if he has both forks.
  • A fork can be Mutex or Semaphore depending on the part.
  • Each philosopher has a state: thinking, eating, sleeping, dead.
  • Each philosopher has a time_to_die, time_to_eat, time_to_sleep and number_of_times_to_eat.
  • The simulation stops when a philosopher dies or when all philosophers have eaten number_of_times_to_eat times.

Getting Started

To get a local copy of the project, perform the following steps:

git clone https://github.com/whoismtrx/42_Philosophers.git philosophers
cd philosophers

for Mandatory part:

cd philo
make

for Bonus part:

cd philo_bonus
make

Usage

To run the simulation, execute the following command:

./philo 4 410 200 200 [5]

where:

  • 4 is the number of philosophers.
  • 410 is the time to die in milliseconds.
  • 200 is the time to eat in milliseconds.
  • 200 is the time to sleep in milliseconds.
  • [5] is the number of times each philosopher must eat. If not provided, the simulation stops when a philosopher dies.

Implementation

Philosophers is a project that requires a good understanding of synchronization and parallel computing. first we need to understand the dining philosophers problem and then implement a solution that satisfies the requirements of the project. We need to create a simulation where philosophers are threads so basically we need to create a separate thread for each philosopher with its own routine. Each philosopher has a state and a set of actions that he can perform. The philosophers share a common resources, the table and forks, the table its the process itself and the forks are the mutexes that we used to lock and unlock the forks, so let's start philosopher routine. Routing is simple, philosopher will think, then try to take his left and right forks, if he can't take both of them he stay in the same state and try again, when he take both forks he will eat, then he will release the forks and sleep and then he will think again and so on. The simulation stops when a philosopher dies or when all philosophers have eaten number_of_times_to_eat times. you can create a thread who monitors the philosophers and check if they are alive and if they have eaten enough times. The bonus part is the same as the mandatory part but instead of threads we use processes, so we need to create a separate process for each philosopher with its own routine. and because processes don't share memory we need to use shared memory to store the state of the philosophers and the forks. we can use semaphores to lock and unlock the forks. the rest of the implementation is the same as the mandatory part.

Resources

42_philosophers's People

Contributors

whoismtrx avatar

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.