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...
Users Online
· Guests Online: 96
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
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.
-
/*
-
* C# Program to Illustrate Dining Philosopher Problem
-
*/
-
using System;
-
using System.Threading;
-
class philofork
-
{
-
bool[] fork = new bool[5];
-
public void Get(int left, int right)
-
{
-
lock (this)
-
{
-
while (fork[left] || fork[right]) Monitor.Wait(this);
-
fork[left] = true; fork[right] = true;
-
}
-
}
-
public void Put(int left, int right)
-
{
-
lock (this)
-
{
-
fork[left] = false; fork[right] = false;
-
Monitor.PulseAll(this);
-
}
-
}
-
}
-
class Philo
-
{
-
int n;
-
int thinkDelay;
-
int eatDelay;
-
int left, right;
-
philofork philofork;
-
public Philo(int n, int thinkDelay, int eatDelay, philofork philofork)
-
{
-
this.n = n;
-
this.thinkDelay = thinkDelay; this.eatDelay = eatDelay;
-
this.philofork = philofork;
-
left = n == 0 ? 4 : n - 1;
-
right = (n + 1) % 5;
-
new Thread(new ThreadStart(Run)).Start();
-
}
-
public void Run()
-
{
-
for (; ; )
-
{
-
try
-
{
-
Thread.Sleep(thinkDelay);
-
philofork.Get(left, right);
-
Console.WriteLine("Philosopher " + n + " is eating...");
-
Console.ReadLine();
-
Thread.Sleep(eatDelay);
-
philofork.Put(left, right);
-
}
-
catch
-
{
-
return;
-
}
-
}
-
}
-
-
}
-
public class philopblm
-
{
-
public static void Main()
-
{
-
philofork philofork = new philofork();
-
new Philo(0, 10, 50, philofork);
-
new Philo(1, 20, 40, philofork);
-
new Philo(2, 30, 30, philofork);
-
new Philo(3, 40, 20, philofork);
-
new Philo(4, 50, 10, philofork);
-
}
-
}
Here is the output of the C# Program:
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.