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