开发者

Looping through a changing event list

I have a list of machines that I ping depending on their response time. Initially all machines are pinged each t milliseonds for 5 times. Depending on the response time for these 5 pings on each machine I adjust the time of the ping to stretch or contract it, till arriving at so开发者_运维技巧me configuration like this:

machine1: t1....t2....t3....t4....t5
machine2: t1......t2......t3......t4......t5
..
machineN: ..t1..t2..t3..t4..t5
            ^
            |machine needs to be pinged at this tick

t1..tN represents the (millisecond) ticks of the clock.

Having a thread per machine to do the ping is evident, but not an optimal solution due the number of machines.

Rather, one thread that iteratites through the global order of the events is desirable, something like this:

while(true){

  fetch_next_machine_to_be_pinged();
  ping_it();

  if(any_machine_pinged_5_times());
     reorder_events(); //adjust the time of its next 5 ping

  //continue
}

What is the best way to achieve this? (ps: language C).


I would use a priority queue.

At any time, the queue would contain one entry per machine. The "priority" of entry M would be the timestamp when machine M needs to be pinged next, and the payload would be some token identifying the machine (so that you know whom to ping).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜