A Self-stabilizing algorithm with tight bounds for mutual ... - CS, Technion

2 downloads 0 Views 201KB Size Report
Sep 22, 2008 - As an example, consider the type 0 move in which p0 is privileged. ..... Let's denote by t01, t02, t31, t32, t71, t72 the number of moves of types 0, ...
Technion - Computer Science Department - Tehnical Report CS-2008-09 - 2008

A Self-stabilizing algorithm with tight bounds for mutual exclusion on a ring ∗ Viacheslav Chernoy 1 Mordechai Shalom Shmuel Zaks1

1

2

Department of Computer Science, Technion, Haifa, Israel [email protected], [email protected] 2 TelHai Academic College, Upper Galilee, 12210, Israel [email protected]

July 3, 2008

Abstract In [Dij74] Dijkstra introduced the notion of self-stabilizing algorithms and presented, among others, an algorithm with three states for the problem of mutual exclusion on a ring of processors. In this work we present a new three state self-stabilizing algorithm for mutual exclusion, with a tight bound of 56 n2 + O(n) for the worst case complexity, which is the number of moves of the algorithm until it stabilizes. This bound is better than lower bounds of other algorithms, including Dijkstra’s. Using similar techniques we improve the 2 analysis of the upper bound for Dijkstra’s algorithm and show a bound of 3 13 18 n + O(n).



The paper appears in the proceedings of the 22nd International Symposium on Distributed Computing (DISC), Arcachon, France, September 22-24, 2008.

1

Technion - Computer Science Department - Tehnical Report CS-2008-09 - 2008

1

Introduction

The notion of self stabilization was introduced by Dijkstra in [Dij74]. He considers a system, consisting of a set of processors, and each running a program of the form: if condition then statement. A processor is termed privileged if its condition is satisfied. A scheduler chooses any privileged processor, which then executes its statement (i.e., makes a move); if there are several privileged processors, the scheduler chooses any of them. Such a scheduler is termed centralized. A scheduler that chooses any subset of the privileged processors, which are then making their moves simultaneously, is termed distributed. Thus, starting from any initial configuration, we get a sequence of moves (termed an execution). The scheduler thus determines all possible executions of the system. A specific subset of the configurations is termed legitimate. The system is self-stabilizing if any possible execution will eventually get — that is, after a finite number of moves — only to legitimate configurations. The number of moves from any initial configuration until the system stabilizes is often referred to as stabilization time (see, e.g., [BJM06, CG02, NKM06, TTK00]). Dijkstra studied in [Dij74] the fundamental problem of mutual exclusion, for which the subset of legitimate configurations includes the configurations in which exactly one processor is privileged. In [Dij74] the processors are arranged in a ring, so that each processor can communicate with its two neighbors using shared memory, and where not all processors use the same program. Three algorithms were presented — without proofs for either correctness or complexity — in which each processor could be in one of k > n, four and three states, respectively (n being the number of processors). A centralized scheduler was assumed. The analysis — correctness and complexity — of Dijkstra’s first algorithm is rather straightforward; its correctness under a centralized scheduler is for any k ≥ n − 1, and under a distributed scheduler for any k ≥ n. The stabilization time under a centralized scheduler is Θ(n2 ) (following [CGR87] this is also the expected number of moves). There is little in the literature regarding the second algorithm, probably since it was extended in [Kru79] to general trees, or since more attention was devoted to the third algorithm, which is rather non-intuitive. For this latter algorithm Dijkstra presented in [Dij86] a proof of correctness (another proof was given in [Kes88], and a proof of correctness under a distributed scheduler was presented in [BGM89]). Though while dealing with proofs of correctness one can sometimes get also complexity results, this was not the case with this proof of [Dij86]. In [CSZ07] we provide an upper bound of 5 34 n2 on the stabilization time of Dijkstra’s third algorithm. In [BD95] a similar three state algorithm with an upper bound of 5 34 n2 is presented. In [CSZ08] this upper bound was improved to 1 12 n2 , and a lower bound of n2 was shown.

2

Our contribution

In this work we present a new three state self-stabilizing algorithm for mutual exclusion for a ring of processors, and show a tight bound of 56 n2 + O(n) for its worst case time complexity. For the lower bound we provide an example that requires 56 n2 − O(n) moves until stabilization. For the upper bound we proceed in two ways. The first one is using the more conventional tool of potential functions, that is used in the literature of self-stabilizing algorithms to deal mainly with the issue of correctness (see, e.g., [Dol00]). In our case the use of this tool is not straightforward, since the potential function can also increase by some of the moves (see Section 1 2 n + O(n). 4.5). We use this tool to achieve a complexity result; namely, an upper bound of 1 12 The second one is using amortized analysis. This more refined technique enables us to achieve a tight bound of 56 n2 + O(n). We use both techniques to improve the analysis for Dijkstra’s algorithm that results in an 2

Technion - Computer Science Department - Tehnical Report CS-2008-09 - 2008

2 improved upper bound of 3 13 18 n + O(n) for its worst case time complexity. We also show a lower 5 2 bound of 1 6 n − O(n) of Dijkstra’s algorithm. Therefore in the worst case our algorithm has better performance than Dijkstra’s and than the one of [BD95]. In Section 3 we present Dijkstra’s algorithm and outline the details of the proof of [Dij86] needed for our discussion. In Section 4 we present our new self-stabilizing algorithm and its analysis. In Section 5 we present the above-mentioned lower and upper bounds for Dijkstra’s algorithm. We summarize our results in Section 6. Most proofs are sketched or omitted.

3

Dijkstra’s algorithm

In this section we present Dijkstra’s third algorithm of [Dij74] (to which we refer throughout this paper as Dijkstra’s algorithm, and its proof of correctness of [Dij86]). Our discussion assumes a centralized scheduler. In our system there are n processors p0 , p1 , . . . , pn−1 arranged in a ring; that is for every 0 ≤ i ≤ n − 1, the processors adjacent to pi are p(i−1) mod n and p(i+1) mod n . Processor pi has a local state xi ∈ {0, 1, 2}. Two processors — namely, p0 and pn−1 — run special programs, while all intermediate processors pi , 1 ≤ i ≤ n − 2, run the same program. Dijkstra’s Algorithm Program for processor p0 : IF x0 + 1 = x1 THEN x0 := x0 + 2 END. Program for processor pi , 1 ≤ i ≤ n − 2: IF (xi−1 − 1 = xi ) OR (xi = xi+1 − 1) THEN xi := xi + 1 END. Program for processor pn−1 : IF (xn−2 = xn−1 = x0 ) OR (xn−2 = xn−1 + 1 = x0 ) THEN xn−1 := xn−2 + 1 END. The legitimate configurations for this problem are those in which exactly one processor is privileged. The configurations x0 = · · · = xn−1 and x0 = · · · = xi = xi+1 = · · · = xn−1 are legitimate. This algorithm self stabilizes; namely, starting from any initial configuration the system achieves mutual exclusion. Given an initial configuration x0 , x1 , . . . , xn−1 , and placing the processors on a line, consider each pair of neighbors pi−1 and pi , for i = 1, . . . , n − 1 (note pn−1 and p0 are not considered here to be neighbors). In this work we denote left arrow and right arrow, introduced in [Dij86], by ’’. Notation xi−1 < xi means xi = (xi−1 + 1) mod 3 and xi−1 > xi means xi = (xi−1 − 1) mod 3. Thus, for each two neighboring processors with states xi−1 and xi , either xi−1 = xi , or xi−1 < xi , or xi−1 > xi . For a given configuration C = x0 , x1 , . . . , xn−1 , Dijkstra introduces the function f (C) = #lef t arrows + 2#right arrows .

(1)

Example. For n = 7, a possible configuration C is x0 = 1, x1 = 1, x2 = 0, x3 = 1, x4 = 2, x5 = 2, x6 = 0. This configuration is denoted as [1 = 1 > 0 < 1 < 2 = 2 < 0] . For this configuration we have f (C) = 3 + 2 × 1 = 5. 3

It follows immediately from (1) that for any configuration C of n processors

Technion - Computer Science Department - Tehnical Report CS-2008-09 - 2008

0 ≤ f (C) ≤ 2(n − 1) .

(2)

There are eight possible types of moves of the system: one possible move for processor p0 , five moves for any intermediate processor pi , 0 < i < n − 1, and two moves for pn−1 . These possibilities are summarized in Table 1. In this table C1 and C2 denote the configurations before and after the move, respectively, and Δf = f (C2 ) − f (C1 ). In the table we show only the local parts of these configurations. As an example, consider the type 0 move in which p0 is privileged. In this case C1 and C2 are the local configurations x0 < x1 and x0 > x1 , correspondingly. Since one left arrow is replaced by a right arrow, we have Δf = f (C2 ) − f (C1 ) = 1. Type Proc. C1 0 p0 x0 < x1 1 pi xi−1 > xi = xi+1 2 pi xi−1 = xi < xi+1 3 pi xi−1 > xi < xi+1 4 pi xi−1 > xi > xi+1 5 pi xi−1 < xi < xi+1 6 pn−1 xn−2 > xn−1 < x0 7 pn−1 xn−2 = xn−1 = x0

C2 Δf x0 > x1 +1 xi−1 = xi > xi+1 0 xi−1 < xi = xi+1 0 xi−1 = xi = xi+1 −3 xi−1 = xi < xi+1 −3 xi−1 > xi = xi+1 0 xn−2 < xn−1 −1 xn−2 < xn−1 +1

Table 1: Dijkstra’s algorithm It is proved that each execution is infinite (that is, there is always at least one privileged processor). Then it is shown that p0 makes infinite number of moves. Then the execution is partitioned into phases; each phase starts with a move of p0 and ends just before its next move. It is argued that the function f decreases at least by 1 after each phase. By (2) it follows that Dijkstra’s algorithm terminates after at most 2(n − 1) phases.

4

New self-stabilizing algorithm for mutual exclusion

In this section we present a new algorithm A for self-stabilization, prove its correctness and show a tight bound for its worst case time complexity. Our discussion includes the following steps. We first describe the new algorithm; we note the issues in which it differs from Dijkstra’s algorithm, discuss its lower bound and prove its correctness. We then introduce a new function h, with which we achieve an upper bound, and finally provide a proof for the tight upper bound using amortized analysis.

4.1

Algorithm A

Algorithm A is similar to Dijkstra’s algorithm with the following changes: moves of types 4 and 5 are not allowed, and moves of type 6 do not depend on processor p0 . Informally Algorithm A allows the arrows to move and bounce (change direction at p0 and pn−1 ) until they are destroyed by moves of type 3. New arrows may be created by a move of type 7. Algorithm A Program for processor p0 : IF x0 + 1 = x1 THEN

4

Technion - Computer Science Department - Tehnical Report CS-2008-09 - 2008

x0 := x0 + 2 END. Program for processor pi , 1 ≤ i ≤ n − 2: IF (xi−1 − 1 = xi = xi+1 ) OR (xi−1 = xi = xi+1 − 1) OR (xi−1 = xi + 1 = xi+1 ) THEN xi := xi + 1 END. Program for processor pn−1 : IF (xn−2 = xn−1 = x0 ) OR (xn−2 = xn−1 + 1) THEN xn−1 := xn−2 + 1 END. We define the function fˆ for any configuration C as follows: fˆ(C) = (#lef t arrows − #right arrows) mod 3 .

(3)

Recalling (1) we get: fˆ(C) ≡ f (C) (mod 3) . Since ’’) is a shortcut for xi − xi−1 ≡ 1 (mod 3) (resp. xi − xi−1 ≡ −1 (mod 3)), we have that fˆ(C) ≡ (xn−1 − x0 ) (mod 3), which means that fˆ(C) = 0 iff xn−1 = x0 . We summarize the moves of algorithm A in Table 2. In this table we also include the changes in the function fˆ and the function h (that will be introduced in Section 4.5) implied by each move. We include the rows for moves 4 and 5 (that do not exist) to simplify the analogy to Dijkstra’s algorithm. Type Proc. C1 C2 Δˆ f 0 p0 x0 < x1 x0 > x1 +1 1 pi xi−1 > xi = xi+1 xi−1 = xi > xi+1 0 2 pi xi−1 = xi < xi+1 xi−1 < xi = xi+1 0 3 pi xi−1 > xi < xi+1 xi−1 = xi = xi+1 0 4 5 6 pn−1 xn−2 > xn−1 xn−2 < xn−1 −1 ˆ 7 pn−1 xn−2 = xn−1 , f = 0 xn−2 < xn−1 +1

Δf Δh +1 n−2 0 −1 0 −1 −3 −(n + 1)

−1 +1

n−2 n−1

Table 2: Algorithm A

4.2

Lower bound

  We denote by regular expressions over {, =}. For example,   3 2 configurations and < = 2 are possible notations for the configuration x0 < x1 < x2 < x3 = x4 = x5 < x6 > x7 > x8 . Note that this notation does not lose relevant information, since the behavior of the algorithm is dictated by the arrows (see Table 2). Theorem 1 The worst case stabilization time of algorithm A is at least 56 n2 − O(n).

  Proof. + 3. For any 0 ≤ i ≤ k, let Ci := =3i xi+1 5 pi xi−1 < xi < xi+1 6 pn−1 xn−2 > xn−1 , fˆ = 2 7 pn−1 xn−2 = xn−1 , fˆ = 0

C2 Δˆ f x0 > x1 +1 xi−1 = xi > xi+1 0 xi−1 < xi = xi+1 0 xi−1 = xi = xi+1 0 xi−1 = xi < xi+1 0 xi−1 > xi = xi+1 0 xn−2 < xn−1 −1 xn−2 < xn−1 +1

Δf Δh +1 n−2 0 −1 0 −1 −3 −(n + 1) −3 3i − 2n + 2 ≤ n − 4 0 n − 3i − 1 ≤ n − 4 −1 n−2 +1 n−1

Table 5: Dijkstra’s algorithm For any execution e, we denote by |e| the number of moves in e. Let ar (resp. al ) be the number of ’>’ (resp. ’ 0, there is at least one type 37 move in e. Let pi be the processor that made this move. Immediately after this move pi is disabled. As there are no deadlocks, pi will be re-enabled again. This happens at the first time there is an > (resp.