Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Dining Philosophers Problem -- I'm having great difficulties

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Dining Philosophers Problem -- I'm having great difficulties

    Hello! I really need help with an exercise.
    The exercise goes like this: Implement a version where a philosopher waits if a chopstick is already used by another philosopher. (You can use synchronized or Semaphores.) In this task
    each philosopher should first pick up the left and then the right chopstick. Run the code a couple of times until you observe deadlock. (If it is hard to get a deadlock, modify the times in sleep.
    --
    After I've implemented my code (in Table Class):
    public class Table {
     
        int nbrOfChopsticks;
        private boolean chopstick[]; //true if chopstick[i] is available
     
     
        public Table(int nbrOfSticks) {
            //nbrOfSticks == 5
            nbrOfChopsticks = nbrOfSticks;
            chopstick = new boolean[nbrOfChopsticks];
            for (int i = 0; i < nbrOfChopsticks; i++) {
                chopstick[i] = true;
            }
        }
     
        public synchronized void getLeft(int n) {
     
            //while the left chopstick is unavailable, wait in while loop for it to become available
            while (chopstick[n] == false) {
                try {
                    //instruction pdf says modify time in wait if you have trouble getting deadlock (which I do)
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //philosopher n picks up its left chopstick
            //chopstick[n] is set to unavailable since it's picked up
            chopstick[n] = false;
        }
     
        public synchronized void getRight(int n) {
     
            //calculate the right chopstick position
            int pos = n + 1;
            if (pos == nbrOfChopsticks) {
                pos = 0;
            }
     
            /* while the right chopstick is not unavailable
            AND while the left chopstick is not picked up
            wait in the for loop
             */
            while ((chopstick[pos] == false) && (chopstick[n] == true)) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
     
            //philosopher n picks up its right chopstick
            //made unavailable since it's picked up
            chopstick[pos] = false;
        }
     
        public synchronized void releaseLeft(int n) {
            //philosopher n puts down its left chopstick
            chopstick[n] = true;
            notifyAll();
        }
     
        public synchronized void releaseRight(int n) {
            //philosopher n puts down its right chopstick
            int pos = n + 1;
            if (pos == nbrOfChopsticks) {
                pos = 0;
            }
     
            chopstick[pos] = true;
            notifyAll();
        }
    }


    Everything in the Table class was given by the teacher, I've only implemented code where the getLeft(int n) method waits while the left chopstick (chopstick[n]) is taken. Then I also made the getRight(int n) method wait while the right chopstick (chopstick[pos]) is taken AND while the left chopstick is not picked up. (Because it says that the philosopher should pick the left up first, then the right). Then I added notifyAll() on both the release methods.

    It seems to say I'm supposed to get a deadlock and I don't get one. I have no idea what to do. Can I get help, please?

    The philosopher class (we are not to make any changes here):
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    public class Philosopher implements Runnable {
        private int id;
        private Table myTable;
     
        public Philosopher(int pid, Table tab) {
            id = pid;
            myTable = tab;
        }
     
        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                try {
                    //think
                    System.out.println("Philosopher " + id + " thinks. Iteration " + i);
                    Thread.sleep((int) (Math.random() * 100));
                    //pick up left chopstick
                    myTable.getLeft(id);
                    System.out.println("Philosopher " + id + " pick up left");
                    Thread.sleep((int) (Math.random() * 10));
                    //pick up right chopstick
                    myTable.getRight(id);
                    System.out.println("Philosopher " + id + " pick up right");
                    //eat
                    System.out.println("Philosopher " + id + " eats. Iteration " + i);
                    Thread.sleep((int) (Math.random() * 100));
                    //release left chopstick
                    myTable.releaseLeft(id);
                    System.out.println("Philosopher " + id + " drop left");
                    Thread.sleep((int) (Math.random() * 10));
                    //release right chopstick
                    myTable.releaseRight(id);
                    System.out.println("Philosopher " + id + " drop right");
                    Thread.sleep((int) (Math.random() * 10));
                } catch (InterruptedException ex) {
                    Logger.getLogger(Philosopher.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    Main class (make no changes here):
    public class DiningPhilosophers {
     
        public static void main(String[] args) {
            int size = 5; //number of philosophers and chopsticks
            Table tab = new Table(size);
     
            //create five philosophers
            for (int i = 0; i < size; i++) {
                Thread th = new Thread(new Philosopher(i, tab));
                th.start();
            }
        }
    }
    Last edited by nik1996; October 24th, 2020 at 04:08 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Dining Philosophers Problem -- I'm having great difficulties

    How do you execute the code for testing? I do not see a main() method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2020
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Dining Philosophers Problem -- I'm having great difficulties

    Quote Originally Posted by Norm View Post
    How do you execute the code for testing? I do not see a main() method.
    Here's the main method (I've put it in the OP now as well):

    public class DiningPhilosophers {
     
        public static void main(String[] args) {
            int size = 5; //number of philosophers and chopsticks
            Table tab = new Table(size);
     
            //create five philosophers
            for (int i = 0; i < size; i++) {
                Thread th = new Thread(new Philosopher(i, tab));
                th.start();
            }
        }
    }

    So there are three classes in the project. This one, the Philosopher class/thread, and Table class (which are both on the OP)
    All changes are to be made on the Table class
    Last edited by nik1996; October 24th, 2020 at 04:16 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Dining Philosophers Problem -- I'm having great difficulties

    Why should there be a deadlock? Can you describe the conditions that will cause a deadlock?
    Can you call the philosophers in the order that will cause a deadlock?

    Add some comments to the code describing how the chopsticks are numbered relative to the philosophers.
    For philosopher N what cs is to left and what to right?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2020
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Dining Philosophers Problem -- I'm having great difficulties

    Quote Originally Posted by Norm View Post
    Why should there be a deadlock? Can you describe the conditions that will cause a deadlock?
    Can you call the philosophers in the order that will cause a deadlock?
    I'm not sure if you're asking me a theoretical question re: if there should be a deadlock. The exercise says there should be one:
    Implement a version where a philosopher waits if a chopstick is already used by another philosopher. (You can use synchronized or Semaphores.) In this task
    each philosopher should first pick up the left and then the right chopstick. Run the code a couple of times until you observe deadlock. (If it is hard to get a deadlock, modify the times in sleep.
    My thoughts however would be that a deadlock could arise in case all of the philosopher's pick up their chopsticks to the left. Then there wouldn't be any chopsticks to pick up -- and everyone would be waiting forever.
    I managed to get this happen by changing the condition in the while statement of the getRight(int i) method. to instead be:
            while ((chopstick[pos] == false) || chopstick[n] == false) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    however I'm not sure the condition makes any sense, or is even correct since the exercise asks us to pick the right chopstick up when the left isn't picked.

    Add some comments to the code describing how the chopsticks are numbered relative to the philosophers.
    For philosopher N what cs is to left and what to right?
    The left chopstick equals the id of the philosopher, and the right chopstick is (id + 1).

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Dining Philosophers Problem -- I'm having great difficulties

    Why the test of the left chopstick status?
    I thought that the left chopstick was picked up before the attempt to pick up the right one.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    nik1996 (October 25th, 2020)

Similar Threads

  1. Hello All! This is Great!!!
    By oblacker in forum Member Introductions
    Replies: 0
    Last Post: February 12th, 2013, 06:27 AM
  2. [SOLVED] Regular Expression Difficulties...
    By snowguy13 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 4th, 2012, 08:31 AM
  3. 1st Java class assignment, and having some difficulties. Little help please.
    By flpanthers1 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: May 27th, 2011, 04:09 AM
  4. Collision Detection difficulties
    By Uritomi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 20th, 2011, 10:10 AM
  5. Having Difficulties With a Multi Thread Code... Help?
    By Allicat in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 5th, 2011, 12:35 AM