Users Online

· Guests Online: 96

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

Dining Philosopher Problem in C#

Dining Philosopher Problem in C#

 

 

 

This C# Program Illustrates Dining Philosopher Problem. Here the Dining philosopher problem is solved and the results are displayed.

 

Here is source code of the C# Program to Illustrate Dining Philosopher Problem. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

  1. /*
  2.  * C# Program to Illustrate Dining Philosopher Problem
  3.  */
  4. using System;
  5. using System.Threading;
  6. class philofork
  7. {
  8.     bool[] fork = new bool[5]; 
  9.     public void Get(int left, int right)
  10.     {
  11.         lock (this)
  12.         {
  13.             while (fork[left] || fork[right]) Monitor.Wait(this);
  14.             fork[left] = true; fork[right] = true;
  15.         }
  16.     }
  17.     public void Put(int left, int right)
  18.     {
  19.         lock (this)
  20.         {
  21.             fork[left] = false; fork[right] = false;
  22.             Monitor.PulseAll(this);
  23.         }
  24.     }
  25. }
  26. class Philo
  27. {
  28.     int n;           
  29.     int thinkDelay;  
  30.     int eatDelay;    
  31.     int left, right; 
  32.     philofork philofork;     
  33.     public Philo(int n, int thinkDelay, int eatDelay, philofork philofork)
  34.     {
  35.         this.n = n;
  36.         this.thinkDelay = thinkDelay; this.eatDelay = eatDelay;
  37.         this.philofork = philofork;
  38.         left = n == 0 ? 4 : n - 1;
  39.         right = (n + 1) % 5;
  40.         new Thread(new ThreadStart(Run)).Start();
  41.     }
  42.     public void Run()
  43.     {
  44.         for (; ; )
  45.         {
  46.             try
  47.             {
  48.                 Thread.Sleep(thinkDelay);
  49.                 philofork.Get(left, right);
  50.                 Console.WriteLine("Philosopher " + n + " is eating...");
  51.                 Console.ReadLine();
  52.                 Thread.Sleep(eatDelay);
  53.                 philofork.Put(left, right);
  54.             }
  55.             catch
  56.             {
  57.                 return;
  58.             }
  59.         }
  60.     }
  61.  
  62. }
  63. public class philopblm
  64. {
  65.     public static void Main()
  66.     {
  67.         philofork philofork = new philofork();
  68.         new Philo(0, 10, 50, philofork);
  69.         new Philo(1, 20, 40, philofork);
  70.         new Philo(2, 30, 30, philofork);
  71.         new Philo(3, 40, 20, philofork);
  72.         new Philo(4, 50, 10, philofork);
  73.     }
  74. }

Here is the output of the C# Program:

Philosopher 0 is eating...
Philosopher 1 is eating...
Philosopher 2 is eating...
Philosopher 3 is eating...
Philosopher 4 is eating...
Philosopher 0 is eating...
Philosopher 1 is eating...
Philosopher 2 is eating...
Philosopher 3 is eating... 
Philosopher 4 is eating...
Philosopher 0 is eating...

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.70 seconds
10,834,294 unique visits