Giter Site home page Giter Site logo

frhd143 / dining-philosophers-problem Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 8 KB

This is a solution to the dining philosophers problem in C using pthreads and semaphores.

License: MIT License

C 100.00%
c cprogramming-language deadlock-avoidance deadlocks dinning-phillospher semaphores threads

dining-philosophers-problem's Introduction

Dining Philosophers Problem

This is a solution to the dining philosophers problem in C using pthreads and semaphores.

Overview

The code is an implementation of the dining philosophers problem in C using semaphores and threads. It aims to break the hold and wait condition as well as the circular wait condition that can occur in the problem.

The program creates 5 philosopher threads and initializes 5 semaphores for the chopsticks. Each philosopher thread goes through a loop where they alternate between thinking and eating. When a philosopher wants to eat, they first try to pick up the chopstick on their right, except for philosopher 0 who picks up the right chopstick first to break the circular wait condition (This prevents the situation where all philosophers are holding one chopstick and waiting for the other).

If the right chopstick is not available, the philosopher puts down the chopstick they have picked up and waits for a random amount of time before trying again. If the right chopstick is available, the philosopher picks it up and tries to pick up the chopstick on their left. If the left chopstick is not available, the philosopher puts down both chopsticks and waits for a random amount of time before trying again. If the left chopstick is available, the philosopher picks it up and starts eating for a random amount of time before putting down both chopsticks.

The code breaks the hold and wait condition by implementing a strategy where a philosopher only picks up both chopsticks if both are available at the same time. This avoids the situation where a philosopher holds one chopstick and waits indefinitely for the other.

Contributing

Contributions to this project are welcome. If you find a bug or want to suggest an improvement, please open an issue or submit a pull request. Or email me here: [email protected]

License

This code is released under the MIT License.

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.