Ejemplo n.º 1
0
        public void GiveForks(Fork leftFork, Fork rightFork, int philosopherNum, int thinkingTime, int eatingTime)
        {
            Console.WriteLine("\tPhilosopher {0} is speaking", philosopherNum);
            Thread.Sleep(thinkingTime * 1000);
            if (leftFork.IsChosen || rightFork.IsChosen)
            {
                Console.WriteLine("\tPhilosopher {0} is waiting for free forks", philosopherNum);
            }

            semaphore.WaitOne();

            lock (leftFork)
            {
                leftFork.IsChosen = true;
                //Console.WriteLine("\tPhilosopher {0} took fork {1}", philosopherNum, leftFork.Number);

                {
                    lock (rightFork)
                    {
                        leftFork.IsChosen = true;
                        //Console.WriteLine("\tPhilosopher {0} took fork {1}", philosopherNum, rightFork.Number);
                        Console.WriteLine("Philosopher {0} is eating", philosopherNum);
                        Thread.Sleep(eatingTime * 1000);
                    }
                    rightFork.IsChosen = false;
                    //Console.WriteLine("\tPhilosopher {0} released fork {1}", philosopherNum, rightFork.Number);
                }
            }
            leftFork.IsChosen = false;

            //Console.WriteLine("\tPhilosopher {0} released fork {1}", philosopherNum, leftFork.Number);

            semaphore.Release(1);
        }
Ejemplo n.º 2
0
 private void InitializeDinerState(int id, Fork leftFork, Fork rightFork)
 {
     ID        = id;
     LeftFork  = leftFork;
     RightFork = rightFork;
     State     = DinerState.TryingToGetForks;
 }
Ejemplo n.º 3
0
        private static double Dine(int index, Fork firstFork, Fork secondFork, int maxThinkingTime, int maxEatingTime,
                                   CancellationToken cancellationToken)
        {
            var watch = new Stopwatch();

            while (!cancellationToken.IsCancellationRequested)
            {
                var thinkingTime = RandomInRange(0, maxThinkingTime);
                Thread.Sleep(thinkingTime);
                Console.WriteLine($"Phil{index.ToString()} finished thinking");
                watch.Start();
                lock (firstFork)
                {
                    watch.Stop();
                    Console.WriteLine($"Phil{index.ToString()} took first fork: {firstFork.Position.ToString()}");
                    watch.Start();
                    lock (secondFork)
                    {
                        watch.Stop();
                        Console.WriteLine($"Phil{index.ToString()} took second fork: {secondFork.Position.ToString()}");
                        Thread.Sleep(RandomInRange(0, maxEatingTime));
                        Console.WriteLine($"Phil{index.ToString()} is done eating");
                    }
                }
            }

            return(watch.Elapsed.TotalMilliseconds);
        }
Ejemplo n.º 4
0
        public Philosopher(int nPhilospher, Fork left, Fork right)
        {
            this.nPhilospher = nPhilospher;

            firstFork  = left.Id < right.Id ? left : right;
            secondFork = left.Id < right.Id ? right : left;
        }
Ejemplo n.º 5
0
 public Philosopher(int number, Fork leftFork, Fork rightFork)
 {
     this.number    = number;
     this.leftFork  = leftFork;
     this.rightFork = rightFork;
     start();
 }
Ejemplo n.º 6
0
 public void Eat(Fork leftFork, Fork rightFork)
 {
     while (true)
     {
         Waiter.Instance.GiveForks(leftFork, rightFork, Number, SpeakingTime, EatingTime);
     }
 }
Ejemplo n.º 7
0
        void grabFork(Fork equipFork)
        {
            Random rnd   = new Random();
            int    tries = rnd.Next(10);

            Console.WriteLine($"philosoph {number} is trying to grab fork{equipFork.Id}, {tries} times");

            int counter = 0;

            while (counter < tries)
            {
                if (Monitor.TryEnter(equipFork))
                {
                    Console.WriteLine($"Philosopher {number} took fork {equipFork.Id}");
                    try
                    {
                        //Eat for a random amount of time
                        Eat();
                        Thread.Sleep(rnd.Next(3000));
                        break;
                    }
                    finally
                    {
                        Console.WriteLine($"Philosopher {number} let go of fork{equipFork.Id}");
                    }
                }
                else
                {
                    Console.WriteLine($"Philosopher {number} could not get fork {equipFork.Id}");
                }

                counter++;
                Thread.Sleep(2000);
            }
        }
Ejemplo n.º 8
0
        public Philosopher(int nPhilospher, Fork left, Fork right)
        {
            this.nPhilospher = nPhilospher;

            firstFork  = left;
            secondFork = right;
        }
Ejemplo n.º 9
0
        public Philosopher(int nPhilospher, Fork left, Fork right, SemaphoreSlim tableSemaphore)
        {
            this.tableSemaphore = tableSemaphore;
            this.nPhilospher    = nPhilospher;

            firstFork  = left.Id < right.Id ? left : right;
            secondFork = left.Id < right.Id ? right : left;
        }
Ejemplo n.º 10
0
 public Philosopher(int name, Fork leftFork, Fork rightFork, Philosopher allPhilosopher)
 {
     this.Name       = name;
     this._leftFork  = leftFork;
     this._rightFork = rightFork;
     //this._allPhilosophers = allPhilosopher;
     this._rnd = new Random(this.Name);
 }
Ejemplo n.º 11
0
        static public void Eat(Fork leftFork, Fork rightFork, int philosopherNumber, int leftForkNumber, int rightForkNumber)
        {
            while (true)
            {
                Stopwatch timePerParse2;
                long      ticksThisTime = 0;

                // philosopher thinks
                Random r             = new Random();
                int    thinking_time = r.Next(0, 5000);
                Console.WriteLine("Philosopher {0} thinks.", philosopherNumber);
                Thread.Sleep(thinking_time);

                timePerParse2 = Stopwatch.StartNew();
                Console.WriteLine("Philosopher {0} wants to take Forks.", philosopherNumber);

                Fork first      = leftFork;
                Fork second     = rightFork;
                int  firstFork  = leftForkNumber;
                int  secondFork = rightForkNumber;

                int even = philosopherNumber % 2;

                // switch forks
                if (even == 0)
                {
                    first      = rightFork;
                    firstFork  = rightForkNumber;
                    second     = leftFork;
                    secondFork = leftForkNumber;
                }

                lock (first)
                {
                    timePerParse2.Stop();
                    ticksThisTime = timePerParse2.ElapsedTicks;
                    Console.WriteLine("Philosopher {0} was waiting for " + ticksThisTime + "ms", philosopherNumber);

                    Console.WriteLine("Philosopher {0} picked {1} Fork.", philosopherNumber, firstFork);

                    lock (second)
                    {
                        // philosopher eats
                        Console.WriteLine("Philosopher {0} picked {1} Fork.", philosopherNumber, secondFork);
                        Console.WriteLine("Philosopher {0} eats.", philosopherNumber);

                        int eating_time = r.Next(0, 10000);
                        Thread.Sleep(eating_time);
                        Console.WriteLine("Philosopher {0} stops eating.", philosopherNumber);
                    }
                    Console.WriteLine("Philosopher {0} released {1} Fork.", philosopherNumber, secondFork);
                }
                Console.WriteLine("Philosopher {0} released {1} Fork.", philosopherNumber, firstFork);
            }
        }
Ejemplo n.º 12
0
 public Philosopher(int index, string name, TimeSpan thinkingTime, TimeSpan eatingTime, Fork leftFork, Fork rightFork)
 {
     _index        = index;
     _thinkingTime = thinkingTime;
     _eatingTime   = eatingTime;
     _leftFork     = leftFork;
     _rightFork    = rightFork;
     Name          = name;
     _random       = new Random();
     _thread       = new Thread(DinnerLoop);
     _thread.Start();
 }
Ejemplo n.º 13
0
 public void Start()
 {
     Console.WriteLine("Start process");
     for (int i = 0; i < philosophersNum; i++)
     {
         int    ix        = i;
         Fork   leftFork  = forks[ix];
         Fork   rightFork = (ix == 0) ? forks[philosophersNum - 1] : forks[ix - 1];
         Thread thread    = new Thread(() => philosophers[ix].Eat(leftFork, rightFork));
         threads.Add(thread);
         thread.Start();
     }
 }
Ejemplo n.º 14
0
 public DiningProcess(int philNumb)
 {
     philosophersNum = philNumb;
     forks           = new List <Fork>();
     philosophers    = new List <Philosopher>();
     threads         = new List <Thread>();
     for (int i = 0; i < philosophersNum; i++)
     {
         Philosopher philosopher = new Philosopher(i);
         philosophers.Add(philosopher);
         Fork fork = new Fork(i);
         forks.Add(fork);
     }
     Waiter.Instance.InitWaiter(forks);
 }
Ejemplo n.º 15
0
        public static void Eat(Fork leftFork, Fork rightFork, int philosopherNumber, int leftForkNumber, int rightForkNumber)
        {
            while (true)
            {
                // philosopher thinks
                Random r            = new Random();
                int    thinkingTime = r.Next(0, 5000);
                Console.WriteLine($"Philosopher {philosopherNumber} thinks.");
                Thread.Sleep(thinkingTime);
                Console.WriteLine($"Philosopher {philosopherNumber} wants to take Forks.");

                Fork first      = leftFork;
                Fork second     = rightFork;
                int  firstFork  = leftForkNumber;
                int  secondFork = rightForkNumber;

                bool isEven = philosopherNumber % 2 == 0;

                // switch forks to prevent deadlock
                if (isEven)
                {
                    first      = rightFork;
                    firstFork  = rightForkNumber;
                    second     = leftFork;
                    secondFork = leftForkNumber;
                }

                lock (first)
                {
                    Console.WriteLine("Philosopher {0} picked {1} Fork.", philosopherNumber, firstFork);

                    // not needed because of deadlock prevention above
                    //lock (second)
                    //{

                    // philosopher eats
                    Console.WriteLine("Philosopher {0} picked {1} Fork.", philosopherNumber, secondFork);
                    Console.WriteLine("Philosopher {0} eats.", philosopherNumber);

                    int eating_time = r.Next(0, 10000);
                    Thread.Sleep(eating_time); // TODO: do not use Thread.Sleep() inside a task -> pauses ALL tasks on the thread (could be multiple)
                    Console.WriteLine("Philosopher {0} stops eating.", philosopherNumber);
                    //}
                    Console.WriteLine("Philosopher {0} released {1} Fork.", philosopherNumber, secondFork);
                }
                Console.WriteLine("Philosopher {0} released {1} Fork.", philosopherNumber, firstFork);
            }
        }
Ejemplo n.º 16
0
        static public void Eat(Fork leftFork, Fork rightFork, int philosopherNumber, int leftForkNumber, int rightForkNumber)
        {
            while (true)
            {
                // philosopher thinks
                Random r             = new Random();
                int    thinking_time = r.Next(0, 5000);
                Console.WriteLine("Philosopher {0} thinks.", philosopherNumber);
                Thread.Sleep(thinking_time);
                Console.WriteLine("Philosopher {0} wants to take Forks.", philosopherNumber);

                Fork first      = leftFork;
                Fork second     = rightFork;
                int  firstFork  = leftForkNumber;
                int  secondFork = rightForkNumber;

                int even = philosopherNumber % 2;

                // switch forks to prevent deadlock
                if (even == 0)
                {
                    first      = rightFork;
                    firstFork  = rightForkNumber;
                    second     = leftFork;
                    secondFork = leftForkNumber;
                }

                lock (first)
                {
                    Console.WriteLine("Philosopher {0} picked {1} Fork.", philosopherNumber, firstFork);

                    // not needed because of deadlock prevention above
                    //lock (second)
                    //{

                    // philosopher eats
                    Console.WriteLine("Philosopher {0} picked {1} Fork.", philosopherNumber, secondFork);
                    Console.WriteLine("Philosopher {0} eats.", philosopherNumber);

                    int eating_time = r.Next(0, 10000);
                    Thread.Sleep(eating_time);
                    Console.WriteLine("Philosopher {0} stops eating.", philosopherNumber);
                    //}
                    Console.WriteLine("Philosopher {0} released {1} Fork.", philosopherNumber, secondFork);
                }
                Console.WriteLine("Philosopher {0} released {1} Fork.", philosopherNumber, firstFork);
            }
        }
Ejemplo n.º 17
0
        public DiningTable(int nPhilosophers)
        {
            Fork[] forks = new Fork[nPhilosophers];

            for (int nFork = 0; nFork < nPhilosophers; nFork++)
            {
                forks[nFork] = new Fork(nFork);
            }

            philosophers = new Philosopher[nPhilosophers];

            for (int nPhilospher = 0; nPhilospher < philosophers.Length; nPhilospher++)
            {
                philosophers[nPhilospher] = new Philosopher(nPhilospher, forks[nPhilospher], forks[(nPhilospher + 1) % nPhilosophers]);
            }
        }
Ejemplo n.º 18
0
        public Table(int number)
        {
            this.NumOfPhilosophers = number;
            this.philosophers      = new Philosopher[NumOfPhilosophers];
            this.forks             = new Fork[NumOfPhilosophers];

            for (int i = 0; i < forks.Length; i++)
            {
                forks[i] = new Fork(i);
            }

            for (int i = 0; i < NumOfPhilosophers; i++)
            {
                Fork right = forks[i];
                Fork left  = forks[(i + 1) % NumOfPhilosophers];
                philosophers[i] = new Philosopher(i, left, right);
            }
        }
Ejemplo n.º 19
0
        public DiningTable(int nPhilosophers)
        {
            Fork[] forks = new Fork[nPhilosophers];

            for (int nFork = 0; nFork < nPhilosophers; nFork++)
            {
                forks[nFork] = new Fork(nFork);
            }

            philosophers = new Philosopher[nPhilosophers];

            SemaphoreSlim tableSemaphore = new SemaphoreSlim(nPhilosophers / 2);

            for (int nPhilospher = 0; nPhilospher < philosophers.Length; nPhilospher++)
            {
                philosophers[nPhilospher] = new Philosopher(nPhilospher, forks[nPhilospher], forks[(nPhilospher + 1) % nPhilosophers], tableSemaphore);
            }
        }
Ejemplo n.º 20
0
        public TableViewModel()
        {
            PhilosopherThinkingTime = "2000";

            fork1 = new Fork();
            fork2 = new Fork();
            fork3 = new Fork();
            fork4 = new Fork();
            fork5 = new Fork();

            philosopher1 = new Philosopher("1", fork1, fork2);
            philosopher2 = new Philosopher("2", fork2, fork3);
            philosopher3 = new Philosopher("3", fork3, fork4);
            philosopher4 = new Philosopher("4", fork4, fork5);
            philosopher5 = new Philosopher("5", fork5, fork1);

            this.RunCommand  = new RelayCommand(this.OnRun, this.CanRun);
            this.StopCommand = new RelayCommand(this.OnStop, this.CanStop);
        }
Ejemplo n.º 21
0
 public static void Init()
 {
     Philosopher[] tmpP = new Philosopher[numberOfPhilosophers];
     Fork[]        tmpF = new Fork[numberOfPhilosophers];
     for (int i = 0; i < numberOfPhilosophers; i++)
     {
         tmpF[i] = Fork.Create();
     }
     for (int i = 0; i < numberOfPhilosophers; i++)
     {
         Philosopher p = Philosopher.Create();
         p.left  = tmpF[i];
         p.right = tmpF[(i + 1) % numberOfPhilosophers];
         tmpP[i] = p;
     }
     for (int i = 0; i < numberOfPhilosophers; i++)
     {
         phils = phils.Add(tmpP[i]);
     }
     mode = Mode.Running;
 }
Ejemplo n.º 22
0
 public Philosopher(string name, Fork left, Fork right)
 {
     this.name  = name;
     this.left  = left;
     this.right = right;
 }
Ejemplo n.º 23
0
 public Philosopher(string name, Fork leftfork, Fork rightfork)
 {
     this.Name = name;
     leftFork  = leftfork;
     rightFork = rightfork;
 }
Ejemplo n.º 24
0
 private static int GetForkHolder(Fork fork)
 {
     return(Diners.Single(d => d.CurrentlyHeldForks.Contains(fork)).ID);
 }
Ejemplo n.º 25
0
 private static bool ForkIsBeingUsed(Fork fork)
 {
     return(Diners.Count(d => d.CurrentlyHeldForks.Contains(fork)) > 0);
 }
Ejemplo n.º 26
0
 private static string FormatForkState(Fork fork)
 {
     return(!ForkIsBeingUsed(fork) ? "  " : "D" + GetForkHolder(fork));
 }
Ejemplo n.º 27
0
 public Philosopher(string name, Fork leftfork, Fork rightfork)
 {
     this.Name = name;
     leftFork = leftfork;
     rightFork = rightfork;
 }
Ejemplo n.º 28
0
 public Philosopher(int number, Fork left, Fork right)
 {
     this.Number = number;
     this.left   = left;
     this.right  = right;
 }
Ejemplo n.º 29
0
 public Philosopher(string name, Fork leftFork, Fork rightFork)
 {
     Name      = name;
     LeftFork  = leftFork;
     RightFork = rightFork;
 }
Ejemplo n.º 30
0
 public Diner(int id, Fork leftFork, Fork rightFork)
 {
     InitializeDinerState(id, leftFork, rightFork);
     BeginDinerActivity();
 }