Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.76 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Collections.Generic;
  4.  
  5. namespace philosophers_os
  6. {
  7.     public class Fork
  8.     {
  9.         private Mutex m = new Mutex();
  10.  
  11.         public void take(){
  12.             m.WaitOne();
  13.         }
  14.  
  15.         public void put() {
  16.             m.ReleaseMutex();
  17.         }
  18.     };
  19.  
  20.     public class Philosopher
  21.     {
  22.         public int id;
  23.         public Fork leftFork;
  24.         public Fork righFork;
  25.         public uint eatCount;
  26.         public double waitTime;
  27.         public DateTime waitStart;
  28.         public bool stopFlag;
  29.         public bool debugFlag;
  30.         Random random;
  31.  
  32.         public bool fL = false;
  33.         public bool fR = false;
  34.  
  35.         void think()
  36.         {
  37.             if (this.debugFlag){
  38.                 Console.WriteLine(this.id + " thinking");
  39.             }
  40.  
  41.             Thread.Sleep(this.random.Next(0, 100));
  42.  
  43.             if (this.debugFlag){
  44.                 Console.WriteLine(this.id + " hungry");
  45.             }
  46.  
  47.             this.waitStart = DateTime.Now;
  48.         }
  49.  
  50.         void eat()
  51.         {
  52.             this.waitTime += DateTime.Now.Subtract(this.waitStart).TotalMilliseconds;
  53.             if (this.debugFlag){
  54.                 Console.WriteLine(this.id + " eating");
  55.             }
  56.  
  57.             Thread.Sleep(this.random.Next(0, 100));
  58.  
  59.             eatCount++;
  60.         }
  61.  
  62.         public Philosopher(int number, Fork left, Fork right, bool dbg)
  63.         {
  64.             this.id = number;
  65.             this.leftFork = left;
  66.             this.righFork = right;
  67.             this.eatCount = 0;
  68.             this.waitTime = 0;
  69.             this.debugFlag = dbg;
  70.             this.stopFlag = false;
  71.             this.random = new Random();
  72.  
  73.            
  74.         }
  75.  
  76.         public void run()
  77.         {
  78.             while (!stopFlag)
  79.             {
  80.                 think();
  81.  
  82.  
  83.                 if (Waiter.WantFork(this, Side.Left))
  84.                 {
  85.  
  86.                     //Waiter.freeForks--;
  87.                     this.leftFork.take();
  88.                     fL = true;
  89.                     if (this.debugFlag)
  90.                     {
  91.                         Console.WriteLine(this.id + " took left fork");
  92.                     }
  93.                 }
  94.                 else {
  95.                     Console.WriteLine("-------------------------");
  96.                 }
  97.  
  98.                 if (Waiter.WantFork(this, Side.Right))
  99.                 {
  100.                     //Waiter.freeForks--;
  101.                     this.righFork.take();
  102.                     fR = true;
  103.                     if (this.debugFlag)
  104.                     {
  105.                         Console.WriteLine(this.id + " took right fork");
  106.                     }
  107.                 }
  108.                 if (fR && fL)
  109.                     eat();
  110.  
  111.                 if (fR)
  112.                 {
  113.                     this.righFork.put();
  114.                     Waiter.freeForks++;
  115.                     fR = false;
  116.                     //Waiter.PutFork(this, Side.Right);
  117.                     if (this.debugFlag)
  118.                     {
  119.                         Console.WriteLine(this.id + " put right fork");
  120.                     }
  121.  
  122.                 }
  123.  
  124.                 if (fL)
  125.                 {
  126.                     this.leftFork.put();
  127.                     Waiter.freeForks++;
  128.                     fL = false;
  129.                     //Waiter.PutFork(this, Side.Left);
  130.                     if (this.debugFlag)
  131.                     {
  132.                         Console.WriteLine(this.id + " put left fork");
  133.                     }
  134.                    
  135.                 }
  136.             }
  137.         }
  138.  
  139.         public void stop(){
  140.             stopFlag = true;
  141.         }
  142.  
  143.         public void printStats(){
  144.             Console.WriteLine(this.id + " " + this.eatCount + " " + Convert.ToInt32(this.waitTime));
  145.         }
  146.     };
  147.  
  148.  
  149.     public static class Waiter
  150.     {
  151.  
  152.         public static int freeForks = 0;
  153.  
  154.  
  155.         public static void WaiterRem(int count) {
  156.             freeForks = count;
  157.         }
  158.  
  159.  
  160.         public static bool WantFork(Philosopher philosopher, Side side)
  161.         {
  162.             bool perm = false;
  163.             if (freeForks > 1)
  164.             {
  165.                 if (side == Side.Left)
  166.                 freeForks--;
  167.                 Console.WriteLine("Waiter: YES)))");
  168.                 return true;
  169.             }
  170.             Console.WriteLine("Waiter: NO(");
  171.  
  172.             return perm;
  173.         }
  174.  
  175.         public static void PutFork(Philosopher philosopher, Side side)
  176.         {
  177.             freeForks++;
  178.         }
  179.     }
  180.  
  181.     public enum Side{
  182.         Left = 0,
  183.         Right = 1
  184.     }
  185.  
  186.     class Program
  187.     {
  188.         static void Main(string[] args)
  189.         {
  190.             int N = 5;
  191.             bool dbg = true;
  192.             int duration = 60000;
  193.  
  194.             Fork[] forks = new Fork[N];
  195.             for (int i = 0; i < N; i++){
  196.                 forks[i] = new Fork();
  197.             }
  198.  
  199.             Philosopher[] phils = new Philosopher[N];
  200.             for (int i = 0; i < N; i++){
  201.                 phils[i] = new Philosopher(i + 1, forks[(i + 1) % N], forks[i], dbg);
  202.             }
  203.  
  204.             //Waiter.WaiterRem(phils, forks);
  205.             Waiter.WaiterRem(forks.Length);
  206.  
  207.             Thread[] runners = new Thread[N];
  208.             for (int i = 0; i < N; i++){
  209.                 runners[i] = new Thread(phils[i].run);
  210.             }
  211.             for (int i = 0; i < N; i++){
  212.                 runners[i].Start();
  213.             }
  214.  
  215.             Thread.Sleep(duration);
  216.  
  217.             for (int i = 0; i < N; i++){
  218.                 phils[i].stop();
  219.             }
  220.  
  221.             for (int i = 0; i < N; i++){
  222.                 runners[i].Join();
  223.             }
  224.  
  225.             for (int i = 0; i < N; i++){
  226.                 phils[i].printStats();
  227.             }
  228.         }
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement