i decided to use C# and implement a class that simulates the Dinning Philosophers.
Why only the first thread gets to run and then the program terminates?
How do i make all the treads run in parallel?
I also used the lock(),Same as Monitor.Enter(), Monitor.Exit)(), to prevent deadlock.
... i don't really know C# yet, i just coded based on what i know about java and c++ and its similarities to C#.
Here is the code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace DinningPhilosophers
{
class Philosopher
{
// static variables to be used throughout the program
const int TOTAL_PHILOSOPHERS = 5;
static readonly object _locker = new Object();
// instance variables
private enum Status { THINKING, HUNGRY, EATING };
private int[] _state; // state of each philosopher
private string[] _name; // name of each philosopher (keep array parallel to the _state)
public Philosopher()
{
System.Console.WriteLine("WELCOME TO THE DINNING PHILOSOPHERS SIMULATION");
// initialize all instance variables
init();
}
// initialize instance variables
private void init()
{
_state = new int[TOTAL_PHILOSOPHERS];
_name = new String[] { "Joaquim", "Socratis", "Plato", "Aristotle", "Karl Marx" };
// initialize all philosophers to thinking state
for (int i = 0; i < TOTAL_PHILOSOPHERS; i++)
{
_state[i] = (int)Status.THINKING;
this.thinking(i);
}
}
private void thinking(int philosId)
{
System.Console.WriteLine("{0,-10} is thinnking.", _name[philosId]);
}
private void eating(int philosId)
{
System.Console.ForegroundColor = ConsoleColor.Green;
System.Console.WriteLine("{0,-10} is eating.", _name[philosId]);
System.Console.ResetColor();
}
private void hungry(int philosId)
{
System.Console.ForegroundColor = ConsoleColor.Red;
System.Console.WriteLine("{0,-10} is hungry.", _name[philosId]);
System.Console.ResetColor();
}
private void printPickup(int philosId)
{
System.Console.WriteLine("{0,-10} pickup left chopstick.", _name[philosId]);
Thread.Sleep(1000); // this is just for the waiting effect
System.Console.WriteLine("{0,-10} pickup right chopstick.", _name[philosId]);
}
private void printPutdown(int philosId)
{
System.Console.WriteLine("{0,-10} putdown the chopsticks.", _name[philosId]);
}
/**
* The philosopher uses this method to putdown chopstick*/
private void putdown(int philosId)
{
lock (_locker)
{
_state[philosId] = (int)Status.THINKING;
this.thinking(philosId);
this.test((philosId + 4) % TOTAL_PHILOSOPHERS); // check left philos
this.test((philosId + 1) % TOTAL_PHILOSOPHERS); // check right philos
this.printPutdown(philosId);
}
}
/**
* The philosopher uses this method to pickup chopstick*/
private void pickup(int philosId)
{
lock (_locker)
{
_state[philosId] = (int)Status.HUNGRY;
this.hungry(philosId);
this.test(philosId);
//if (_state[philosId] != (int)Status.EATING) {}
Thread.Sleep(1000);
}
}
/**
* check if philosopher is hungry and attempt to feed him
*/
private void test(int philosId)
{
//allow philosopher to eat if philosopher is hungry
//the philosopher to the left/right are not eating
if ((_state[(philosId + 4) % TOTAL_PHILOSOPHERS] != (int)Status.EATING) &&
(_state[philosId] == (int)Status.HUNGRY &&
(_state[(philosId + 1) % TOTAL_PHILOSOPHERS] != (int)Status.EATING)))
{
_state[philosId] = (int)Status.EATING;
this.printPickup(philosId);
Thread.Sleep(2000);
this.eating(philosId);
}
}
/**
* this method runs the pickup and putdown method to simulate
* philosophers eating in a table.*/
public void run(object philosId)
{
int id = (int) philosId;
// while (true)
//{
Thread.Sleep(1000);
this.pickup(id);
Thread.Sleep(1000);
this.putdown(id);
// }
}
static void Main(string[] args)
{
Philosopher philos = new Philosopher();
Thread[] threads = new Thread[Philosopher.TOTAL_PHILOSOPHERS];
// initialize all threads
for (int i = 0; i < Philosopher.TOTAL_PHILOSOPHERS; i++)
{
threads[i] = new Thread(new ParameterizedThreadStart(philos.run));
System.Console.WriteLine("THREAD {0} IS BEING CREATED\n", i);
}
// FOR DEBUG
System.Console.WriteLine("______________________________________\n");
// start all thread
for (int i = 0; i < Philosopher.TOTAL_PHILOSOPHERS; i++)
{
try
{
System.Console.WriteLine("THREAD {0} IS BEING STARTED\n", i);
threads[i].Start(i);
}
catch (ThreadStateException e)
{
Console.WriteLine(e); // Display text of exception
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e);
}
}
// FOR DEBUG
System.Console.WriteLine("______________________________________\n");
// join all thread
for (int i = 0; i < Philosopher.TOTAL_PHILOSOPHERS; i++)
{
try
{
System.Console.WriteLine("THREAD {0} IS BEING JOINED\n", i);
threads[i].Join();
}
catch (ThreadStateException e)
{
Console.WriteLine(e); // Display text of exception
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e);
}
}
// FOR DEBUG
System.Console.WriteLine("______________________________________\n");
}
}
}

New Topic/Question
Reply




MultiQuote






|