public Philosopher(Fork left, Fork right, string threadName) { this.left = left; this.right = right; thinkDurationTime = new Random().Next(1, 2000); eatDurationTime = new Random().Next(1, 2000); thread = new Thread(Start); thread.Name = threadName; thread.Start(); }
private Fork GetOrCreateFork(Fork[] forks, int i) { var fork = forks[i]; if (fork != null) { return(fork); } fork = new Fork(i); forks[i] = fork; return(fork); }
public Philosopher(int id, Fork left, Fork right, int maxThinkTime, int maxEatTime) { _left = left; _right = right; _id = id; _stopFlag = false; _random = new Random(); _maxThinkTime = maxThinkTime; _maxEatTime = maxEatTime; EatCount = 0; TotalWaitTime = 0; _stopwatch = new Stopwatch(); }
public Philosopher(Fork left, Fork right, string name) { _left = left ?? throw new ArgumentNullException(nameof(left)); _right = right ?? throw new ArgumentNullException(nameof(right)); Name = name; var random = new Random(); _thinkDurationMs = random.Next(1, MaxThinkDurationMs); _eatDurationMs = random.Next(1, MaxEatDurationMs); var thread = new Thread(Start); thread.Start(); }
static private void MakeDecision(Fork fork, int neighbourId, Intracommunicator comm) { if (fork.haveFork) { if (!fork.Dirty) { return; } else if (fork.Dirty) { string pom = ""; fork.haveFork = false; for (int i = 0; i < comm.Rank; i++) { pom += SPACING; } Console.WriteLine(pom + "sending fork" + neighbourId.ToString()); comm.Send <bool>(true, neighbourId, 1); } } }
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; } }
static void Main(string[] args) { using (new MPI.Environment(ref args)) { Fork rightFork = null; Fork leftFork = null; bool rightToken = false; bool leftToken = false; int rightNeighbour = -1; int leftNeighbour = -1; string pom = ""; Intracommunicator comm = Communicator.world; int procesId = comm.Rank; rightNeighbour = procesId + 1; if (rightNeighbour > (comm.Size - 1)) { rightNeighbour = 0; } leftNeighbour = procesId - 1; if (leftNeighbour < 0) { leftNeighbour = comm.Size - 1; } for (int i = 0; i < procesId; i++) { pom += SPACING; } Console.WriteLine(pom + "Philosopher" + procesId.ToString()); if (procesId == 0) { rightFork = new Fork(true); leftFork = new Fork(true); } else if (procesId == (comm.Size - 1)) { rightFork = new Fork(false); leftFork = new Fork(false); rightToken = true; leftToken = true; } else { rightFork = new Fork(true); leftFork = new Fork(false); leftToken = true; } while (true) { bool RightNbMessage = false; bool LeftNbMessage = false; Random rand = new Random(); int sleep = rand.Next(5); Console.WriteLine(pom + "thinking"); for (int i = 0; i < sleep; i++) { RightNbMessage = CheckForMessage(rightNeighbour, comm); LeftNbMessage = CheckForMessage(leftNeighbour, comm); if (RightNbMessage) { comm.Receive <bool>(rightNeighbour, 0); rightToken = true; } if (LeftNbMessage) { comm.Receive <bool>(leftNeighbour, 0); leftToken = true; } if (rightToken) { MakeDecision(rightFork, rightNeighbour, comm); } if (leftToken) { MakeDecision(leftFork, leftNeighbour, comm); } System.Threading.Thread.Sleep(1000); } while (true) { RightNbMessage = false; LeftNbMessage = false; RightNbMessage = CheckForMessage(rightNeighbour, comm); LeftNbMessage = CheckForMessage(leftNeighbour, comm); if (RightNbMessage) { comm.Receive <bool>(rightNeighbour, 0); rightToken = true; } if (LeftNbMessage) { comm.Receive <bool>(leftNeighbour, 0); leftToken = true; } if (rightToken) { MakeDecision(rightFork, rightNeighbour, comm); } if (leftToken) { MakeDecision(leftFork, leftNeighbour, comm); } if (rightFork.haveFork && leftFork.haveFork) { break; } else { if (!rightFork.haveFork) { bool check = CheckForFork(rightNeighbour, comm); if (check) { Console.WriteLine(pom + "got fork" + rightNeighbour.ToString()); comm.Receive <bool>(rightNeighbour, 1); rightFork.haveFork = true; rightFork.CLeanFork(); } } if (!rightFork.haveFork) { if (rightToken) { AskForFork(rightNeighbour, comm); rightToken = false; } } if (!leftFork.haveFork) { bool check = CheckForFork(leftNeighbour, comm); if (check) { Console.WriteLine(pom + "got fork" + leftNeighbour.ToString()); comm.Receive <bool>(leftNeighbour, 1); leftFork.haveFork = true; leftFork.CLeanFork(); } } if (!leftFork.haveFork) { if (leftToken) { AskForFork(leftNeighbour, comm); leftToken = false; } } } } rand = new Random(); int eating = rand.Next(5001); Console.WriteLine(pom + "eating"); rightFork.DirtyFork(); leftFork.DirtyFork(); System.Threading.Thread.Sleep(eating); #region comm RightNbMessage = false; LeftNbMessage = false; RightNbMessage = CheckForMessage(rightNeighbour, comm); LeftNbMessage = CheckForMessage(leftNeighbour, comm); if (RightNbMessage) { comm.Receive <bool>(rightNeighbour, 0); rightToken = true; } if (LeftNbMessage) { comm.Receive <bool>(leftNeighbour, 0); leftToken = true; } if (rightToken) { MakeDecision(rightFork, rightNeighbour, comm); } if (leftToken) { MakeDecision(leftFork, leftNeighbour, comm); } #endregion } } }