• Aucun résultat trouvé

An O(n log n) Election Algorithm for Unidirectional Rings .1 Context and Principles.1 Context and Principles

Leader Election Algorithms

4.4 An O(n log n) Election Algorithm for Unidirectional Rings .1 Context and Principles.1 Context and Principles

Each internal term is less than 2n, and there are at most 1+log2nterms. It fol-lows that the total number of messages is upper bounded by 8n(1+log2n), i.e., O(nlog2n).

Time Complexity To simplify, let us assume thatn=2k, and let us consider the best case, namely, all the processes start simultaneously. Moreover, as usual for time complexity, it is assumed that each message takes one time unit. The process with the highest identity will execute from round 0 until round k, and a round r will take 2r time units. By summing the time taken by all rounds, the process with the highest identity will be elected after at most 2(1+21+22+· · ·+2r+· · ·+2k)time units (the factor 2 is due to a messageELECTION()in one direction plus a message

REPLY()in the other direction). It follows that, in the best case, the time complexity is upper bounded by 2(2k2+111)=4n−2, i.e.,O(n).

The worst case time complexity, which is alsoO(n), is addressed in Exercise3.

This means that the time complexity is linear with respect to the size of the ring.

4.4 An O(n log n) Election Algorithm for Unidirectional Rings 4.4.1 Context and Principles

Context This section considers a unidirectional ring network in which the chan-nels are FIFO (i.e., on each channel, messages are received in their sending order).

As in the previous section, each processpi knows only its identity idi and the fact that no two processes have the same identity. No process knows the valuen.

Principle The algorithm presented below is due to D. Dolev, M. Klawe, and M.

Rodeh (1982). It is based on a very simple and elegant idea.

As in the previous election algorithms, initially all processes compete to be elected as a leader, and execute consecutive rounds to that end. During each round, at most half of the processes that are competitors remain competitors during the next round. Hence, there will be at mostO(log2n)rounds.

4.4 AnO(nlogn)Election Algorithm for Unidirectional Rings 87

Fig. 4.7 Neighbor processes on the unidirectional ring

Fig. 4.8 From the first to the second round

The novel idea is the way the processes simulate a bidirectional ring. Let us consider three processespi,pj, and pk such that pi andpk are the left and the right neighbor ofpj on the ring (Fig.4.7). Moreover, let us assume that a process receives messages from its right neighbor and sends messages to its left neighbor.

During the first round, each process sends its identity to its left neighbor, and after it has received the identity idx of its right neighborpx, it forwards idx to its left neighbor. Hence, when considering Fig.4.7,pi receives first idj and then idk, which means that it knows three identities: idi, idj, and idk. It follows that it can play the role ofpj. More precisely

If idj >max(idi,idk),pi considers idj as the greatest identity it has seen and progresses to the next round as a competitor on behalf of idj.

If idj<max(idi,idk),pi stops being a competitor and its role during the next rounds will be to forward to the left the messages it receives from the right.

It is easy to see that, ifpi remains a competitor (on behalf of idj) during the second round, its left neighborph and its right neighborpj can no longer remain competitors (on behalf of idi, and on behalf of idk, respectively). This is because

idj>max(idi,idk)⇒ ¬

idi>max(idh,idj)

∧ ¬

idk>max(idj,id) . It follows that, during the second round, at most half of the processes remain com-petitors. During that round, the processes that are no longer competitors only for-ward the messages they receive, while the processes that remain competitors do the same as during the first round, except that they consider the greatest identity they have seen so far instead of their initial identity. This is illustrated in Fig.4.8, where it is assumed that idj>max(idi,idk), id<max(idk,idm), and idm>max(id,idp), and consequentlypi competes on behalf of idj, andpcompetes on behalf of idm. The processes that remain competitors during the second round define a logical ring with at mostn/2 processes. This ring is denoted with dashed arrows in the figure.

Finally, the competitor processes that are winners during the second round pro-ceed to the third round, etc., until a round with a single competitor is attained, which occurs after at most 1+ log2nrounds.

whenSTART()is received do

(1) competitoritrue; maxidiidi;sendELECTION(1,idi).

whenELECTION(1,id)is received do (2) if(¬competitori)

(3) thensendELECTION(1,id) (4) else if(id=maxidi)

(5) thensendELECTION(2,id); proxy_foriid (6) else sendELECTED(id,idi)

(7) end if

(8) end if.

whenELECTION(2,id)is received do (9) if(¬competitori)

(10) thensendELECTION(2,id)

(11) else if(proxy_fori>max(id,maxidi))

(12) then maxidiproxy_fori;sendELECTION(1,proxy_fori) (13) else competitorifalse

(14) end if

(15) end if.

whenELECTED(id1,id2)is received do

(16) leaderiid1; doneitrue; electedi(id1=idi);

(17) if(id2=idi)thensendELECTED(id1,id2)end if.

Fig. 4.9 Dolev, Klawe, and Rodeh’s election algorithm (code forpi)

4.4.2 The Algorithm

As in Sect.4.3, it is assumed that each process receives an external messageSTART() before any message sent by the algorithm. The algorithm is described in Fig.4.9.

Local Variables In addition to donei, leaderi, and electedi, each processpi man-ages three local variables.

competitoriis a Boolean which indicates ifpiis currently competing on behalf of some process identity or is only relaying messages. The two other local variables are meaningful only when competitori is equal to true.

maxidi is the greatest identity know bypi.

proxy_foriis the identity of the process for whichpi is competing.

Process Behavior When a process receives a message START(), it initializes competitori and maxidi before sending a messageELECTION(1,idi)on its single outgoing channel (line1). Let us observe that messages do not carry round numbers.

Actually, round numbers are used only to explain the behavior of the algorithm and compute the total number of messages.

When a processpi receives a messageELECTION(1,id),pi forwards it on its outgoing channel if it is no longer a competitor (lines2–3). If pi is a competitor, there are two cases.

Outline

Documents relatifs