예제 #1
0
        static void Main(string[] args)
        {
            PetersonTwoProcess sharedVariable = new PetersonTwoProcess();
            WorkerProcess      process1       = new WorkerProcess(0, sharedVariable);
            WorkerProcess      process2       = new WorkerProcess(1, sharedVariable);

            Thread t1 = new Thread(process1.RunPeterson);
            Thread t2 = new Thread(process2.RunPeterson);

            ApplicationLog.Instance.WriteInfo("Running original version of peterson's algorithm with process 1 and 2.");
            Console.WriteLine("Running original version of peterson's algorithm with process 1 and 2.");

            t1.Start();
            t2.Start();

            while (t1.IsAlive && t2.IsAlive)
            {
                Thread.Sleep(10000);
                sharedVariable.shouldStop = true;
            }
            t1.Abort();
            t2.Abort();
            t1.Join();
            t2.Join();

            Console.WriteLine("Stopping original version of peterson's algorithm with process 1 and 2.");
            ApplicationLog.Instance.WriteInfo("Stopping original version of peterson's algorithm with process 1 and 2.");

            sharedVariable = new PetersonTwoProcess();
            WorkerProcess process3 = new WorkerProcess(0, sharedVariable);
            WorkerProcess process4 = new WorkerProcess(1, sharedVariable);

            Thread t3 = new Thread(process3.RunModifiedPeterson);
            Thread t4 = new Thread(process4.RunModifiedPeterson);

            Console.WriteLine("Running modified version of peterson's algorithm with process 3 and 4.");
            ApplicationLog.Instance.WriteInfo("Running modified version of peterson's algorithm with process 3 and 4.");

            t3.Start();
            t4.Start();

            while (t3.IsAlive && t3.IsAlive)
            {
                Thread.Sleep(10000);
                sharedVariable.shouldStop = true;
            }
            t3.Abort();
            t4.Abort();
            t3.Join();
            t4.Join();
            Console.WriteLine("Stopping modified version of peterson's algorithm with process 3 and 4.");
            ApplicationLog.Instance.WriteInfo("Stopping modified version of peterson's algorithm with process 3 and 4.");
            Console.ReadLine();
        }
예제 #2
0
        static void Main(string[] args)
        {
            PetersonTwoProcess sharedVariable = new PetersonTwoProcess();
            WorkerProcess      process1       = new WorkerProcess(0, sharedVariable);
            WorkerProcess      process2       = new WorkerProcess(1, sharedVariable);

            CancellationTokenSource cancellationSource = new CancellationTokenSource();
            CancellationToken       ct = cancellationSource.Token;

            Task t1 = Task.Factory.StartNew(() => process1.RunPeterson(ct), ct);
            Task t2 = Task.Factory.StartNew(() => process2.RunPeterson(ct), ct);

            while (t1.Status.Equals(TaskStatus.Running) &&
                   t2.Status.Equals(TaskStatus.Running) &&
                   !cancellationSource.IsCancellationRequested)
            {
                Thread.Sleep(10000);
                cancellationSource.Cancel();
            }

            cancellationSource.Cancel();
            sharedVariable.shouldStop = true;

            try
            {
                //Task.WaitAll(new Task[] { t1, t2 });
            }
            catch (Exception ex)
            {
            }
            finally
            {
                cancellationSource.Dispose();
            }

            Console.WriteLine("Task {0} status is now {1}", t1.Id, t1.Status);
            Console.WriteLine("Task {0} status is now {1}", t2.Id, t2.Status);


            //Task ta = new Task(process1.RunPeterson);
            //Task tb = new Task(process2.RunPeterson);

            //ta.Start();
            //tb.Start();

            //Thread t1 = new Thread(process1.RunPeterson);
            //Thread t2 = new Thread(process2.RunPeterson);

            //Console.WriteLine("Running original version of peterson's algorithm with process 1 and 2.");

            //t1.Start();
            //t2.Start();

            //while (t1.IsAlive && t2.IsAlive)
            //{
            //    //for (long i = 1000000000; i >= 0; i--)
            //    //{
            //    //    if (i.Equals(0))
            //    //        sharedVariable.shouldStop = true;
            //    //}
            //    Thread.Sleep(10000);
            //    sharedVariable.shouldStop = true;
            //}
            //t1.Abort();
            //t2.Abort();
            //t1.Join();
            //t2.Join();

            //Console.WriteLine("Stopping original version of peterson's algorithm with process 1 and 2.");

            //sharedVariable = new PetersonTwoProcess();
            //WorkerProcess process3 = new WorkerProcess(0, sharedVariable);
            //WorkerProcess process4 = new WorkerProcess(1, sharedVariable);

            //Thread t3 = new Thread(process3.RunModifiedPeterson);
            //Thread t4 = new Thread(process4.RunModifiedPeterson);

            //Console.WriteLine("Running modified version of peterson's algorithm with process 3 and 4.");

            //t3.Start();
            //t4.Start();

            //while (t3.IsAlive && t3.IsAlive)
            //{
            //    for (long i = 1000000000; i >= 0; i--)
            //    {
            //        if (i.Equals(0))
            //            sharedVariable.shouldStop = true;
            //    }
            //}
            //t3.Abort();
            //t4.Abort();
            //t3.Join();
            //t4.Join();
            //Console.WriteLine("Stopping modified version of peterson's algorithm with process 3 and 4.");
            Console.ReadLine();
        }