static void Main(string[] args) { // Creating five philosophers and setting them at the table Philosopher Aristotle = new Philosopher("Arystoteles", 6, Table.OakChopstick, Table.PineChopstick); Philosopher Plato = new Philosopher("Platon", 5, Table.PineChopstick, Table.FirChopstick); Philosopher Socrates = new Philosopher("Sokrates", 5, Table.FirChopstick, Table.AshChopstick); Philosopher Pythagoras = new Philosopher("Pitagoras", 9, Table.AshChopstick, Table.BeechChopstick); Philosopher Epicurus = new Philosopher("Epikur", 3, Table.BeechChopstick, Table.OakChopstick); // Creating Threads (each philosopher starts thinking) new Thread(Aristotle.Think).Start(); new Thread(Plato.Think).Start(); new Thread(Socrates.Think).Start(); new Thread(Pythagoras.Think).Start(); new Thread(Epicurus.Think).Start(); }
static void Main(string[] args) { var n = 10; var forks = new object[n]; var philosophers = new Philosopher[n]; var threads = new Thread[n]; for (var i = 0; i < n; ++i) { forks[i] = new object(); } for (var i = 0; i < n; ++i) { philosophers[i] = new Philosopher(forks, i); threads[i] = new Thread(philosophers[i].Live); threads[i].Start(); } Console.ReadLine(); foreach (var cell in philosophers) { cell.Alive = false; } }
public void Create(int numPhilosophers) { if (numPhilosophers < 0) { throw new ArgumentException("Must be at least 0", nameof(numPhilosophers)); } if (_philosophers != null) { Dispose(); } _philosophers = new Philosopher[numPhilosophers]; var forks = new Fork[numPhilosophers]; for (int i = 0; i < numPhilosophers; i++) { var forkLeft = GetOrCreateFork(forks, i > 0 ? i - 1 : numPhilosophers - 1); var forkRight = GetOrCreateFork(forks, i); var philosopher = new Philosopher(forkLeft, forkRight, i.ToString()); _philosophers[i] = philosopher; } }
private void updateDisplay(byte[] s) { LogEntry entry = new LogEntry(); entry.msg = (LogMsg)s[0]; if (entry.msg == LogMsg.LOG_START) { phils = new List <Philosopher>(); } else { if (phils == null) { return; } Philosopher p; entry.props = Encoding.ASCII.GetString(s, 5, s.Length - 5).Split(';'); switch (entry.msg) { case LogMsg.LOG_NEW_TASK: p = new Philosopher(); p.tid = uint.Parse(entry.props[0]); if (p.tid == 0) { return; } p.name = entry.props[1]; phils.Add(p); break; case LogMsg.LOG_TASK_STATUS_CHANGE: TaskStates state = (TaskStates)int.Parse(entry.props[1]); uint tid = uint.Parse(entry.props[0]); if (tid == 0) { return; } p = (from temp in phils where temp.tid == tid select temp).First(); switch (state) { case TaskStates.READY: p.state = PhilStates.EATING; break; case TaskStates.SUSPENDED: p.state = PhilStates.WAITING; break; case TaskStates.SLEEPING: p.state = PhilStates.THINKING; break; case TaskStates.RUNNING: p.state = PhilStates.EATING; break; } break; } Refresh(); } }
static void Main(string[] args) { Philosopher forForkQueue = new Philosopher(5, 3); forForkQueue.Run(); }
static void Main(string[] args) { Philosopher forTurn = new Philosopher(); forTurn.Run(); }