This manuscript contains the complete text of Part I of Concurrent programming in Erlang Programming techniques for programming concurrent real time systems have

Transcription:

This page is intentionally plank,Concurrent Programming. Second Edition,Joe Armstrong,Robert Virding,Claes Wikstro m. Mike Williams,Telecommunications Systems Laboratories. S 125 25 A lvsjo,PRENTICE HALL,Englewood Cliffs New Jersey 07632. Preface viii,Acknowledgments x,Introduction 1,I Programming 7. 1 An ERLANG Tutorial 9,1 1 Sequential Programming 9. 1 2 Data Types 12,1 3 Pattern Matching 13,1 4 Built in Functions 15. 1 5 Concurrency 15,2 Sequential Programming 18,2 1 Terms 18. 2 2 Pattern Matching 21,2 3 Expression Evaluation 23. 2 4 The Module System 25,2 5 Function Definition 26. 2 6 Primitives 31,2 7 Arithmetic Expressions 34,2 8 Scope of Variables 35. 3 Programming with Lists 37,3 1 List Processing BIFs 37. vi Contents,3 2 Some Common List Processing Functions 38. 3 3 Examples 41,3 4 Common Patterns of Recursion on Lists 47. 3 5 Functional Arguments 50,4 Programming with Tuples 52. 4 1 Tuple Processing BIFs 52,4 2 Multiple Return Values 53. 4 3 Encrypting PIN Codes 54,4 4 Dictionaries 56,4 5 Unbalanced Binary Trees 58. 4 6 Balanced Binary Trees 62,5 Concurrent Programming 67. 5 1 Process Creation 67,5 2 Inter process Communication 68. 5 3 Timeouts 75,5 4 Registered Processes 78,5 5 Client Server Model 78. 5 6 Process Scheduling Real time and Priorities 83. 5 7 Process Groups 84,6 Distributed Programming 85. 6 1 Motivation 85,6 2 Distributed mechanisms 86,6 3 Registered Processes 88. 6 4 Connections 88,6 5 A Banking Example 88,7 Error Handling 91. 7 1 Catch and Throw 91,7 2 Process Termination 95,7 3 Linked Processes 96. 7 4 Run time Failure 100, 7 5 Changing the Default Signal Reception Action 102. 7 6 Undefined Functions and Unregistered Names 106. 7 7 Catch Versus Trapping Exits 108,8 Programming Robust Applications 109. 8 1 Guarding Against Bad Data 109,8 2 Robust Server Processes 111. 8 3 Isolating Computations 114,8 4 Keeping Processes Alive 115. 8 5 Discussion 115,Contents vii,9 Miscellaneous Items 118. 9 1 Last Call Optimisation 118,9 2 References 121,9 3 Code Replacement 121. 9 4 Ports 123,9 5 Binaries 130,9 6 Process Dictionary 132. 9 7 The Net Kernel 133,9 8 Hashing 136,9 9 Efficiency 138. II Applications 141,Bibliography 143,A ERLANG Reference Grammar 145. B Built in Functions 150,B 1 The BIFs 151,B 2 BIFs Sorted by Type 172. C The Standard Libraries 176,C 1 io 176,C 2 file 177. C 3 lists 178,C 4 code 179,D Errors in ERLANG 180,D 1 Match Errors 180. D 2 Throws 181,D 3 Exit signals 181,D 4 Undefined Functions 182. D 5 The error logger 182,E Drivers 183, Erlang1 is a declarative language for programming concurrent and distributed. systems which was developed by the authors at the Ericsson and Ellemtel Computer. Science Laboratories, The development of Erlang started as an investigation into whether modern. declarative programming paradigms could be used for programming large industrial. telecommunications switching systems It was soon realised that languages which. were suitable for programming telecommunications systems were also suitable for. a wide range of industrial embedded real time control problems. Many of the Erlang primitives provide solutions to problems which are com. monly encountered when programming large concurrent real time systems The. module system allows the structuring of very large programs into conceptually. manageable units Error detection mechanisms allow the construction of fault. tolerant software Code loading primitives allow code in a running system to be. changed without stopping the system 2, Erlang has a process based model of concurrency Concurrency is explicit. and the user can precisely control which computations are performed sequentially. and which are performed in parallel Message passing between processes is asyn. chronous that is the sending process continues as soon as a message has been. The only method by which Erlang processes can exchange data is message. passing This results in applications which can easily be distributed an applica. tion written for a uniprocessor can easily be changed to run on a multiprocessor. or network of uniprocessors The language has built in mechanisms for distributed. programming which makes it easy to write applications which can run either on a. Agner Krarup Erlang 1878 1929 was a Danish mathematician who developed a theory of. stochastic processes in statistical equilibrium his theories are widely used in the telecommuni. cations industry, This is very important in embedded real time systems such as telephone exchanges or air. traffic control systems such systems should not normally be stopped for software maintenance. single computer or on a network of computers, Variables in Erlang have the property of single assignment3 once a value has. been assigned to a variable this value can never be changed This property has. important consequences when debugging or transforming a program. Programs are written entirely in terms of functions function selection is made. by pattern matching which leads to highly succinct programs. The Erlang system has an inbuilt notion of time the programmer can specify. how long a process should wait for a message before taking some action This. allows the programming of real time applications Erlang is suitable for most. soft real time applications where response times are in the order of milliseconds. Current information about Erlang can be obtained from the World Wide Web. at http www ericsson se erlang e mail requests for information can be sent. to erlang erix ericsson se, Commercially supported implementations of Erlang can be obtained from. Ericsson Software Technology AB For information please send e mail to. erl biz erlang ericsson se,Joe Armstrong,Robert Virding. Claes Wikstro m,Mike Williams,Computer Science Laboratory. Ericsson Telecommunications Systems Laboratories,S 125 25 A lvsjo. erlang erix ericsson se, Also called write once variables or non destructive assignment. Acknowledgments, The ideas in Erlang are difficult to trace to a single source Many features of the. language have been influenced and improved as a result of comments by our friends. and colleagues of the Computer Science Laboratory and we would like to thank. them all for their help and advice In particular we would like to thank Bjarne. Da cker Head of the Computer Science Laboratory for his enthusiastic support. and encouragement and for the help he has provided in spreading the language. Many people have made contributions to this book Richard Ehrenborg wrote. the code for AVL trees in Chapter Per Hedeland wrote pxw which is described. in Chapter Roger Skagervall and Sebastian Strollo provided the ideas behind. the object oriented programming methods described in Chapter Carl Wilhelm. Welin wrote an LALR 1 parser generator in Erlang which generates Erlang. and provided the reference grammar contained in Appendix A. Early users in particular the first group of users ingen na mnd ingen glo md. at Ericsson Business Systems in Bollmora stoically acted as guinea pigs and did. battle with many early and mutually incompatible versions of the Erlang system. Their comments have helped us greatly, We would like to thank Torbjo rn Johnson from Ellemtel and Bernt Ericson from. Ericsson Telecom without whose unfailing support Erlang would not have seen. the light of day, This book was typeset in LaTEX with the macro package ph sty provided by. Richard Fidczuk from Prentice Hall Comp text tex also helped answer our na ve. UNIX is a registered trademark of AT T Bell Laboratories X Window Sys. tem is a trademark of MIT,Introduction, Erlang is a new programming language which was designed for programming. concurrent real time distributed fault tolerant systems. Programming techniques for programming concurrent real time systems have. for many years lagged behind those techniques used for programming sequential. applications When the use of languages such as C or Pascal was standard practice. for programming sequential applications most programmers of real time systems. were still struggling with assembly languages Today s real time systems can be. written in languages such as Ada Modula2 Occam etc in which there are explicit. constructs for programming concurrency or in languages such as C which lack. constructs for concurrency, Our interest in concurrency is motivated by a study of problems which exhibit a. large degree of natural concurrency This is a typical property of real time control. problems The Erlang programmer explicitly specifies which activities are to be. represented as parallel processes This view of concurrency is similar to that found. in Occam CSP Concurrent Pascal etc but dissimilar to concurrent languages. where the prime motivation for introducing concurrency is not for modelling real. world concurrency but for obtaining higher performance by compiling programs. for execution on a parallel processor, Languages such as Prolog and ML are now used for a wide range of. industrial applications and have resulted in dramatic reductions in the total effort. required to design implement and maintain applications We have designed and. implemented Erlang to enable the programming of concurrent real time systems. at a similarly high level, Declarative syntax Erlang has a declarative syntax and is largely free from. side effects, Concurrent Erlang has a process based model of concurrency with asynchron. ous message passing The concurrency mechanisms in Erlang are light. weight i e processes require little memory and creating and deleting pro. cesses and message passing require little computational effort. 2 Introduction, Real time Erlang is intended for programming soft real time systems where. response times in the order of milliseconds are required. Continuous operation Erlang has primitives which allow code to be replaced. in a running system and allow old and new versions of code to execute at the. same time This is of great use in non stop systems telephone exchanges. air traffic control systems etc where the systems cannot be halted to make. changes in the software, Robust Safety is a crucial requirement in systems such as the above There are. three constructs in the language for detecting run time errors These can be. used to program robust applications, Memory management Erlang is a symbolic programming language with a. real time garbage collector Memory is allocated automatically when re. quired and deallocated when no longer used Typical programming errors. associated with memory management cannot occur, Distribution Erlang has no shared memory All interaction between processes. is by asynchronous message passing Distributed systems can easily be built. in Erlang Applications written for a single processor can without diffi. culty be ported to run on networks of processors, Integration Erlang can easily call or make use of programs written in other. programming languages These can be interfaced to the system in such a. way that they appear to the programmer as if they were written in Erlang. We have freely borrowed ideas from declarative and concurrent programming. languages The early syntax of Erlang owed much to STRAND though the. current syntax is more reminiscent of an untyped ML The model of concurrency. is similar to that of SDL, Our goal was to produce a small simple and efficient language suitable for pro. gramming robust large scale concurrent industrial applications Thus for reasons. of efficiency we have avoided many features commonly found in modern functional. or logic programming languages Currying higher order functions lazy evalua. tion ZF comprehension logical variables deep guards etc add to the expressive. power of a declarative programming language but their absence is not a significant. detriment to the programming of typical industrial control applications The use. of a pattern matching syntax and the single assignment property of Erlang. variables leads to clear short and reliable programs. Erlang was designed at the same time as its first implementation which was an. interpreter written in Prolog We were fortunate in having an enthusiastic group. of users who were at the same time developing a prototype of a new telephone. This resulted in an extremely pragmatic approach to language design Con. structs which were not used were removed New constructs were introduced to. solve problems which had caused our users to write convoluted code Despite the. fact that we often introduced backwardly incompatible changes to the language. our users had soon produced tens of thousands of lines of code and were actively. Introduction 3, encouraging others to use the language Some of the results of their labours in. producing a new way of programming telephone exchanges have been published in. The first Prolog based interpreter for Erlang has long since been abandoned. in favour of compiled implementations One of these implementations is available. free of charge but is subject to non commercial licensing The present generation. of Erlang implementations meets our real time requirements as regards speed. and lightweight concurrency Erlang implementations have been ported to and. run on several operating systems and several processors. Erlang is suitable for programming a wide range of concurrent applications. Several tools have been written to support Erlang programming for example. interfaces to the X Windows System and ASN 1 compiler written in Erlang and. generating Erlang parser generators debuggers, This book is intended for people who are interested in real time control systems and. have some previous programming experience Previous knowledge of functional or. logic languages is not necessary, The material in the book is loosely based on an Erlang course which has been. held many times in recent years at Ericsson and its subsidiary companies world. wide and at several Swedish universities This course takes four days which is more. than sufficient to teach not only the language but also many of the paradigms used. in Erlang programming The last day of the course usually has a programming. exercise in which the students write a control system for a telephone exchange. similar to that described in Chapter and run it on a real exchange. The book is divided into two main parts The first part Programming introduces. the Erlang language and some of the most commonly used paradigms when. programming in Erlang The second part Applications has a number of self. contained chapters containing case studies of typical Erlang applications. Programming, Chapter is a tutorial introduction to Erlang The major ideas in the language. are introduced through a series of examples, Chapter introduces sequential programming The module system is intro. duced as is the basic terminology used when we talk about Erlang programs. 4 Introduction, Chapters and contain examples of sequential programming with lists and. tuples Basic list and tuple programming techniques are introduced Several stand. ard modules which will be used later in the book are introduced These include. modules for implementing sets dictionaries balanced and unbalanced binary trees. Chapter introduces concurrency Sequential Erlang needs the addition of. a small number of primitives to turn it into a concurrent programming language. We introduce the primitives necessary to create a parallel process and for message. passing between processes We also introduce the idea of a registered process which. allows us to associate a name with a process, The basic ideas behind the client server model are explained This model is. often used in later chapters and is one of the basic programming techniques for. coordinating the activities of several parallel processes We also introduce timeouts. which can be used for writing programs which have real time behaviour. Chapter has a general introduction to distributed programming where we. explain some of the reasons for writing distributed applications We describe the. language primitives which are needed to write distributed Erlang programs and. explain how sets of Erlang process can be arranged to run on a network of. Erlang nodes, Chapter explains the error handling mechanisms available in Erlang We. have designed Erlang for programming robust applications and the language. has three orthogonal mechanisms for detecting errors We take the view that. the language should detect as many errors as possible at run time and leave the. responsibility for correction of such errors to the programmer. Chapter shows how the error handling primitives introduced in the previous. chapter can be used to build robust and fault tolerant systems We show how to. protect against faulty code provide a fault tolerant server by extending the client. server model and show how to isolate a computation so as to limit the extent of. any damage caused if it should fail, Chapter is a collection of ideas and programming techniques not introduced. elsewhere in the book We start with a discussion of the last call optimisation An. understanding of this optimisation is essential if the programmer wishes to write. correct code for non terminating software We then introduce references which. provide unique unforgeable symbols The next two sections in this chapter contain. details of how to change Erlang code in a running system this is needed for. writing non stop systems and how to interface Erlang to programs written in. other languages Following this we discuss binaries which are used for efficiently. handling large quantities of untyped data the process dictionary which provides. each process with simple destructive storage capabilities and the net kernel which. is the basis of distributed Erlang Finally we discuss efficiency giving examples.