Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Threading;
- namespace OperatingSystemProject
- {
- public partial class Form1 : Form
- {
- // Array to represent the forks, initially set to available (0)
- bool[] fork = new bool[5];
- public Form1()
- {
- InitializeComponent();
- }
- // Picking left and right fork
- private void Get(int left, int right, int n)
- {
- lock(this) // Using 'this' object as a lock to synchronize access to shared resources
- {
- while (fork[left] || fork[right]) // While either fork is in use
- {
- Nasr.Invoke((MethodInvoker)(() => Nasr.AppendText($"Philosopher {n} is thinking...\n"))); // Output that the philosopher is thinking
- Monitor.Wait(this); // Wait for notification to acquire both forks (waiting PulseAll(this)
- }
- fork[left] = true; // Pick up left fork
- fork[right] = true; // Pick up right fork
- }
- }
- // Putting down forks
- private void Put(int left, int right, int n)
- {
- lock(this) // Using 'this' object as a lock to synchronize access to shared resources
- {
- fork[left] = false; // put down left fork
- fork[right] = false; // put down right fork
- Nasr.Invoke((MethodInvoker)(() => Nasr.AppendText($"Philosopher {n} stopped eating...\n"))); // Output that the philosopher stopped eating
- Monitor.PulseAll(this); // Notify waiting threads that forks are available (the Mointor.Wait(This) will continue)
- }
- }
- private void PhilosopherAction(int n, int thinkDelay, int eatDelay)
- {
- int left = n == 0 ? 4 : n - 1; // Index of the left fork
- int right = n; // Index of the right fork
- new Thread(() =>
- {
- while(true) // Infinite loop to (continuous actions of a philosopher)
- {
- try
- {
- Thread.Sleep(thinkDelay); // thinking time
- Get(left, right, n); // try to pick up forks
- Nasr.Invoke((MethodInvoker)(() => Nasr.AppendText($"Philosopher {n} is eating...\n"))); // Output that the philosopher is eating
- Thread.Sleep(eatDelay); // eating time
- Put(left, right, n); // put down forks after eating
- }
- catch
- {
- return;
- }
- }
- }).Start(); // Start the thread
- }
- // run the dining philosophers problem
- private void RunProblem()
- {
- PhilosopherAction(0, 5000, 5000); // Philosopher 0
- PhilosopherAction(1, 5000, 5000); // Philosopher 1
- PhilosopherAction(2, 5000, 5000); // Philosopher 2
- PhilosopherAction(3, 5000, 5000); // Philosopher 3
- PhilosopherAction(4, 5000, 5000); // Philosopher 4
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private System.Threading.Timer timer;
- private void simpleButton1_Click(object sender, EventArgs e)
- {
- RunProblem(); // Start the dining philosophers simulation
- // calling the timer function that will excute every 5.1 secs
- timer = new System.Threading.Timer(TimerCallback, null, 5100, 5100);
- }
- // The Timer function that adds newline
- private void TimerCallback(Object o)
- {
- Nasr.Invoke((MethodInvoker)(() => Nasr.AppendText($"\n")));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment