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