Facebook
From Mammoth Tern, 2 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 97
  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.             public static void Main()
  13.             {
  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.                 for (int i = 0; i < 2; i++)
  24.                 {
  25.                     System.Threading.Thread Filozof0 = new System.Threading.Thread(new System.Threading.ThreadStart(Filozof0Exe));
  26.                     Filozof0.Name = "|Filozof0|";
  27.                     Filozof0.Start();
  28.  
  29.                     System.Threading.Thread Filozof4 = new System.Threading.Thread(new System.Threading.ThreadStart(Filozof4Exe));
  30.                     Filozof4.Name = "|Filozof4|";
  31.                     Filozof4.Start();
  32.  
  33.                     System.Threading.Thread Filozof3 = new System.Threading.Thread(new System.Threading.ThreadStart(Filozof3Exe));
  34.                     Filozof3.Name = "|Filozof3|";
  35.                     Filozof3.Start();
  36.  
  37.                     System.Threading.Thread Filozof2 = new System.Threading.Thread(new System.Threading.ThreadStart(Filozof2Exe));
  38.                     Filozof2.Name = "|Filozof2|";
  39.                     Filozof2.Start();
  40.  
  41.                     System.Threading.Thread Filozof1 = new System.Threading.Thread(new System.Threading.ThreadStart(Filozof1Exe));
  42.                     Filozof1.Name = "|Filozof1|";
  43.                     Filozof1.Start();
  44.                 }
  45.             }
  46.             private static void Filozof0Exe()
  47.             {
  48.                 çatal1.WaitOne();
  49.                 Console.WriteLine("Filozof 0 has çatal 0");
  50.                 System.Threading.Thread.Sleep(10);
  51.                 çatal0.WaitOne();
  52.                 Console.WriteLine("Filozof 0 has çatal 1");
  53.                 Console.WriteLine("Filozof 0 is eating..........");
  54.                 System.Threading.Thread.Sleep(100);
  55.                 çatal0.Release();
  56.                 çatal1.Release();
  57.                 Console.WriteLine("Filozof 0 released çatal 0 and 1");
  58.                 Console.WriteLine("..........Filozof 0 is thinking");
  59.                 System.Threading.Thread.Sleep(100);
  60.  
  61.             }
  62.             private static void Filozof1Exe()
  63.             {
  64.                 çatal1.WaitOne();
  65.                 Console.WriteLine("Filozof 1 has çatal 1");
  66.                 System.Threading.Thread.Sleep(10);
  67.                 çatal2.WaitOne();
  68.                 Console.WriteLine("Filozof 1 has çatal 1");
  69.                 Console.WriteLine("Filozof 1 is eating..........");
  70.                 System.Threading.Thread.Sleep(100);
  71.                 çatal1.Release();
  72.                 çatal2.Release();
  73.                 Console.WriteLine("Filozof 1 released çatal 1 and 0");
  74.                 Console.WriteLine("..........Filozof 1 is thinking");
  75.                 System.Threading.Thread.Sleep(100);
  76.             }
  77.             private static void Filozof2Exe()
  78.             {
  79.                 çatal2.WaitOne();
  80.                 Console.WriteLine("Filozof 2 has çatal 2");
  81.                 System.Threading.Thread.Sleep(10);
  82.                 çatal3.WaitOne();
  83.                 Console.WriteLine("Filozof 2 has çatal 2");
  84.                 Console.WriteLine("Filozof 2 is eating..........");
  85.                 System.Threading.Thread.Sleep(100);
  86.                 çatal2.Release();
  87.                 çatal3.Release();
  88.                 Console.WriteLine("Filozof 2 released çatal 2 and 1");
  89.                 Console.WriteLine("..........Filozof 2 is thinking");
  90.                 System.Threading.Thread.Sleep(100);
  91.             }
  92.             private static void Filozof3Exe()
  93.             {
  94.                 çatal3.WaitOne();
  95.                 Console.WriteLine("Filozof 3 has çatal 3");
  96.                 System.Threading.Thread.Sleep(10);
  97.                 çatal4.WaitOne();
  98.                 Console.WriteLine("Filozof 3 has çatal 2");
  99.                 Console.WriteLine("Filozof 3 is eating..........");
  100.                 System.Threading.Thread.Sleep(100);
  101.                 çatal3.Release();
  102.                 çatal4.Release();
  103.                 Console.WriteLine("Filozof 3 released çatal 3 and 2");
  104.                 Console.WriteLine("..........Filozof 3 is thinking");
  105.                 System.Threading.Thread.Sleep(100);
  106.             }
  107.             private static void Filozof4Exe()
  108.             {
  109.                 çatal4.WaitOne();
  110.                 Console.WriteLine("Filozof 4 has çatal 4");
  111.                 System.Threading.Thread.Sleep(10);
  112.                 çatal0.WaitOne();
  113.                 Console.WriteLine("Filozof 4 has çatal 0");
  114.                 Console.WriteLine("Filozof 4 is eating..........");
  115.                 System.Threading.Thread.Sleep(100);
  116.                 çatal4.Release();
  117.                 çatal0.Release();
  118.                 Console.WriteLine("Filozof 4 released çatal 4 and 0");
  119.                 Console.WriteLine("..........Filozof 4 is thinking");
  120.                 System.Threading.Thread.Sleep(100);
  121.             }
  122.         }
  123.     }
  124. }
  125.