Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using System.Collections.Generic;
- namespace philosophers_os
- {
- public class Fork
- {
- private Mutex m = new Mutex();
- public void take(){
- m.WaitOne();
- }
- public void put() {
- m.ReleaseMutex();
- }
- };
- public class Philosopher
- {
- public int id;
- public Fork leftFork;
- public Fork righFork;
- public uint eatCount;
- public double waitTime;
- public DateTime waitStart;
- public bool stopFlag;
- public bool debugFlag;
- Random random;
- public bool fL = false;
- public bool fR = false;
- void think()
- {
- if (this.debugFlag){
- Console.WriteLine(this.id + " thinking");
- }
- Thread.Sleep(this.random.Next(0, 100));
- if (this.debugFlag){
- Console.WriteLine(this.id + " hungry");
- }
- this.waitStart = DateTime.Now;
- }
- void eat()
- {
- this.waitTime += DateTime.Now.Subtract(this.waitStart).TotalMilliseconds;
- if (this.debugFlag){
- Console.WriteLine(this.id + " eating");
- }
- Thread.Sleep(this.random.Next(0, 100));
- eatCount++;
- }
- public Philosopher(int number, Fork left, Fork right, bool dbg)
- {
- this.id = number;
- this.leftFork = left;
- this.righFork = right;
- this.eatCount = 0;
- this.waitTime = 0;
- this.debugFlag = dbg;
- this.stopFlag = false;
- this.random = new Random();
- }
- public void run()
- {
- while (!stopFlag)
- {
- think();
- if (Waiter.WantFork(this, Side.Left))
- {
- //Waiter.freeForks--;
- this.leftFork.take();
- fL = true;
- if (this.debugFlag)
- {
- Console.WriteLine(this.id + " took left fork");
- }
- }
- else {
- Console.WriteLine("-------------------------");
- }
- if (Waiter.WantFork(this, Side.Right))
- {
- //Waiter.freeForks--;
- this.righFork.take();
- fR = true;
- if (this.debugFlag)
- {
- Console.WriteLine(this.id + " took right fork");
- }
- }
- if (fR && fL)
- eat();
- if (fR)
- {
- this.righFork.put();
- Waiter.freeForks++;
- fR = false;
- //Waiter.PutFork(this, Side.Right);
- if (this.debugFlag)
- {
- Console.WriteLine(this.id + " put right fork");
- }
- }
- if (fL)
- {
- this.leftFork.put();
- Waiter.freeForks++;
- fL = false;
- //Waiter.PutFork(this, Side.Left);
- if (this.debugFlag)
- {
- Console.WriteLine(this.id + " put left fork");
- }
- }
- }
- }
- public void stop(){
- stopFlag = true;
- }
- public void printStats(){
- Console.WriteLine(this.id + " " + this.eatCount + " " + Convert.ToInt32(this.waitTime));
- }
- };
- public static class Waiter
- {
- public static int freeForks = 0;
- public static void WaiterRem(int count) {
- freeForks = count;
- }
- public static bool WantFork(Philosopher philosopher, Side side)
- {
- bool perm = false;
- if (freeForks > 1)
- {
- if (side == Side.Left)
- freeForks--;
- Console.WriteLine("Waiter: YES)))");
- return true;
- }
- Console.WriteLine("Waiter: NO(");
- return perm;
- }
- public static void PutFork(Philosopher philosopher, Side side)
- {
- freeForks++;
- }
- }
- public enum Side{
- Left = 0,
- Right = 1
- }
- class Program
- {
- static void Main(string[] args)
- {
- int N = 5;
- bool dbg = true;
- int duration = 60000;
- Fork[] forks = new Fork[N];
- for (int i = 0; i < N; i++){
- forks[i] = new Fork();
- }
- Philosopher[] phils = new Philosopher[N];
- for (int i = 0; i < N; i++){
- phils[i] = new Philosopher(i + 1, forks[(i + 1) % N], forks[i], dbg);
- }
- //Waiter.WaiterRem(phils, forks);
- Waiter.WaiterRem(forks.Length);
- Thread[] runners = new Thread[N];
- for (int i = 0; i < N; i++){
- runners[i] = new Thread(phils[i].run);
- }
- for (int i = 0; i < N; i++){
- runners[i].Start();
- }
- Thread.Sleep(duration);
- for (int i = 0; i < N; i++){
- phils[i].stop();
- }
- for (int i = 0; i < N; i++){
- runners[i].Join();
- }
- for (int i = 0; i < N; i++){
- phils[i].printStats();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement