IMohammedNasr

Untitled

Dec 18th, 2023 (edited)
845
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Threading;
  11.  
  12. namespace OperatingSystemProject
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         // Array to represent the forks, initially set to available (0)
  17.         bool[] fork = new bool[5];
  18.         // Arrty of labels to represent labels of Phil
  19.         Label[] philosopherLabels = new Label[5];
  20.         // Arrty to labels represent labels of forks
  21.         Label[] ForksLabels = new Label[5];
  22.  
  23.         public Form1()
  24.         {
  25.             InitializeComponent();
  26.             // putting array's values according to label's names in Phil
  27.             philosopherLabels[0] = Ph0;
  28.             philosopherLabels[1] = Ph1;
  29.             philosopherLabels[2] = Ph2;
  30.             philosopherLabels[3] = Ph3;
  31.             philosopherLabels[4] = Ph4;
  32.             //putting array's valuea according to label's name in Forks
  33.             ForksLabels[0] = F0;
  34.             ForksLabels[1] = F1;
  35.             ForksLabels[2] = F2;
  36.             ForksLabels[3] = F3;
  37.             ForksLabels[4] = F4;
  38.         }
  39.  
  40.         // function to Change status of phil (eating, or not)
  41.         private void ChangePhiLabelColor(int n, bool eating)
  42.         {
  43.             if(eating)
  44.                philosopherLabels[n].Invoke((MethodInvoker)(() => philosopherLabels[n].ForeColor = Color.Red));
  45.             else
  46.                philosopherLabels[n].Invoke((MethodInvoker)(() => philosopherLabels[n].ForeColor = Color.Lime));
  47.         }
  48.  
  49.         // function to Change status of Forks (used, or not)
  50.         private void ChangeForkLabelColor(int n, bool eating)
  51.         {
  52.             if (eating)
  53.                 ForksLabels[n].Invoke((MethodInvoker)(() => ForksLabels[n].ForeColor = Color.Red));
  54.             else
  55.                 ForksLabels[n].Invoke((MethodInvoker)(() => ForksLabels[n].ForeColor = Color.Lime));
  56.         }
  57.  
  58.         // Picking left and right fork
  59.         private void Get(int left, int right, int n)
  60.         {
  61.             lock(this) // Using 'this' object as a lock to synchronize access to shared resources
  62.             {
  63.                 while (fork[left] || fork[right]) // While either fork is in use
  64.                 {
  65.                     Monitor.Wait(this); // Wait for notification to acquire both forks (waiting PulseAll(this)
  66.                 }
  67.                 fork[left] = true; // Pick up left fork
  68.                 fork[right] = true; // Pick up right fork
  69.                 ChangePhiLabelColor(n, true); //change Phi label Color to ``red`` (eating)
  70.                 ChangeForkLabelColor(left, true); // change left Fork color to ``red`` (used)
  71.                 ChangeForkLabelColor(right, true); // change right Fork color to ``red`` (used)
  72.             }
  73.         }
  74.  
  75.         // Putting down forks
  76.         private void Put(int left, int right, int n)
  77.         {
  78.             lock(this) // Using 'this' object as a lock to synchronize access to shared resources
  79.             {
  80.                 fork[left] = false; // put down left fork
  81.                 fork[right] = false; // put down right fork
  82.                 ChangePhiLabelColor(n, false); // change Phi label Color to ``green`` (not eating)
  83.                 ChangeForkLabelColor(left, false); // change left Fork color to ``green`` (not used)
  84.                 ChangeForkLabelColor(right, false); // change right Fork color to ``green`` (not used)
  85.                 Nasr.Invoke((MethodInvoker)(() => Nasr.AppendText($"Philosopher {n} is Thinnking...\n"))); // Output that the philosopher is thinking
  86.                 Monitor.PulseAll(this); // Notify waiting threads that forks are available (the Mointor.Wait(This) will continue)
  87.                 Thread.Sleep(4000); // sleep 4 secs for better visualization
  88.             }
  89.         }
  90.  
  91.         private void PhilosopherAction(int n, int thinkDelay, int eatDelay)
  92.         {
  93.             int left = n == 0 ? 4 : n - 1; // Index of the left fork
  94.             int right = n; // Index of the right fork
  95.  
  96.             new Thread(() =>
  97.             {
  98.                 while(true) // Infinite loop to (continuous actions of a philosopher)
  99.                 {
  100.                     try
  101.                     {
  102.                         Thread.Sleep(thinkDelay); // thinking time
  103.                         Get(left, right, n); // try to pick up forks
  104.                         Nasr.Invoke((MethodInvoker)(() => Nasr.AppendText($"Philosopher {n} is eating...\n"))); // Output that the philosopher is eating
  105.                         Thread.Sleep(eatDelay); // eating time
  106.                         Put(left, right, n); // put down forks after eating
  107.  
  108.                     }
  109.                     catch
  110.                     {
  111.                         return;
  112.                     }
  113.                 }
  114.             }).Start(); // Start the thread
  115.         }
  116.  
  117.         // run the dining philosophers problem
  118.         private void RunProblem()
  119.         {
  120.             PhilosopherAction(0, 4000, 7000); // Philosopher 0
  121.             PhilosopherAction(1, 4000, 7000); // Philosopher 1
  122.             PhilosopherAction(2, 4000, 7000); // Philosopher 2
  123.             PhilosopherAction(3, 4000, 7000); // Philosopher 3
  124.             PhilosopherAction(4, 4000, 7000); // Philosopher 4
  125.         }
  126.  
  127.         private void Form1_Load(object sender, EventArgs e)
  128.         {
  129.         }
  130.  
  131.         private void simpleButton1_Click(object sender, EventArgs e)
  132.         {
  133.             RunProblem(); // Start the dining philosophers simulation
  134.  
  135.         }
  136.  
  137.         private void simpleButton2_Click(object sender, EventArgs e)
  138.         {
  139.  
  140.         }
  141.  
  142.         private void Ph0_Click(object sender, EventArgs e)
  143.         {
  144.  
  145.         }
  146.     }
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment