Git Product home page Git Product logo

philos's Introduction

๐Ÿฝ๏ธ 42 Philosophers

philos_badge

๐ŸŽ“ Significance

The Dining Philosophers problem is a fundamental example in both computer science and philosophy. It illustrates the challenges of resource allocation and process synchronization in concurrent programming. By simulating philosophers who must share limited resources (forks) without causing deadlock or starvation, this problem teaches important concepts that are applicable to real-world systems where resources are limited and processes must be managed efficiently.

Philos


Overview

This project is a simulation of the classic Dining Philosophers problem, where philosophers alternate between eating, thinking, and sleeping while trying to avoid starvation and deadlocks. The goal is to understand and implement concepts of multithreading and synchronization using mutexes.

๐Ÿ’ก Concepts

  • Multithreading: Using POSIX threads (pthread) to create concurrent execution flows.
  • Mutexes: Synchronization primitives to avoid race conditions when accessing shared resources.
  • Deadlock Prevention: Ensuring that philosophers do not block each other indefinitely.
  • Dynamic Memory Management: Using malloc and free to manage memory allocation.

๐Ÿ“‹ Table of Contents

โš™๏ธ Installation

  1. Clone the repository:

    git clone https://github.com/pandashaly/philos.git
    cd philos
  2. Compile the project using the provided Makefile:

    make

๐Ÿš€ Usage

Run the program with the following command:

./philo <number_of_philosophers> <time_to_die> <time_to_eat> <time_to_sleep> [number_of_times_each_philosopher_must_eat]
  • number_of_philosophers: The number of philosophers and forks.
  • time_to_die: Time in milliseconds before a philosopher dies without eating.
  • time_to_eat: Time in milliseconds it takes for a philosopher to eat.
  • time_to_sleep: Time in milliseconds a philosopher will spend sleeping.
  • number_of_times_each_philosopher_must_eat (optional): If specified, the simulation stops when each philosopher has eaten at least this many times.

Example

./philo 5 800 200 200

This will run the simulation with 5 philosophers, where each philosopher has 800 milliseconds to live without eating, 200 milliseconds to eat, and 200 milliseconds to sleep.


๐Ÿ“ Code Structure

42-philosophers/
โ”œโ”€โ”€ Makefile
โ”œโ”€โ”€ main.c
โ”œโ”€โ”€ philos.h
โ””โ”€โ”€ README.md
  • Makefile: Contains rules to compile the project.
  • main.c: Contains the main program and function implementations.
  • philosophers.h: Header file with struct definitions and function prototypes.
  • README.md: This readme file.

๐Ÿ“š What I Learned

By doing this project as part of my 42 Core Curriculum, I learned the importance of multithreading and synchronization in software development. I gained practical experience with POSIX threads (pthread) and mutexes, which are essential for managing concurrency and ensuring that shared resources are used safely. Additionally, I understood the complexities of avoiding deadlock and starvation, which are critical for creating robust and reliable systems.


Ressources

philos's People

Contributors

pandashaly avatar

Watchers

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