#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Dining Philosophers Problem Help


    Hello everyone,

    I have to write a solution code for dining philosophers problem

    i will take the PHILOSOPHERS as threads and every philosophers has 3 states, such as THINKING, EATING and HUNGRY.

    The main condition is;

    The philosopher i can eat only if his left and right (philosopher i-1 & philosopher i+1) neighbors are not in EATING state.

    My problem is i couldn't start to coding, i could not designate its starting point, please give me some advice about it

    Thanks
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,363
    Rep Power
    1870
    Perhaps you should begin with drawing a picture of the problem

    > i will take the PHILOSOPHERS as threads and every philosophers has 3 states, such as THINKING, EATING and HUNGRY.
    http://en.wikipedia.org/wiki/State_diagram
    Rather than just list the states randomly, draw another picture showing the sequence. It seems obvious that EATING follows HUNGRY, but where does THINKING fit?

    Consider starting them all in the THINKING state, then making them randomly HUNGRY as time progresses.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,701
    Rep Power
    480
    After eating a philosopher leaves to think for random length of time. Then returns hungry---does he have an assigned seat???? Edsger is dead. Can't ask him. Anyway, he grabs two forks, one from each side, and eats. Deadlocked when everyone takes the fork to his left. Or right.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0
    Do you re-write in c?, or help me to do it ?

    //------------------------------------------------------------------------
    // Filename:
    // Philosopher-mon.cpp
    // PROGRAM DISCRIPTION
    // PhilosopherMonitor class implementation file
    //------------------------------------------------------------------------

    #include <iostream.h>
    #include <stdlib.h>
    #include "ThreadClass.h"
    #include "Philosopher-Thrd.h"
    #include "Philosopher-mon.h"

    //------------------------------------------------------------------------
    // PhilosopherMonitor::PhilosopherMonitor()
    // constructor
    //------------------------------------------------------------------------

    PhilosopherMonitor::PhilosopherMonitor(char* Name)
    : Monitor(Name, HOARE)
    {
    for (int i = 0; i < PHILOSOPHERNUM; i++) {
    state[i] = THINKING; // initially, all philosophers are thinking
    strstream *ConditionName;
    ConditionName = new strstream;
    ConditionName->seekp(0, ios::beg);
    (*ConditionName) << "Self" << i << '\0';
    self[i] = new Condition(ConditionName->str());
    }
    }

    //------------------------------------------------------------------------
    // PhilosopherMonitor::Test(int Number)
    // Check if philosopher Number can have two chopsticks and eat
    // int Number -- The number of the philosopher
    //------------------------------------------------------------------------

    void PhilosopherMonitor::Test(int Number)
    {
    if (state[(Number+PHILOSOPHERNUM-1)%PHILOSOPHERNUM] != EATING && // left is not eating
    state[Number] == HUNGRY && // I am hungry, and
    state[(Number+1)%PHILOSOPHERNUM] != EATING ) { // right is not eating
    state[Number] = EATING; // philosopher Number can eat
    self[Number]->Signal(); // wake him up
    }
    }

    //------------------------------------------------------------------------
    // FUNCTION GetChopsticks(int):
    // This function implement a philosopher picking up two chopsticks
    // int Number -- The number of the philosopher
    //------------------------------------------------------------------------

    void PhilosopherMonitor::GetChopsticks(int Number)
    {
    MonitorBegin();
    state[Number] = HUNGRY; // I am hungry
    Test(Number); // Test if I can eat
    if (state[Number] != EATING) // if the test result says no,
    self[Number]->Wait(); // then wait
    MonitorEnd(); // finally, I can eat
    }

    //------------------------------------------------------------------------
    // FUNCTION PutChopsticks(int):
    // This function implement a philosopher returning two chopsticks
    // int Number -- The number of the philosopher
    //------------------------------------------------------------------------

    void PhilosopherMonitor::PutChopsticks(int Number)
    {
    MonitorBegin();
    state[Number] = THINKING; // go back to thinking
    Test((Number+PHILOSOPHERNUM-1) % PHILOSOPHERNUM); // let my left neighbor eat
    Test((Number+1)%PHILOSOPHERNUM); // let my right neighbor eat
    MonitorEnd();
    }

    // end of Philospher-mon.cpp








    the reason why i take this because the deadlock can not happen, since the philosophers are threads
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,363
    Rep Power
    1870
    > I have to write a solution code for dining philosophers problem
    ...
    > Do you re-write in c?, or help me to do it ?
    You took a week to find someone else's code on the web

    If you're not going to make an honest effort on your own, then why bother being on the course? If by some miracle you manage to google-search your way to a qualification, be sure that whilst the 'qualification' might help you to get a job, it will do nothing to help you keep a job (for which you will need real practical skills).

    There's certainly no longer any reward for us helping you, so it looks like you struck out.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,701
    Rep Power
    480
    If the kitty's out of the sack, there are solutions in various languages at rosetta code programming chrestomathy
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo