Giter Site home page Giter Site logo

philosophers's Introduction

philosophers

Final Score 125/100

Dining philosophers problem.
All files must pass the norminette check.

Mandatory part

One need to solve the problem so that there is no deadlock. Philosophers are seated in a circle and a fork is placed to the right of each. Philosophers do not communicate and do not know anything about each other. Philosophers should eat, sleep and think. 4 or 5 parameters are fed into the program: number_of_philosophers, time_to_die, time_to_eat, time_to_sleep, number_of_times_each_philosopher_must_eat (optional). All philosophers are in range from 1 to number_of_philosophers. Any state change of a philosopher looks like: "timestamp_in_ms num_of_philosopher message". In this part philosopher is a thread, other details in the subject.

Algorithm

  1. Create a structure for collecting all needed information about each philosopher and a pointer to base parameters structure philo.h.
  2. Check parameters which is fed into the program and save these into the sctructures validate.c.
  3. Initialize all structures for every philosopher (from 1 to number_of_philosophers) begin.c.
  4. Use get_max_fork() and get_min_fork(). For every philosopher except the last first gives get_min_fork() then give get_max_fork(), this will guarantee no deadlock. forks.c
  5. Create algorithm for each philosopher to wait: timing.c
  • if number_of_philosopher == 1 then wait some delay (START_MS = 10ms)
  • if id is even then wait some delay + time_tie_eat (id = ordinal number of philosopher).
  • if id is the last one then wait some delay twice + time_tie_eat.
  • if id is odd then wait some delay.
  1. The algorithm still won't guarantee death of philosopher so in this case thinking will help steps.c.
  2. Create thread for every philosopher and thread for checking if philosopher is dead.
  3. Mutex_lock and mutex_unlock everything that is needed and mutex print status and go until someone dies or until everyone eats number_of_times_each_philosopher_must_eat else move forever.
    In thinking(): steps.c
  • If time to eat is less than time to sleep then we are going to wait some delay again and get uniform cycles.
  • For odd number_of_philosopher it is needed to wait some delay.

Usage

make - to compile mandatory part go to philo/ else philo_bonus/ make clean - to remove objects
make fclean - to remove objects and binary files
make re - to re-compile

Visualizer

To see color data output output: nafuka11

philosophers's People

Contributors

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