Example #1
0
        static void Main(string[] args)
        {
            var sticks = new Chopstick[5];
            for (int i = 0; i < 5; i++)
            {
                sticks[i] = new Chopstick();
            }

            var philosophers = new Philosopher[5];
            for (int i = 0; i < 5; i++)
            {
                philosophers[i] = new Philosopher(sticks[i],sticks[(i + 1) % 5],(i + 1).ToString());
            }

            var threads = new Thread[5];
            for (int i = 0; i < 5; i++)
            {
                threads[i] = new Thread(philosophers[i].Run);
            }

            foreach (var thread in threads)
            {
                thread.Start();
            }

            threads[0].Join();
        }
Example #2
0
        static void Main(string[] args)
        {
            var sticks = new Chopstick[5];

            for (int i = 0; i < 5; i++)
            {
                sticks[i] = new Chopstick();
            }

            var philosophers = new Philosopher[5];

            for (int i = 0; i < 5; i++)
            {
                philosophers[i] = new Philosopher(sticks[i], sticks[(i + 1) % 5], (i + 1).ToString());
            }

            var threads = new Thread[5];

            for (int i = 0; i < 5; i++)
            {
                threads[i] = new Thread(philosophers[i].Run);
            }

            foreach (var thread in threads)
            {
                thread.Start();
            }

            threads[0].Join();
        }
Example #3
0
 public Philosopher(Chopstick left, Chopstick right,string name)
 {
     _left = left;
     _right = right;
     _name = name;
     _random = new Random();
 }
 //constructor - assign ID, name, and left and right chopstick for the philosopher
 public Philosopher(int nId, String sPName, Chopstick cLeft, Chopstick cRight)
 {
     nMyID    = nId;
     sName    = sPName;
     cMyLeft  = cLeft;
     cMyRight = cRight;
 }
Example #5
0
 public Philosopher(Chopstick left, Chopstick right, string name)
 {
     _left   = left;
     _right  = right;
     _name   = name;
     _random = new Random();
 }
 //Responds to a request for the chopstick
 void GiveChopstick(Chopstick chopstick)
 {
     if (chopstick.pHasIt != this)
     {
         return;           //doesn't have it
     }
     if (chopstick.bDirty) //used - philosopher has eaten (or initial state) - will give it
     {
         Console.WriteLine("{0} accepted request for chopstick number {1} from {2}", sName, chopstick.nChopstickID, chopstick.Contender(this).sName);
         lock (chopstick)
         {
             chopstick.bDirty = false;
             chopstick.pHasIt = null;
         }
         Release(chopstick, chopstick.Contender(this));
     }
     else //clean - decline, but will note the request
     {
         Console.WriteLine("{0} declined request for chopstick number {1} from {2}", sName, chopstick.nChopstickID, chopstick.Contender(this).sName);
         if (chopstick == cMyLeft)
         {
             cGiveToL = chopstick;                       //note that it was asked for
         }
         else
         {
             cGiveToR = chopstick;
         }
     }
 }
        static void Main()
        {
            Philosopher[] philosophers      = new Philosopher[nDimension];
            Chopstick[]   chopsticks        = new Chopstick[nDimension];
            Thread[]      philosopherThread = new Thread[nDimension];

            String sTemp;
            Random rnd = new Random();
            int    randomNumber;

            for (int i = 0; i < nDimension; i++)
            {
                chopsticks[i] = new Chopstick(i);
            }
            for (int i = 0; i < nDimension; i++)
            {
                sTemp = "";
                Chopstick cLeft  = chopsticks[getLeft(i)];
                Chopstick cRight = chopsticks[getRight(i)];
                while (sTemp == "") //name all philosophers
                {
                    randomNumber = rnd.Next(111);
                    if (top111[randomNumber] != "")
                    {
                        sTemp = top111[randomNumber];
                        top111[randomNumber] = "";
                    }
                }
                philosophers[i]      = new Philosopher(i, sTemp, cLeft, cRight);
                cLeft.pNeighborR     = philosophers[i]; //right neighbor of the left chopstick is the philosopher himself
                cRight.pNeighborL    = philosophers[i]; //left neighbor of the right chopstick is the philosopher himself
                philosopherThread[i] = new Thread(philosophers[i].Engage);
            }
            foreach (Chopstick chopstick in chopsticks)
            {
                if (chopstick.pNeighborL.nMyID > chopstick.pNeighborR.nMyID)
                {
                    chopstick.pHasIt = chopstick.pNeighborR;                                                          //Philosopher with smaller ID gets it
                }
                else
                {
                    chopstick.pHasIt = chopstick.pNeighborL;
                }
            }
            foreach (Philosopher philosopher in philosophers)
            {
                philosopher.Initialize();
            }
            for (int i = 0; i < nDimension; i++)
            {
                philosopherThread[i].Start();                                  //start each thread
            }
            for (int i = 0; i < nDimension; i++)
            {
                philosopherThread[i].Join();
            }
            Console.ReadLine();
        }
 //Philosopher takes the chopstick
 void TakeChopstick(Chopstick chopstick, Philosopher pWho)
 {
     if (pWho != this)
     {
         return;
     }
     if (chopstick == cMyLeft)
     {
         cTakeFromL = null;                       //have taken it
     }
     else
     {
         cTakeFromR = null;
     }
     lock (chopstick)
     {
         chopstick.pHasIt = this; //it's mine
     }
     Console.WriteLine("{0} takes chopstick number {1} from {2} and has {3} chopstick(s)", sName, chopstick.nChopstickID, chopstick.Contender(this).sName, HaveChopsticks());
 }
        static void Main()
        {
            Philosopher[] philosophers = new Philosopher[nDimension];
            Chopstick[] chopsticks = new Chopstick[nDimension];
            Thread[] philosopherThread = new Thread[nDimension];

            String sTemp;
            Random rnd = new Random();
            int randomNumber;

            for (int i = 0; i < nDimension; i++) chopsticks[i] = new Chopstick(i);
            for (int i = 0; i < nDimension; i++)
            {
                sTemp = "";
                Chopstick cLeft = chopsticks[getLeft(i)];
                Chopstick cRight = chopsticks[getRight(i)];
                while (sTemp == "") //name all philosophers
                {
                    randomNumber = rnd.Next(111);
                    if (top111[randomNumber] != "")
                    {
                        sTemp = top111[randomNumber];
                        top111[randomNumber] = "";
                    }
                }
                philosophers[i] = new Philosopher(i, sTemp, cLeft, cRight);
                cLeft.pNeighborR = philosophers[i]; //right neighbor of the left chopstick is the philosopher himself
                cRight.pNeighborL = philosophers[i]; //left neighbor of the right chopstick is the philosopher himself
                philosopherThread[i] = new Thread(philosophers[i].Engage);
            }
            foreach (Chopstick chopstick in chopsticks)
            {
                if (chopstick.pNeighborL.nMyID > chopstick.pNeighborR.nMyID) chopstick.pHasIt = chopstick.pNeighborR; //Philosopher with smaller ID gets it
                else chopstick.pHasIt = chopstick.pNeighborL;
            }
            foreach (Philosopher philosopher in philosophers) philosopher.Initialize();
            for (int i = 0; i < nDimension; i++) philosopherThread[i].Start(); //start each thread
            for (int i = 0; i < nDimension; i++) philosopherThread[i].Join();
            Console.ReadLine();
        }
 //Philosopher takes the chopstick
 void TakeChopstick(Chopstick chopstick, Philosopher pWho)
 {
     if (pWho != this) return;
     if (chopstick == cMyLeft) cTakeFromL = null; //have taken it
     else cTakeFromR = null;
     lock (chopstick)
     {
         chopstick.pHasIt = this; //it's mine
     }
     Console.WriteLine("{0} takes chopstick number {1} from {2} and has {3} chopstick(s)", sName, chopstick.nChopstickID, chopstick.Contender(this).sName, HaveChopsticks());
 }
 //Responds to a request for the chopstick
 void GiveChopstick(Chopstick chopstick)
 {
     if (chopstick.pHasIt != this) return; //doesn't have it
     if (chopstick.bDirty) //used - philosopher has eaten (or initial state) - will give it
     {
         Console.WriteLine("{0} accepted request for chopstick number {1} from {2}", sName, chopstick.nChopstickID, chopstick.Contender(this).sName);
         lock (chopstick)
         {
             chopstick.bDirty = false;
             chopstick.pHasIt = null;
         }
         Release(chopstick, chopstick.Contender(this));
     }
     else //clean - decline, but will note the request
     {
         Console.WriteLine("{0} declined request for chopstick number {1} from {2}", sName, chopstick.nChopstickID, chopstick.Contender(this).sName);
         if (chopstick == cMyLeft) cGiveToL = chopstick; //note that it was asked for
         else cGiveToR = chopstick;
     }
 }
            private int turnsLeft = 15; //turns to eat

            #endregion Fields

            #region Constructors

            //constructor - assign ID, name, and left and right chopstick for the philosopher
            public Philosopher(int nId, String sPName, Chopstick cLeft, Chopstick cRight)
            {
                nMyID = nId;
                sName = sPName;
                cMyLeft = cLeft;
                cMyRight = cRight;
            }