Facebook
From Unreliable Marmoset, 1 Week ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 58
  1. using System;
  2. using System.Threading;
  3.  
  4.  
  5.     public class FilozofExample
  6.     {
  7.     // Define semaphores    
  8.     private static Semaphore çatal0, çatal1, çatal2, çatal3, çatal4;
  9.  
  10.     public static void Main()
  11.     {
  12.  
  13.         // Initialize semaphores
  14.         çatal0 = new Semaphore(1, 1);
  15.         çatal1 = new Semaphore(1, 1);
  16.         çatal2 = new Semaphore(1, 1);
  17.         çatal3 = new Semaphore(1, 1);
  18.         çatal4 = new Semaphore(1, 1);
  19.  
  20.         // Define threads
  21.         // Create and start thread Filozof 0
  22.  
  23.         System.Threading.Thread Filozof0 = new System.Threading.Thread(new System.Threading.ThreadStart(Filozof0Exe));
  24.         Filozof0.Name = "|Filozof0|";
  25.         Filozof0.Start();
  26.         System.Threading.Thread Filozof1 = new System.Threading.Thread(new System.Threading.ThreadStart(Filozof1Exe));
  27.         Filozof1.Name = "|Filozof1|";
  28.         Filozof1.Start();
  29.         System.Threading.Thread Filozof2 = new System.Threading.Thread(new System.Threading.ThreadStart(Filozof2Exe));
  30.         Filozof2.Name = "|Filozof2|";
  31.         Filozof2.Start();
  32.         System.Threading.Thread Filozof3 = new System.Threading.Thread(new System.Threading.ThreadStart(Filozof3Exe));
  33.         Filozof3.Name = "|Filozof3|";
  34.         Filozof3.Start();
  35.         System.Threading.Thread Filozof4 = new System.Threading.Thread(new System.Threading.ThreadStart(Filozof4Exe));
  36.         Filozof4.Name = "|Filozof4|";
  37.         Filozof4.Start();
  38.  
  39.         // Add code for other philosophers
  40.     }
  41.     private static void Filozof0Exe()
  42.     {
  43.         çatal1.WaitOne();
  44.         Console.WriteLine("Filozof 0 has çatal 0");
  45.         System.Threading.Thread.Sleep(10);
  46.         çatal0.WaitOne();
  47.         Console.WriteLine("Filozof 0 has çatal 1");
  48.         Console.WriteLine("Filozof 0 is eating..........");
  49.         System.Threading.Thread.Sleep(100);
  50.         çatal0.Release();
  51.         çatal1.Release();
  52.         Console.WriteLine("Filozof 0 released çatal 0 and 1");
  53.         Console.WriteLine("..........Filozof 0 is thinking");
  54.         System.Threading.Thread.Sleep(100);
  55.        
  56.     }
  57.     private static void Filozof1Exe()
  58.     {
  59.         çatal1.WaitOne();
  60.         Console.WriteLine("Filozof 1 has çatal 1");
  61.         System.Threading.Thread.Sleep(10);
  62.         çatal2.WaitOne();
  63.         Console.WriteLine("Filozof 1 has çatal 1");
  64.         Console.WriteLine("Filozof 1 is eating..........");
  65.         System.Threading.Thread.Sleep(100);
  66.         çatal1.Release();
  67.         çatal2.Release();
  68.         Console.WriteLine("Filozof 1 released çatal 1 and 0");
  69.         Console.WriteLine("..........Filozof 1 is thinking");
  70.         System.Threading.Thread.Sleep(100);
  71.     }
  72.     private static void Filozof2Exe()
  73.     {
  74.         çatal2.WaitOne();
  75.         Console.WriteLine("Filozof 2 has çatal 2");
  76.         System.Threading.Thread.Sleep(10);
  77.         çatal3.WaitOne();
  78.         Console.WriteLine("Filozof 2 has çatal 2");
  79.         Console.WriteLine("Filozof 2 is eating..........");
  80.         System.Threading.Thread.Sleep(100);
  81.         çatal2.Release();
  82.         çatal3.Release();
  83.         Console.WriteLine("Filozof 2 released çatal 2 and 1");
  84.         Console.WriteLine("..........Filozof 2 is thinking");
  85.         System.Threading.Thread.Sleep(100);
  86.     }
  87.     private static void Filozof3Exe()
  88.     {
  89.         çatal3.WaitOne();
  90.         Console.WriteLine("Filozof 3 has çatal 3");
  91.         System.Threading.Thread.Sleep(10);
  92.         çatal4.WaitOne();
  93.         Console.WriteLine("Filozof 3 has çatal 2");
  94.         Console.WriteLine("Filozof 3 is eating..........");
  95.         System.Threading.Thread.Sleep(100);
  96.         çatal3.Release();
  97.         çatal4.Release();
  98.         Console.WriteLine("Filozof 3 released çatal 3 and 2");
  99.         Console.WriteLine("..........Filozof 3 is thinking");
  100.         System.Threading.Thread.Sleep(100);
  101.     }
  102.     private static void Filozof4Exe()
  103.     {
  104.         çatal4.WaitOne();
  105.         Console.WriteLine("Filozof 4 has çatal 4");
  106.         System.Threading.Thread.Sleep(10);
  107.         çatal0.WaitOne();
  108.         Console.WriteLine("Filozof 4 has çatal 0");
  109.         Console.WriteLine("Filozof 4 is eating..........");
  110.         System.Threading.Thread.Sleep(100);
  111.         çatal4.Release();
  112.         çatal0.Release();
  113.         Console.WriteLine("Filozof 4 released çatal 4 and 0");
  114.         Console.WriteLine("..........Filozof 4 is thinking");
  115.         System.Threading.Thread.Sleep(100);
  116.     }
  117. }