Dining Philosophers Problem

Reply

Join Date: Feb 2007
Posts: 7
Reputation: paeez is an unknown quantity at this point 
Solved Threads: 0
paeez paeez is offline Offline
Newbie Poster

Dining Philosophers Problem

 
0
  #1
Jul 10th, 2007
can any one explain about the below code which is Dining Philosophers Problem..
  1. //Dining Philosophers Problem
  2. import java.util.Random;
  3. class Monitor {
  4. int phil_States[] = new int[5]; // 0=not_waiting, 1=waiting // 2=eating
  5. boolean fork_States[] = new boolean[5]; // false = in use, true = free
  6. Monitor() { // constructor
  7. for(int i=0;i<5;i++) {
  8. phil_States[i]=0;
  9. fork_States[i]=true;
  10. }
  11. }
  12. synchronized void print_State()
  13. {
  14. System.out.println(); // newline
  15. for(int i=0;i<5;i++)
  16. System.out.print(" " + phil_States[i]);
  17. }
  18. synchronized void ask_to_eat(int pId)
  19. {
  20. while(!fork_States[pId] || !fork_States[(pId+1)%4])
  21. { // while it can't have both forks, wait
  22. phil_States[pId] = 1;
  23. try
  24. {wait();}
  25. catch(InterruptedException e){} // it gets released
  26. } // by a process doing a call to notify()
  27. phil_States[pId] = 2; // eating
  28. fork_States[pId] = false; // in use
  29. fork_States[(pId+1)%4] = false;
  30. }
  31. synchronized void ask_to_leave(int pId)
  32. {
  33. fork_States[pId] = true; // available
  34. fork_States[(pId+1)%4] = true;
  35. phil_States[pId] = 0; // thinking
  36. notify(); // free the Phil that has waited the longest
  37. }
  38. }
  39.  
  40. class Diners {
  41. public static void main(String args[]) { // execution of the whole
  42. Monitor m = new Monitor(); // thing begins here
  43. Timer t = new Timer(m); // make a new timer
  44. Phil p[] = new Phil[5]; // make an array of 5 refs to Phils
  45. for(int i=0; i<5; i++)
  46. p[i] = new Phil(i,m,t); // create the phils and start them
  47. }
  48. }
  49. class Phil implements Runnable {
  50. Monitor m;
  51. Timer t;
  52. Random r = new Random(); // Random number generator object
  53. int pId;
  54. float time;
  55. Phil(int pId, Monitor m, Timer t) { // constructor
  56. System.out.println(pId + "is started: ");
  57. this.pId = pId;
  58. this.m = m;
  59. this.t = t;
  60. new Thread(this, "Phil").start(); // make a new thread and start it
  61. }
  62. public void run() { // must override run, this is what
  63. for(int i=0; i<20; i++) { // is executed when the thread starts
  64. m.ask_to_eat(pId); // running
  65. time = 1000*r.nextFloat();
  66. try {Thread.sleep((int)time);} catch(Exception e){}
  67. m.ask_to_leave(pId);
  68. time = 1000*r.nextFloat();
  69. try {Thread.sleep((int)time);} catch(Exception e){}
  70. }
  71. t.report_Stop(); // tell the timer this one is done
  72. }
  73. }
  74. class Timer implements Runnable {
  75. Monitor m;
  76. int completed;
  77. Timer(Monitor m) { // constructor
  78. this.m = m;
  79. new Thread(this, "Tim").start(); // make a new thread and start it
  80. completed=0;
  81. }
  82. public void report_Stop() {
  83. completed++;
  84. }
  85. public void run() { // must override run(), this is
  86. while(completed!=5) { // what happens when the thread
  87. m.print_State(); // begins
  88. try {Thread.sleep(500);} catch(Exception e){}
  89. }
  90. }
  91. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,346
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 498
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Dining Philosophers Problem

 
0
  #2
Jul 10th, 2007
Explain what about it? You haven't asked a question.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 7
Reputation: paeez is an unknown quantity at this point 
Solved Threads: 0
paeez paeez is offline Offline
Newbie Poster

Re: Dining Philosophers Problem

 
0
  #3
Jul 11th, 2007
explain what it does..
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Dining Philosophers Problem

 
0
  #4
Jul 11th, 2007
Looks like a threaded program. What d you know about threads so far?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,346
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 498
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Dining Philosophers Problem

 
0
  #5
Jul 11th, 2007
Originally Posted by paeez View Post
explain what it does..
Well, I would say it is a possible solution to the Dining Philosophers problem, which one quick Google search provided the explanation:
http://en.wikipedia.org/wiki/Dining_...ophers_problem

Are you wanting someone to walk you through how the code works step by step? I'm still not understanding what your question is exactly.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,145
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Dining Philosophers Problem

 
0
  #6
Jul 11th, 2007
seeing as the solution to the problem is contained in the official Sun tutorial, I wonder what the problem is
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 83
Reputation: ProgrammersTalk is an unknown quantity at this point 
Solved Threads: 7
ProgrammersTalk's Avatar
ProgrammersTalk ProgrammersTalk is offline Offline
Junior Poster in Training

Re: Dining Philosophers Problem

 
0
  #7
Jul 13th, 2007
The ProgrammersTalk Community | Programming & Marketing | Buying & Selling Script
Hang out place of novice and intermediate programmers
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC