- using System;
- using System.Threading;
- namespace ConsoleApp4
- {
- internal class Program
- {
- public class FilozofExample
- {
- // Define semaphoresprivate
- static Semaphore çatal0, çatal1, çatal2, çatal3, çatal4;
- public static void Main()
- {
- çatal0 = new Semaphore(1, 1);
- çatal1 = new Semaphore(1, 1);
- çatal2 = new Semaphore(1, 1);
- çatal3 = new Semaphore(1, 1);
- çatal4 = new Semaphore(1, 1);
- // Define threads//
- //Create and start thread Filozof 0
- for (int i = 0; i < 5; i++)
- {
- System.Threading.Thread Filozof0 = new System.Threading.Thread(new System.Threading.ThreadStart(Filozof0Exe));
- Filozof0.Name = "|Filozof0|";
- Filozof0.Start();
- System.Threading.Thread Filozof4 = new System.Threading.Thread(new System.Threading.ThreadStart(Filozof4Exe));
- Filozof4.Name = "|Filozof4|";
- Filozof4.Start();
- }
- System.Threading.Thread Filozof3 = new System.Threading.Thread(new System.Threading.ThreadStart(Filozof3Exe));
- Filozof3.Name = "|Filozof3|";
- Filozof3.Start();
- System.Threading.Thread Filozof2 = new System.Threading.Thread(new System.Threading.ThreadStart(Filozof2Exe));
- Filozof2.Name = "|Filozof2|";
- Filozof2.Start();
- System.Threading.Thread Filozof1 = new System.Threading.Thread(new System.Threading.ThreadStart(Filozof1Exe));
- Filozof1.Name = "|Filozof1|";
- Filozof1.Start();
- }
- private static void Filozof0Exe()
- {
- çatal1.WaitOne();
- Console.WriteLine("Filozof 0 has çatal 0");
- System.Threading.Thread.Sleep(10);
- çatal0.WaitOne();
- Console.WriteLine("Filozof 0 has çatal 1");
- Console.WriteLine("Filozof 0 is eating..........");
- System.Threading.Thread.Sleep(100);
- çatal0.Release();
- çatal1.Release();
- Console.WriteLine("Filozof 0 released çatal 0 and 1");
- Console.WriteLine("..........Filozof 0 is thinking");
- System.Threading.Thread.Sleep(100);
- }
- private static void Filozof1Exe()
- {
- çatal1.WaitOne();
- Console.WriteLine("Filozof 1 has çatal 1");
- System.Threading.Thread.Sleep(10);
- çatal2.WaitOne();
- Console.WriteLine("Filozof 1 has çatal 1");
- Console.WriteLine("Filozof 1 is eating..........");
- System.Threading.Thread.Sleep(100);
- çatal1.Release();
- çatal2.Release();
- Console.WriteLine("Filozof 1 released çatal 1 and 0");
- Console.WriteLine("..........Filozof 1 is thinking");
- System.Threading.Thread.Sleep(100);
- }
- private static void Filozof2Exe()
- {
- çatal2.WaitOne();
- Console.WriteLine("Filozof 2 has çatal 2");
- System.Threading.Thread.Sleep(10);
- çatal3.WaitOne();
- Console.WriteLine("Filozof 2 has çatal 2");
- Console.WriteLine("Filozof 2 is eating..........");
- System.Threading.Thread.Sleep(100);
- çatal2.Release();
- çatal3.Release();
- Console.WriteLine("Filozof 2 released çatal 2 and 1");
- Console.WriteLine("..........Filozof 2 is thinking");
- System.Threading.Thread.Sleep(100);
- }
- private static void Filozof3Exe()
- {
- çatal3.WaitOne();
- Console.WriteLine("Filozof 3 has çatal 3");
- System.Threading.Thread.Sleep(10);
- çatal4.WaitOne();
- Console.WriteLine("Filozof 3 has çatal 2");
- Console.WriteLine("Filozof 3 is eating..........");
- System.Threading.Thread.Sleep(100);
- çatal3.Release();
- çatal4.Release();
- Console.WriteLine("Filozof 3 released çatal 3 and 2");
- Console.WriteLine("..........Filozof 3 is thinking");
- System.Threading.Thread.Sleep(100);
- }
- private static void Filozof4Exe()
- {
- çatal4.WaitOne();
- Console.WriteLine("Filozof 4 has çatal 4");
- System.Threading.Thread.Sleep(10);
- çatal0.WaitOne();
- Console.WriteLine("Filozof 4 has çatal 0");
- Console.WriteLine("Filozof 4 is eating..........");
- System.Threading.Thread.Sleep(100);
- çatal4.Release();
- çatal0.Release();
- Console.WriteLine("Filozof 4 released çatal 4 and 0");
- Console.WriteLine("..........Filozof 4 is thinking");
- System.Threading.Thread.Sleep(100);
- }
- }
- }
- }