IMohammedNasr

Untitled

Dec 14th, 2023
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.96 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.  
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         // Picking left and right fork
  25.         private void Get(int left, int right, int n)
  26.         {
  27.             lock(this) // Using 'this' object as a lock to synchronize access to shared resources
  28.             {
  29.                 while (fork[left] || fork[right]) // While either fork is in use
  30.                 {
  31.                     Nasr.Invoke((MethodInvoker)(() => Nasr.AppendText($"Philosopher {n} is thinking...\n"))); // Output that the philosopher is thinking
  32.                     Monitor.Wait(this); // Wait for notification to acquire both forks (waiting PulseAll(this)
  33.                 }
  34.                 fork[left] = true; // Pick up left fork
  35.                 fork[right] = true; // Pick up right fork
  36.             }
  37.         }
  38.  
  39.         // Putting down forks
  40.         private void Put(int left, int right, int n)
  41.         {
  42.             lock(this) // Using 'this' object as a lock to synchronize access to shared resources
  43.             {
  44.                 fork[left] = false; // put down left fork
  45.                 fork[right] = false; // put down right fork
  46.                 Nasr.Invoke((MethodInvoker)(() => Nasr.AppendText($"Philosopher {n} stopped eating...\n"))); // Output that the philosopher stopped eating
  47.                 Monitor.PulseAll(this); // Notify waiting threads that forks are available (the Mointor.Wait(This) will continue)
  48.             }
  49.         }
  50.  
  51.         private void PhilosopherAction(int n, int thinkDelay, int eatDelay)
  52.         {
  53.             int left = n == 0 ? 4 : n - 1; // Index of the left fork
  54.             int right = n; // Index of the right fork
  55.  
  56.             new Thread(() =>
  57.             {
  58.                 while(true) // Infinite loop to (continuous actions of a philosopher)
  59.                 {
  60.                     try
  61.                     {
  62.                         Thread.Sleep(thinkDelay); // thinking time
  63.                         Get(left, right, n); // try to pick up forks
  64.                         Nasr.Invoke((MethodInvoker)(() => Nasr.AppendText($"Philosopher {n} is eating...\n"))); // Output that the philosopher is eating
  65.                         Thread.Sleep(eatDelay); // eating time
  66.                         Put(left, right, n); // put down forks after eating
  67.                     }
  68.                     catch
  69.                     {
  70.                         return;
  71.                     }
  72.                 }
  73.             }).Start(); // Start the thread
  74.         }
  75.  
  76.         // run the dining philosophers problem
  77.         private void RunProblem()
  78.         {
  79.             PhilosopherAction(0, 5000, 5000); // Philosopher 0
  80.             PhilosopherAction(1, 5000, 5000); // Philosopher 1
  81.             PhilosopherAction(2, 5000, 5000); // Philosopher 2
  82.             PhilosopherAction(3, 5000, 5000); // Philosopher 3
  83.             PhilosopherAction(4, 5000, 5000); // Philosopher 4
  84.         }
  85.  
  86.         private void Form1_Load(object sender, EventArgs e)
  87.         {
  88.         }
  89.  
  90.         private System.Threading.Timer timer;
  91.         private void simpleButton1_Click(object sender, EventArgs e)
  92.         {
  93.             RunProblem(); // Start the dining philosophers simulation
  94.             // calling the timer function that will excute every 5.1 secs
  95.             timer = new System.Threading.Timer(TimerCallback, null, 5100, 5100);
  96.         }
  97.         // The Timer function that adds newline
  98.         private void TimerCallback(Object o)
  99.         {
  100.             Nasr.Invoke((MethodInvoker)(() => Nasr.AppendText($"\n")));
  101.         }
  102.     }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment