Principles of Concurrent and Distributed Programming
Principles of Concurrent and Distributed Programming
Distributed Programming
Visit the Principles of Concurrent and Distributed Programming, Second
Edition Companion Website at www.pearsoned.co.uk/ben-ari to find
valuable student learning material including:
Source code for all the algorithms in the book
Links to sites where software for studying concurrency may be downloaded.
M. Ben-Ari
Contents
Preface
xi
1.1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2
1.3
Multitasking . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.4
1.5
Multiple computers . . . . . . . . . . . . . . . . . . . . . . . .
1.6
2.1
2.2
2.3
13
2.4
Arbitrary interleaving . . . . . . . . . . . . . . . . . . . . . . .
17
2.5
Atomic statements . . . . . . . . . . . . . . . . . . . . . . . . .
19
2.6
Correctness . . . . . . . . . . . . . . . . . . . . . . . . . . . .
21
2.7
Fairness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
23
2.8
Machine-code instructions . . . . . . . . . . . . . . . . . . . . .
24
2.9
28
2.10
29
2.11
Concurrency in Ada . . . . . . . . . . . . . . . . . . . . . . . .
31
vi
Contents
2.12
Concurrency in Java . . . . . . . . . . . . . . . . . . . . . . . .
34
2.13
36
2.14
37
45
3.1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . .
45
3.2
45
3.3
First attempt . . . . . . . . . . . . . . . . . . . . . . . . . . . .
48
3.4
49
3.5
53
3.6
Second attempt . . . . . . . . . . . . . . . . . . . . . . . . . . .
55
3.7
Third attempt . . . . . . . . . . . . . . . . . . . . . . . . . . .
57
3.8
Fourth attempt . . . . . . . . . . . . . . . . . . . . . . . . . . .
58
3.9
Dekkers algorithm . . . . . . . . . . . . . . . . . . . . . . . .
60
3.10
61
67
4.1
68
4.2
69
4.3
72
4.4
75
4.5
79
4.6
Model checking . . . . . . . . . . . . . . . . . . . . . . . . . .
83
4.7
83
4.8
86
4.9
88
93
5.1
93
5.2
95
5.3
96
Contents
vii
5.4
Fast algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.5
Semaphores
97
107
6.1
6.2
6.3
6.4
6.5
6.6
6.7
6.8
6.9
6.10
6.11
6.12
6.13
6.14
6.15
Monitors
145
7.1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
7.2
7.3
7.4
7.5
7.6
7.7
7.8
7.9
viii
Contents
7.10
7.11
7.12
Channels
179
8.1
8.2
Channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
8.3
8.4
8.5
8.6
Rendezvous . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
8.7
Spaces
197
9.1
9.2
9.3
9.4
9.5
10 Distributed Algorithms
211
10.1
10.2
Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . 215
10.3
10.4
10.5
10.6
10.7
11 Global Properties
11.1
237
Contents
ix
11.2
11.3
11.4
Snapshots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
12 Consensus
257
12.1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
12.2
12.3
12.4
12.5
12.6
12.7
12.8
12.9
285
13.1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
13.2
Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
13.3
13.4
13.5
13.6
13.7
13.8
13.9
Contents
317
321
B.1
B.2
Induction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
B.3
B.4
331
D Software Tools
339
D.1
D.2
D.3
DAJ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
E Further Reading
349
Bibliography
351
Index
355
Supporting Resources
Visit www.pearsoned.co.uk/ben-ari to find valuable online resources
Companion Website for students
Source code for all the algorithms in the book
Links to sites where software for studying concurrency may be downloaded.
For instructors
PDF slides of all diagrams, algorithms and scenarios (with LATEX source)
Answers to exercises
For more information please contact your local Pearson Education sales
representative or visit www.pearsoned.co.uk/ben-ari
Preface
Concurrent and distributed programming are no longer the esoteric subjects for
graduate students that they were years ago. Programs today are inherently concurrent or distributed, from event-based implementations of graphical user interfaces
to operating and real-time systems to Internet applications like multiuser games,
chats and ecommerce. Modern programming languages and systems (including
Java, the system most widely used in education) support concurrent and distributed
programming within their standard libraries. These subjects certainly deserve a
central place in computer science education.
What has not changed over time is that concurrent and distributed programs cannot
be hacked. Formal methods must be used in their specification and verification, making the subject an ideal vehicle to introduce students to formal methods.
Precisely for this reason I find concurrency still intriguing even after forty years
experience writing programs; I hope you will too.
I have been very gratified by the favorable response to my previous books Principles of Concurrent Programming and the first edition of Principles of Concurrent
and Distributed Programming. Several developments have made it advisable to
write a new edition. Surprisingly, the main reason is not any revolution in the principles of this subject. While the superficial technology may change, basic concepts
like interleaving, mutual exclusion, safety and liveness remain with us, as have
the basic constructs used to write concurrent programs like semaphores, monitors,
channels and messages. The central problems we try to solve have also remained
with us: critical section, producerconsumer, readers and writers and consensus.
What has changed is that concurrent programming has become ubiquitous, and this
has affected the choice of language and software technology.
Language: I see no point in presenting the details of any particular language or
system, details that in any case are likely to obscure the principles. For that reason,
I have decided not to translate the Ada programs from the first edition into Java
programs, but instead to present the algorithms in pseudocode. I believe that the
high-level pseudocode makes it easier to study the algorithms. For example, in the
xi
xii
Preface
Preface
xiii
I have chosen to present the Spin model checker because, on the one hand, it is
a widely-used industrial-strength tool that students are likely to encounter as software engineers, but on the other hand, it is very friendly. The installation is trivial and programs are written in a simple programming language that can be easily
learned. I have made a point of using Spin to verify all the algorithms in the book,
and I have found this to be extremely effective in increasing my understanding of
the algorithms.
An outline of the book: After an introductory chapter, Chapter 2 describes the
abstraction that is used: the interleaved execution of atomic statements, where the
simplest atomic statement is a single access to a memory location. Short introductions are given to the various possibilities for studying concurrent programming:
using a concurrency simulator, writing programs in languages that directly support
concurrency, and working with a model checker. Chapter 3 is the core of an introduction to concurrent programming. The critical-section problem is the central
problem in concurrent programming, and algorithms designed to solve the problem
demonstrate in detail the wide range of pathological behaviors that a concurrent
program can exhibit. The chapter also presents elementary verification techniques
that are used to prove correctness.
More advanced material on verification and on algorithms for the critical-section
problem can be found in Chapters 4 and 5, respectively. For Dekkers algorithm,
we give a proof of freedom from starvation as an example of deductive reasoning
with temporal logic (Section 4.5). Assertional proofs of Lamports fast mutual exclusion algorithm (Section 5.4), and Barzs simulation of general semaphores by
binary semaphores (Section 6.10) are given in full detail; Lamport gave a proof that
is partially operational and Barzs is fully operational and difficult to follow. Studying assertional proofs is a good way for students to appreciate the care required to
develop concurrent algorithms.
Chapter 6 on semaphores and Chapter 7 on monitors discuss these classical concurrent programming primitives. The chapter on monitors carefully compares the
original construct with similar constructs that are implemented in the programming
languages Ada and Java.
Chapter 8 presents synchronous communication by channels, and generalizations
to rendezvous and remote procedure calls. An entirely different approach discussed
in Chapter 9 uses logically-global data structures called spaces; this was pioneered
in the Linda model, and implemented within Java by JavaSpaces.
The chapters on distributed systems focus on algorithms: the critical-section problem (Chapter 10), determining the global properties of termination and snapshots
(Chapter 11), and achieving consensus (Chapter 12). The final Chapter 13 gives
an overview of concurrency in real-time systems. Integrated within this chapter
xiv
Preface
are descriptions of software defects in spacecraft caused by problems with concurrency. They are particularly instructive because they emphasize that some software
really does demand precise specification and formal verification.
A summary of the pseudocode notation is given in Appendix A. Appendix B reviews the elementary mathematical logic needed for verification of concurrent programs. Appendix C gives a list of well-known problems for concurrent programming. Appendix D describes tools that can be used for studying concurrency: the
BACI concurrency simulator; Spin, a model checker for simulating and verifying
concurrent programs; and DAJ, a tool for constructing scenarios of distributed algorithms. Appendix E contains pointers to more advanced textbooks, as well as
references to books and articles on specialized topics and systems; it also contains
a list of websites for locating the languages and systems discussed in the book.
Audience: The intended audience includes advanced undergraduate and beginning graduate students, as well as practicing software engineers interested in obtaining a scientific background in this field. We have taught concurrency successfully to high-school students, and the subject is particularly suited to nonspecialists because the basic principles can be explained by adding a very few
constructs to a simple language and running programs on a concurrency simulator.
While there are no specific prerequisites and the book is reasonably self-contained,
a student should be fluent in one or more programming languages and have a basic
knowledge of data structures and computer architecture or operating systems.
Advanced topics are marked by A . This includes material that requires a degree of
mathematical maturity.
Chapters 1 through 3 and the non-A parts of Chapter 4 form the introductory core
that should be part of any course. I would also expect a course in concurrency
to present semaphores and monitors (Chapters 6 and 7); monitors are particularly
important because concurrency constructs in modern programming languages are
based upon the monitor concept. The other chapters can be studied more or less
independently of each other.
Exercises: The exercises following each chapter are technical exercises intended
to clarify and expand on the models, the algorithms and their proofs. Classical
problems with names like the sleeping barber and the cigarette smoker appear in
Appendix C because they need not be associated with any particular construct for
synchronization.
Supporting material: The companion website contains an archive with the
source code in various languages for the algorithms appearing in the book. Its
address is:
http://www.pearsoned.co.uk/ben-ari.
Preface
xv
Lecturers will find slides of the algorithms, diagrams and scenarios, both in readyto-display PDF files and in LATEX source for modification. The site includes instructions for obtaining the answers to the exercises.
Acknowledgements: I would like to thank:
Yifat Ben-David Kolikant for six years of collaboration during which we
learned together how to really teach concurrency;
Pieter Hartel for translating the examples of the first edition into Promela,
eventually tempting me into learning Spin and emphasizing it in the new
edition;
Pieter Hartel again and Hans Henrik Lvengreen for their comprehensive
reviews of the manuscript;
Gerard Holzmann for patiently answering innumerable queries on Spin during my development of jSpin and the writing of the book;
Bill Bynum, Tracy Camp and David Strite for their help during my work on
jBACI;
Shmuel Schwarz for showing me how the frog puzzle can be used to teach
state diagrams;
The Helsinki University of Technology for inviting me for a sabbatical during which this book was completed.
M. Ben-Ari
Rehovot and Espoo, 2005