Example #1
0
        public void Start(int userId, Bakery b)
        {
            Task.Factory.StartNew(() =>
                {
                    var r = new Random();

                    Console.WriteLine("Starting GoodUser {0}...", userId);

                    b.Lock(userId);

                    Console.WriteLine("Inside critical section {0}...", userId);

                    // Do some silly work, and keep health upto date.
                    for (var j=0; j < 100; j++)
                    {
                        Thread.Sleep(r.Next(0, 10));
                    }

                    b.Unlock(userId);

                    //throw new Exception();

                    Console.WriteLine("Released critical section {0}...", userId);
                })
                .ContinueWith(t =>
                {
                    if (t.Exception != null)
                    {
                        Console.WriteLine("Oops!!!");
                    }
                });
        }
Example #2
0
        /// <summary>
        /// Main entry point for simulation.
        /// </summary>
        public static void Main(string[] args)
        {
            // Initialize the bkery, with a max size.
            var b = new Bakery(8);

            // This user process will fail during the critical section,
            // in order to simulate a failed process with no recovery.
            //new BadUser().Start(8, b);

            // These users represent normal, well-behaved user processes.
            new GoodUser().Start(0, b);
            new GoodUser().Start(1, b);
            new GoodUser().Start(2, b);
            new GoodUser().Start(3, b);

            // add some more user process a little bit later.
            //Thread.Sleep(2000);
            new GoodUser().Start(4, b);
            new GoodUser().Start(5, b);
            new GoodUser().Start(6, b);
            new GoodUser().Start(7, b);

            // Wait
            Console.ReadLine();
        }
Example #3
0
        public void Start(int userId, Bakery b)
        {
            Task.Factory.StartNew(() =>
                {
                    var r = new Random();

                    Console.WriteLine("Starting BadUser {0}...", userId);

                    b.Lock(userId);

                    Console.WriteLine("Inside critical section {0}...", userId);

                    Thread.Sleep(TimeSpan.FromSeconds(r.Next(1, 3)));

                    Console.WriteLine("Exiting without unlocking - FAILURE simulation...");
                });
        }