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];
- // Arrty of labels to represent labels of Phil
- Label[] philosopherLabels = new Label[5];
- // Arrty to labels represent labels of forks
- Label[] ForksLabels = new Label[5];
- public Form1()
- {
- InitializeComponent();
- // putting array's values according to label's names in Phil
- philosopherLabels[0] = Ph0;
- philosopherLabels[1] = Ph1;
- philosopherLabels[2] = Ph2;
- philosopherLabels[3] = Ph3;
- philosopherLabels[4] = Ph4;
- //putting array's valuea according to label's name in Forks
- ForksLabels[0] = F0;
- ForksLabels[1] = F1;
- ForksLabels[2] = F2;
- ForksLabels[3] = F3;
- ForksLabels[4] = F4;
- }
- // function to Change status of phil (eating, or not)
- private void ChangePhiLabelColor(int n, bool eating)
- {
- if(eating)
- philosopherLabels[n].Invoke((MethodInvoker)(() => philosopherLabels[n].ForeColor = Color.Red));
- else
- philosopherLabels[n].Invoke((MethodInvoker)(() => philosopherLabels[n].ForeColor = Color.Lime));
- }
- // function to Change status of Forks (used, or not)
- private void ChangeForkLabelColor(int n, bool eating)
- {
- if (eating)
- ForksLabels[n].Invoke((MethodInvoker)(() => ForksLabels[n].ForeColor = Color.Red));
- else
- ForksLabels[n].Invoke((MethodInvoker)(() => ForksLabels[n].ForeColor = Color.Lime));
- }
- // 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
- {
- 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
- ChangePhiLabelColor(n, true); //change Phi label Color to ``red`` (eating)
- ChangeForkLabelColor(left, true); // change left Fork color to ``red`` (used)
- ChangeForkLabelColor(right, true); // change right Fork color to ``red`` (used)
- }
- }
- // 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
- ChangePhiLabelColor(n, false); // change Phi label Color to ``green`` (not eating)
- ChangeForkLabelColor(left, false); // change left Fork color to ``green`` (not used)
- ChangeForkLabelColor(right, false); // change right Fork color to ``green`` (not used)
- Nasr.Invoke((MethodInvoker)(() => Nasr.AppendText($"Philosopher {n} is Thinnking...\n"))); // Output that the philosopher is thinking
- Monitor.PulseAll(this); // Notify waiting threads that forks are available (the Mointor.Wait(This) will continue)
- Thread.Sleep(4000); // sleep 4 secs for better visualization
- }
- }
- 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, 4000, 7000); // Philosopher 0
- PhilosopherAction(1, 4000, 7000); // Philosopher 1
- PhilosopherAction(2, 4000, 7000); // Philosopher 2
- PhilosopherAction(3, 4000, 7000); // Philosopher 3
- PhilosopherAction(4, 4000, 7000); // Philosopher 4
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void simpleButton1_Click(object sender, EventArgs e)
- {
- RunProblem(); // Start the dining philosophers simulation
- }
- private void simpleButton2_Click(object sender, EventArgs e)
- {
- }
- private void Ph0_Click(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment