0

我正在尝试实现 Bully Coordinator 选举算法。在该算法中,Coordinator 每隔 10 秒发送一次存活消息,所有进程至少等待 14 秒才能接收到存活消息,如果在这段时间内没有收到消息,它们将发起死协调者选举。

问题是 AliveTimer (Timer3_Count) 呈指数增长,并且活动进程也在影响它。我不知道为什么它的行为很奇怪。

当初始协调器发送 Alive 消息时,计数器工作正常,但在死亡协调器选举后,它的行为很奇怪。

else if (Received_Text.Contains("Alive:"))
            {
                SetText(Received_Text + "\n");
                Coordinator_Alive = true;

                Timer3_Counter = 0;

                if (Alive_Count == 0)
                {
                    Alive_Count++;


                    AliveTimer.Interval = (1 * 1000);
                    AliveTimer.Enabled = true;
                    AliveTimer.Elapsed += new System.Timers.ElapsedEventHandler(AliveTimer_Elapsed);
                    AliveTimer.Start();

                }

            }

elapsed 函数在这里我认为我的程序有问题,我尝试了所有方法。

private void AliveTimer_Elapsed(object sender, EventArgs e)
    {
        Timer3_Counter++;
        SetTimer(Timer3_Counter.ToString());

        Random rnd = new Random();
        int rand_time = rnd.Next(14, 18);

        if (Timer3_Counter == 14)
        {
            AliveTimer.Stop();

            Timer3_Counter = 0;
            Alive_Count = 0;

            if (Coordinator_Alive == false)
            {
                byte[] buffer = Encoding.ASCII.GetBytes("Dead Coordinator Election: " + txName.Text);
                _clientSocket.Send(buffer);

                Timer4_Counter = 0;

                DeadTimer.Interval = (1 * 1000);
                DeadTimer.Elapsed += new System.Timers.ElapsedEventHandler(DeadTimer_Elapsed);
                DeadTimer.Enabled = true;
                DeadTimer.Start();

            }

        }

        if (Coordinator_Alive == true)
            Coordinator_Alive = false;

    }

和死协调员选举代码在这里

else if (Received_Text.Contains("Dead Coordinator Election:"))
            {
                SetCPID("");
                Coordinator_Alive = false;
                Alive_Count = 0;
                Timer3_Counter = 0;

                AliveTimer.Stop();
                AliveTimer.Enabled = false;


                string output = Regex.Match(Received_Text, @"\d+").Value;
                SetText("Dead Coordinator Election Received from Process ID: " + output + "\n");

                if (Convert.ToInt32(txName.Text) > Convert.ToInt32(output))
                {
                    byte[] buffer = Encoding.ASCII.GetBytes("Greater Process No: " + txName.Text + " found than " + output + "\n");
                    _clientSocket.Send(buffer);
                    SetText("Our Process No: " + txName.Text + " is Greater than " + output + "\n");
                    Lower_Count++;

                    byte[] buffer1 = Encoding.ASCII.GetBytes("Dead Coordinator Election: " + txName.Text);
                    _clientSocket.Send(buffer1);
                }
                else
                {
                    byte[] Txt_Send = Encoding.ASCII.GetBytes("Our Process No: " + txName.Text + " is less than " + output);
                    _clientSocket.Send(Txt_Send);
                    Greater_Count++;
                }

            }

完整的代码可以在这里找到 欺凌算法

注意:我使用被动服务器只是为了广播来自每个进程的消息

4

1 回答 1

0

我不知道导致问题的原因,但我认为如果您记录启动和停止所有方法并分析输出,将能够快速找出原因。

这将有助于确定: 1. 正如@Idle_Mind 建议的那样,您正在添加越来越多的处理程序 2. 执行每个方法所花费的时间越来越多......

我不知道您的应用程序是如何构建的,但您甚至可以从Console.WriteLineor开始Debug.WriteLine

于 2017-07-28T03:25:21.033 回答