Leslie Lamport's Logical Clocks : a Tutorial
Total Page:16
File Type:pdf, Size:1020Kb
Leslie Lamport's logical clocks : a tutorial Citation for published version (APA): Hoogerwoord, R. R. (2002). Leslie Lamport's logical clocks : a tutorial. (Universitaet Hamburg. FBI-HH-B, Fachbereich Informatik : Bericht; Vol. 238), (Computer science reports; Vol. 0201). Technische Universiteit Eindhoven. Document status and date: Published: 01/01/2002 Document Version: Publisher’s PDF, also known as Version of Record (includes final page, issue and volume numbers) Please check the document version of this publication: • A submitted manuscript is the version of the article upon submission and before peer-review. There can be important differences between the submitted version and the official published version of record. People interested in the research are advised to contact the author for the final version of the publication, or visit the DOI to the publisher's website. • The final author version and the galley proof are versions of the publication after peer review. • The final published version features the final layout of the paper including the volume, issue and page numbers. Link to publication General rights Copyright and moral rights for the publications made accessible in the public portal are retained by the authors and/or other copyright owners and it is a condition of accessing publications that users recognise and abide by the legal requirements associated with these rights. • Users may download and print one copy of any publication from the public portal for the purpose of private study or research. • You may not further distribute the material or use it for any profit-making activity or commercial gain • You may freely distribute the URL identifying the publication in the public portal. If the publication is distributed under the terms of Article 25fa of the Dutch Copyright Act, indicated by the “Taverne” license above, please follow below link for the End User Agreement: www.tue.nl/taverne Take down policy If you believe that this document breaches copyright please contact us at: [email protected] providing details and we will investigate your claim. Download date: 02. Oct. 2021 Leslie Lamport’s Logical Clocks: a tutorial Rob R. Hoogerwoord 29 january 2002 Contents 0 Introduction 1 1 Causal precedence and logical clocks 2 1.0 Processes, states, and events .................... 2 1.1 Naming conventions, and some more ............... 3 1.2 Sequential precedence ....................... 4 1.3 Causal precedence ......................... 5 1.4 Local and global states ....................... 8 1.5 Time stamps ............................ 10 1.6 An implementation ......................... 11 1.7 Afterthoughts ............................ 14 2 Distributed Mutual Exclusion 16 2.0 Synchronisation ........................... 16 2.1 Mutual exclusion for two processes ................ 17 2.2 Distributed implementation .................... 21 2.3 Many processes ........................... 22 2.4 Keeping the values dierent .................... 23 2.5 Progress and fairness ........................ 23 3 Epilogue 25 Bibliography 26 i Chapter 0 Introduction In 1978 the American computing scientist Leslie Lamport published a paper [2] in which he introduced so-called logical clocks to synchronize processes in a distributed system. These logical clocks can be used to record (information about) the causal order in which events in the distributed system take place: some events always “occur before” other ones, independent of the (relative) speeds of the processes. In his paper Lamport used this in a fair algorithm for distributed mutual exclusion; here, “fair” means that the order in which requests (to obtain the exclusive right) are granted complies with the causal order of these requests. This note is mainly written for tutorial reasons, not in the least to develop my own understanding of the subject. A minor reason is my dissatisfaction with the cumbersome notation and nomenclature in the existing presentations, not only in Lamport’s original paper but also in recent publications like one by Raynal and Singhal [3]. In addition, this is an exercise in separation of concerns. This note consists of two parts that can be read independently, as these parts are rather self-contained. In Section 1 we introduce causal precedence and Lamport’s logical clocks to record information about causal precedence. In Section 2 we present an algorithm for distributed mutual exclusion. This is not Lamport’s algorithm: to fully understand his algorithm I decided to design it myself, but it so happened that I came up with a simpler, and somewhat more ecient, algorithm. This turned out to be the algorithm designed by Ricart and Agrawala [4] . The connection between the two stories is rather thin: some variables used in the algorithm for mutual exclusion may be viewed as logical clocks, but to understand (the correctness of) the algorithm one does not need to know this. 1 Chapter 1 Causal precedence and logical clocks 1.0 Processes, states, and events An event is a change of state in a (single) process. This process can be part of a distributed collection of processes. The adjective “distributed” means that the state of the system, as a whole, is distributed over the individual processes: each process can only change its own state. Moreover, the state of the system consists exclusively of the states of its processes. The processes communicate by exchanging messages. We assume that sending a message by one process and receiving it by another is not an indi- visible (atomic) action, that is, message transmission is not instantaneous; in this case all messages sent but not yet received are also part of the state of the system. Mathematically, we could model this by introducing additional processes, so-called channels, whose states would represent the messages sent but not yet received. This is, however, not necessary, because we can also, for every process proper, view the states of its outgoing channels as part of the state of that process. Phrased dierently, if we equip every process with two auxiliary variables, one representing the set of messages sent by that process and one representing the set of messages received by that process, then the dierences between the “sent sets” and the “received sets” represent the states of the (directed) channels connecting the processes. In this way, the above view of the state of a distributed system is equally well applicable to systems with non-instantaneous – also called asynchronous – communication. Because both sending a message and receiving a message aect the state 2 rh235c 3 of the sending or receiving process, both sending a message and receiving a message are events, in the above meaning of the word. Notice that the representation with sent sets and received sets complies with the view that each process only changes its own state: a sending process changes its state by adding the message to its sent set, whereas a receiving process changes its state by adding the message to its received set. Hence, the channel state is distributed over sender and receiver in a way that is consistent with our view of a distributed system. 1.1 Naming conventions, and some more To distinguish the processes we assume that they have been properly named. In this note we will use variables p, q, r to range over the process names. In the course of a process the same state transition may occur several times, but such multiple occurrences of the same state transition are considered dierent events. So, for example, a state transition like x := x+1 may occur many times within a process but each individual occurrence of x := x+1isa separate event. To distinguish all events in the system we assume that the events have been properly named, thus making each event unique; in what follows we will use variables e, f, g to denote (names of) events. An event is a state transition in a single process, so each event uniquely identies the process in which it occurs. This is formally rendered by means of a function pr , from events to processes, with the interpretation: pre = “the process in which event e occurs” . Possible events are “sending a message” and “receiving a message”, and for our purposes these are the only events of interest. We shall ignore so- called internal events, representing purely local state changes of processes; if so desired, internal events can be treated as send events. Every message is sent by exactly one process – its sender – and what follows is based upon the assumption that every message is also received by exactly one process – its receiver – . We only make this assumption here to keep the notation as simple as possible, but the assumption is not essential: it is per- fectly admissible to have the same message being received by many (or all) processes in the system – so-called broadcast messages –. Every send event and every receive event involves a unique message; there- fore, these events can be represented by their messages: we also assume that the messages have been properly named. We will use variables l, m, n to de- note (names of) messages. So, every message represents two events, namely 4 rh235c the event of sending it and the event of receiving it. To formalize this we introduce, for every message m : sndm = “the event of sending message m ” sdrm = “the process that sends message m ” rcvm = “the event of receiving message m ” rvrm = “the process that receives message m ” This nomenclature is redundant, but this is an advantage: whenever we wish to take the nature of events into account we use messages to identify events and whenever the nature of events is irrelevant we identify them by their own names. Because of this redundancy we can also formulate a few relations that reect the above interpretations. Because we consider no other events than sending and receiving, we have, for every event e : (∃m :: e = sndm ∨ e = rcvm ) . Furthermore, functions sdr and rvr are related to snd , rcv , and pr as follows – for all m –: sdrm = pr(sndm) rvrm = pr(rcvm) A complete formalisation also requires formulae expressing that all send events dier from all receive events and that dierent messages represent dier- ent events, but for the time being I feel no urge to write down these formulae.