Coder Social home page Coder Social logo

0bvim / philosophers Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 409 KB

In this project, you will learn the basics of threading a process. You will see how to create threads and you will discover mutexes.

License: MIT License

Makefile 8.58% C 91.42%
algorithms clanguage gdb helgrind makefile mutex mutex-lock threads valgrind

philosophers's Introduction

Dining Philosophers Problem

This repository contains an implementation of the Dining Philosophers Problem in C, using pthreads (POSIX threads) for concurrency management.

Table of Contents


Introduction

The Dining Philosophers Problem is a classic synchronization problem in computer science, designed to illustrate the challenges of concurrent programming. In this scenario, a number of philosophers sit around a dining table, each with a plate of spaghetti and a fork between each pair of adjacent philosophers. The philosophers spend their time either eating or thinking. To eat, a philosopher must acquire two forks—one from their left and one from their right. The challenge lies in preventing deadlocks and resource contention, ensuring that each philosopher can eat without causing conflicts with their neighbors.

This implementation provides a solution to the Dining Philosophers Problem using C and pthreads. It aims to demonstrate a practical approach to concurrency management in a multithreaded environment.

Problem Description

The Dining Philosophers Problem can be summarized as follows:

  • There are N philosophers sitting around a circular dining table.
  • Each philosopher alternates between thinking and eating.
  • To eat, a philosopher must pick up two forks—one from their left and one from their right.
  • The challenge is to devise a strategy that allows each philosopher to eat without leading to deadlocks or resource contention.

Implementation Details

  • The solution is implemented in C programming language.
  • POSIX threads (pthreads) are used for managing concurrency.
  • A mutex lock is employed to ensure exclusive access to shared resources (forks).
  • Each philosopher is represented by a separate thread.
  • The program ensures that no two adjacent philosophers can simultaneously hold the same fork.

How to Use

To run the program, follow these steps:

  1. Clone this repository to your local machine.
  2. Navigate to the directory containing the source code.
  3. Compile the program using make and then ./philo <number_of_philos> <time_to_die> <time_to_eat> <time_to_sleep> (optional)<times_to_eat>

philosophers's People

Contributors

0bvim avatar

Stargazers

Gustavo A. Bastian avatar Daniel Bruno avatar  avatar Dominik Antal avatar

Watchers

 avatar  avatar

philosophers's Issues

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.