Distributed Algorithms Lecture 3 Resource Allocation 1. Problem
Total Page:16
File Type:pdf, Size:1020Kb
1. Problem description • 1.1 Mutual Exclusion Distributed Algorithms – There are N processes and one resource. lecture 3 – N processes compete for one resource. Resource Allocation – The resource can be accessed(used) by only one .process at a time. – Mutual exclusive access, deadlock-free, starvation-free University of Aizu have to be guaranteed. Zixue Cheng p1 p2 …….. pi pn r 1.2 Distributed resource allocation problem 1.2 Distributed resource allocation problem • N processes and M resources. • A resource can be accessed (used) by only one process at • A subset of processes may compete for one resource. a time. – If M = 1 and all processes compete for the resource, • Every process has to acquire all resources which it then the problem reduces to mutual exclusion problem. requires. • Mutual exclusive access to a resource, deadlock-free, p1 p2 …….. pi pn starvation-free p1 p2 …….. pi pn r1 r2 …….. rj rm r1 r2 …….. rj rm 1.3 Conventional dining philosophers problem • There are five philosophers, a dining room with a table, five seats around the table, and five forks on the table. • Every philosophers behaves like thinking, hungry (going into the dining room, seating at a chair, picking up a fork, picking up another fork), eating, thinking • A fork can be used by only one philosopher at a time. • Every philosopher has to acquire the two forks which it requires. • Examples of deadlock and starvation • Mutual exclusive use a fork, deadlock-free, starvation- free 1 Deadlock, if every one picks up his right fork at the same time Deadlock, if every one picks up his right fork at the same time See how starvation happens See how starvation happen See how starvation happen 2 See how starvation happen See how starvation happen See how starvation happen See how starvation happen See how starvation happen See how starvation happen 3 See how starvation happen See how starvation happen See how starvation happen See how starvation happen See how starvation happen See how starvation happen 4 See how starvation happen See how starvation happen 1.4 Distributed dining philosophers problem • A graph (a network) with N nodes. • A node represents a philosopher, an edge is related with a fork. • Any pair of neighboring nodes compete for the fork corresponding to the edge between the pair of nodes. • A fork can be used by only one philosopher at a time. • Every philosopher has to acquire the all forks which it requires. • Mutual exclusive use of a fork, deadlock-free, starvation- free 1.5 Drinking philosophers problem • A graph (a network) with N nodes and multiple-edges between a pair of nodes. • A node represents a philosopher, an edge is related with a bottle. • Any pair of neighboring nodes compete for a subset of bottles corresponding to the multiple-edges between the pair of nodes. • A bottle can be used by only one philosopher at a time. • Every philosopher has to acquire the all bottles which it requires. • Mutual exclusive use of a fork, deadlock-free, starvation- free 5 2 An algorithm for distributed dining 2.1.2 The possible problems philosophers problem (Deadlock and starvation) • 2.1 Basic ideas of the algorithm 1. (a) If a philosopher sends forks to their neighbors 1. 2.1.1 The basic behavior of a philosopher immediately on receiving their Request message then 1. (a) A philosopher becomes hungry starvation may happen. 2. (b) The philosopher tries to get all forks necessary for 2. (b) If a philosopher sends forks only when it has finished eating by sending Request messages to its neighbors. eating (used the forks), then deadlock may happen. 3. (c) The philosopher begins to eat when all the forks are 3. (c) When and under what condition should a philosopher acquired. send its forks to its neighbors. 4. (d) A philosopher sends forks to its neighbors on their request. 2.1.3 Solution to the problems (Introducing a new concept “Turn”) 1. (a) a Turn is related with each edge (each fork) 2. (b) The turn is held by one endpoint (philosopher) of the edge. 3. (c) The philosopher held the turn has priority to use the fork p3 4. (d) A turn is sent over the corresponding edge everytime the philosopher holding the turn finishes its eating to guarantee that the priority to hold the fork alternates between the two p1 p2 philosophers. 5. (e) A turn is different from a fork in the sense that 1. A philosopher does not need to acquire turns for all the edges p4 incident to it in order to eat. 2. The turns are sent to a philosopher's neighbors as soon as the philosopher finishes its eating. p3 p3 p1 p2 p1 p2 p4 p4 6 p3 p3 p1 p2 p1 p2 p4 p4 2.1.4 Detailed solution (Implementation of 2.2 Formal description of the algorithn above solution) (a) The following messages are used by the algorithm 1. (8.11) pi sends request messages, if it becomes hungry 1. Request: and the forks is not enough. 2. Fork(nil) 2. (8.12) Competition resolution. (how to deal with the 3. Fork(turn) received request 4. Fork(request) 3. (8.13) On receiving a fork 4. (8.14) On receiving a turn (b) The following variables are used by each p hilosopher pi 1. Hungry: a boolean variable to indicate if pi wants to eat. j: 2.∀j ∈ Neighber(pi) hold_forki j: 3.∀j ∈ Neighber(pi) hold_turni ∀j ∈ j: 4. Neighber(pi) owes_forki 2.3 Correctness of the algorithm 2.3 Correctness of the algorithm 1. Both neighbors of an edge can't hold a fork in the same 4. New idea: directed acyclic graph time, to guarantee mutual exclusion. (a) Consider an edge with direction. The arrow from the endpoint without turn to the endpoint with the turn. 2. A fork related with an edge could be initially assigned to Note: the directed graph is just for assign turn, but not for any one of endpoints of the edge. describing direction of communication channel. (b) Condition for assigning turn initially is such that there is no 3. A turn related with an edge should be initially assigned cycle in the directed graph. to an endpoint of the edge with some conditions (c) The deadlock-free is guaranteed. Since the graph is an acyclic, (described later) to guarantee deadlock-free and there must be at least a sink. All turns of edges of the sink starvation-free belongs the sink. So the sink will acquire all forks in finite time. After finishing eating, the sink philosopher sends all its turns to the corresponding neighbors, which guarantees the graph is still acyclic. Thus deadlock never happens. (d) The starvation-free is guaranteed. Every philosopher has a chance to be a sink. 7 2.4 Complexities of the algorithm 2.4 Complexities of the algorithm 1. The message complexity of the algorithm (per every eating) is 2. The bit complexity of the algorithm is also Ο(Δ) Ο(Δ), where Δ=max {|Neig |} ∈ni N i Consider the fact, no message includes a variable A philosopher p may send as many Request messages as it has i parameter. The lengths of messages are constant. neighbors, i.e. |Neigi|. In the worst case pi has no turn Its neighbors are not hungry at the time receiving Request 3. The time complexity of the algorithm is Ο(n) Before pi acquires all forks, some neighbor becomes hungry Consider the worst case such as a ring with only one sink. and asks to send back the corresponding fork. The distance from a source to a sink may be n. pi has to return the acquired fork and ask it again. Finally, pi acquires all turns and forks For each edges, a Request, a fork(nil), a fork(request) a fork(turn) messages may be used. Thank you 8.