Professional Documents
Culture Documents
Joe Armstrong
Computer Science Laboratory
Ericsson Telecommunications Systems Laboratory
Box 1505
S - 125 25 A lvsjo
Sweden
joe@erix.ericsson.se
September 18, 1996
1
putation in Erlang is performed within a process. Erlang started ARM92a] as a Prolog meta-
Processes have no shared memory and communi- interpretor which added a notion of concurrency to
cate by asynchronous message passing. An Er- Prolog. Having started as a set of extensions to Pro-
lang system running on one processor can cre- log, Erlang slowly drifted from the logic to functional
ate a parallel process running on another system school of programming. Erlang has many of the fea-
(which need not even be the same processor or op- tures found in a modern functional language (higher
erating system) and thereafter communicate with order functions, list comprehensions etc.). It diers
this process. from most modern functional languages by having a
dynamic type system (inherited from Prolog) and an
Garbage collection - Erlang is used to program eager evaluator.
real-time systems. Long garbage collection de-
lays in such systems are unacceptable. Erlang 2 Sequential Erlang programs
implementations are written using bounded-time Erlang programs are composed of modules in which
garbage techniques, some of these techniques are functions are dened. Functions are written as sets of
described in ARM95b, VIR95]. recursion equations. The familiar factorial function is
written:
Incremental code loading - Users can control
in detail how code is loaded. In embedded sys- -module(math).
tems, all code is usually loaded at boot time. In
development systems, code is loaded when it is -export(fac/0]).
needed. If testing uncovers bugs, only the faulty
code need be replaced. fac(N) when N > 0 -> N * fac(N-1)
Robustness - The Erlang abstract machine fac(0) -> 1.
has three independent error detection primitives This can be evaluated in the Erlang shell as follows:
which can be used to structure fault-tolerant sys-
tems. One of these mechanisms allows processes
to monitor the activities of other processes, even if > math:fac(25).
these processes are executing on other processors. 15511210043330985984000000
We can group processes together in distributed The \>" symbol is the Erlang shell prompt, the
systems and use these as building blocks in de- line following the prompt is the value returned by the
signing distributed transaction oriented systems. Erlang evaluator.
Timing - Erlang has mechanisms for allowing The annotation -module(Name) denotes the mod-
processes to timeout while waiting for events and ule name and -export(Funs) is a list of the functions
to read a real-time clock. which this module exports. Functions which are not
declared in the export list cannot be called from out-
External Interfaces - Erlang has a "port" side the module.
mechanism which allows processes to communi- In what follows we will often omit the module dec-
cate with the outside world in a manner which is larations where they are implied by the text.
semantically equivalent to internal message pass- A more complex example might be a function to
ing between Erlang processes. This mechanism is search for a value in a binary tree:
used for communication with the host operating
system and for interaction with other processes lookup(Key, {Key, Val, _, _}) ->
(perhaps written in other languages), which run {ok, Val}
on the host operating system. If required for rea- lookup(Key,{Key1,Val,S,B}) when Key<Key1 ->
sons of eciency, a special version of the \port" lookup(Key, S)
concept allows other software to be directly linked lookup(Key, {Key1,Val,S,B}) ->
into the abstract machine. Examples of the use lookup(Key, B)
of the port mechanism are interacting with the lookup(Key, nil) ->
host le system, interfacing to a graphical inter- not_found.
face and a low level socket interface.
Nodes in the tree are are either tuples of the form
The execution mechanisms listed above are pro- {Key,Value,S,B} or the atom nil. S are trees with
vided by the Erlang abstract machine. Programs keys smaller than and B are trees with keys bigger
Key
which use these mechanisms are completely portable or equal to .
Key
between implementations of Erlang running on dier- Lists are written as in Prolog - as examples the
ent operating systems and processors. familiar append and member functions are written:
append(H|T], L) -> H|append(T,L)] map(F, H|T]) -> F(H)|map(T)]
append(], L) -> L. map(F, ]) -> ]
lists - for storing a variable number of objects, Where ++ is the inx append operator.
thus: a, 23, hi]
3 Concurrent Erlang Programs
Functional objects are introduced with the syntax: Erlang is a concurrent programming language {
parallel activities can be programmed in Erlang itself
fun(Arg1,..,ArgN) -> ... end. and do not make use of any concurrency mechanisms
in the host operating system.
So, for example, the following sequence of expres- Erlang has a process model of concurrency. New
sions: process are created by evaluating the Erlang primitive
spawn send (written with the inx operator !) sends
K = 2, a message to a process and receive can be used to
F = fun(X) -> X * K end, receive a message.
creates a functional object X * K which is bound to The primitive spawn(M, F, A1, A2,..,An]) cre-
the variable F. In this expression X is a free variable ates a new parallel process. This process evaluates the
and K is bound to the integer 2. function M:F(A1,A2,..,An). When the function eval-
We can evaluate F with the syntax F(Args). uation has completed the process dies.
Functions (funs) are rst class objects - they can We can write an echo process thus:
be passed as arguments to other functions or can be
returned by functions. -module(echo).
To illustrate this suppose we dene the higher order -export(start/0, loop/0]).
function adder(C) as follows: start() ->
adder(C) -> fun(X) -> X + C end. spawn(echo, loop, ]).
-export(start/0, server/1]).
The module bank_client is used to access the To turn the bank client-server model from a sin-
server: gle processor solution to a fully distributed program
we make a single line change to the code in the
-module(bank_client). bank_client module, changing the line:
-export(ask/1, deposit/2, withdraw/2]).
bank_server ! {self(), Msg},
ask(Who) ->
rpc({ask, Who}). to:
deposit(Who, Amount) ->
rpc({deposit, Who, Amount}).
{bank_server,'Host@Domain'} ! {self(), Msg}
withdraw(Who, Amount) ->
rpc({withdraw, Who, Amount}). where Host@Domain is the name of the node where the
bank server is running.
rpc(Msg) -> Note that no data transfer languages are needed to
bank_server ! {self(), Msg}, describe the structure of the messages which are sent
receive between the dierent systems. The set of messages to
{bank_server, Reply} -> be sent does not need to be known in advance but is
Reply encoded at run-time. Problems of confusion between
end. dierent byte orderings on dierent machines and dif-
ferent representations of data structures in memory
Note that remote procedure calls are not primitives are eliminated.
but are programmed in Erlang itself.
4 Distributed Erlang Programs 5 Error Handling in Erlang
Erlang has three dierent mechanism for trapping
Erlang applications are often designed to run on run-time errors, these can be used to:
distributed networks of processors. We use the term
node to mean a complete self-contained Erlang system. Monitor the evaluation of an expression.
In a distributed network one or more Erlang nodes
may run on a single computer. Monitor the behaviour of a process.
In designing the language we were careful to avoid Raise an exception when an undened function is
any primitives which would not map naturally onto a called.
distributed model of computation. The only way of
exchanging data between process is by message pass- Suppose we evaluate N/M where M is equal to zero,
ing. All the primitives involving processes work trans- this will cause a run-time error and the process in
parently on a network of Erlang nodes. which this evaluation is performed will die.
Inter-processor communication makes use of a so- We can detect this error in one of two ways. The
phisticated caching technique WIK94] which makes rst method makes use of the primitives catch and
data transfer highly ecient. Indeed a cross-processor throw which are used for monitoring the evaluation of
remote procedure call in Erlang is often more ecient an expression.
than in a language such as C. The expression X = catch N/M evaluates to the tu-
Recall that the primitive spawn creates a new ple {'EXIT', What} where What is an error descriptor,
parallel process. The new process is always in this case the term:
created on the current node. To create a
parallel process on a dierent node the primi- {badarith,{erl_eval,eval_op,'/',1,0]}}}
tive Pid = spawn(N, Mod, Func, ArgList) is used.
This creates a new parallel process on the Erlang node catch can be though of as a meta-function which
N. This allows an Erlang process running on one Er- changes the normal evaluation model by converting
lang node to create a parallel process on another node errors into terms which can be manipulated by the
in the system. All operations on the new process be- program.
have in exactly the same way as if the process had If no error occurs then the result of evaluating
been created on the local node. catch Expr is the same as that of evaluating Expr.
Recall that that register primitive created a The primitive throw(Expr) evaluates Expr which
global alias for a process identier, the scope of this then becomes the return value of any enclosing catch.
alias is the node on which the process was registered, Using catch and throw programs can be written to
so this name is not visible outside the node. monitor and correct local errors within a process.
To send a message to a registered process on a dif- The second error handling mechanism determines
ferent node the syntax {Atom, Node} ! Msg is used. what happens when a process dies. Suppose some code
This sends Msg to the process named Atom on Node. evaluating in a process generates an error and that
this error is not handled using the catch mechanism exit({undef_fun,{Mod,Func,Args}})
described above. false ->
If the process Pid1 evaluates the expression case code:load_file(Mod) of
link(Pid2) then a link is established between these {module, _} ->
two processes. apply(Mod, Func, Args)
When any process dies it broadcasts a message con- {error, _} ->
taining information about why it died to all processes exit({undef_mod, Mod})
in the system to which it is linked. Thus if Pid1 dies, end
a message containing the reason for death is sent to end.
the process Pid2. link is symmetric, so if Pid2 dies
an error message will be sent to Pid1. The point to note here is not this particular code,
As an example, the function monitor(M,F,Args) but rather the fact that the user can program exactly
creates two parallel processes. One of these processes what happens when a program tries to execute code
evaluates the expression M:F(Args) the other moni- that has not been loaded into the system.
tors the process performing the evaluation. If the pro- Dierent types of system, for example, embedded
cess performing the evaluation dies, a new process is systems, or, development systems need dierent types
created by the monitoring process. of code loading strategy. The above mechanism allows
-module(monitor). code loading strategies to be programmed by the user
in Erlang itself.
-export(monitor/3, start_monitor/3]).
6 The Industrial Use of Erlang
monitor(M, F, A) -> Erlang was developed at the Ericsson Computer
spawn(monitor, start_monitor, M, F, A]). Science Laboratory and started to spread outside the
lab. for internal use in Ericsson in 1987. The rst
start_monitor(Mod, Func, Args) -> projects were simple prototypes written using a very
process_flag(trap_exit, true), slow version of Erlang which was implemented in Pro-
restart(Mod, Func, Args). log.
Work with the interpretor led to the development
restart(Mod, Func, Args) -> of a much faster Erlang machine ARM92b] which was
Pid = spawn_link(Mod, Func, Args), loosely based on the WAM with extensions to handle
receive
{Pid, 'EXIT', Why} ->
process creation and message passing.
%% the monitored process died
The availability of a faster Erlang implementation
encouraged the spread of the language within Ericsson
%% start a new process ...
restart(Mod, Func, Args)
and a number of experimental projects were started.
end.
At the time of writing some of these have developed
into full-scale Ericsson products.
The primitive process_flag(trap_exit, true) In 1994 the rst International Erlang Conference
allows the evaluating process to trap exit messages. was held in Stockholm. This conference, which pub-
spawn_link creates a new parallel process and links lishes no proceedings has been held every year since
to it in an atomic action. 1994. The 1995 conference attracted 160 delegates
The third error handling mechanism is used to trap from 10 dierent countries. The Erlang conference is
errors occurring when undened functions are called. the principle forum within Ericsson for reporting work
If an attempt is made to evaluate M:F(Args) and no done with Erlang.
code for the module M has been loaded into the system By 1995 three projects had matured into stable
then the function undefined_function(M,F,Args) in commercial products, these were:
the module error_handler is called.
error_handler is a normal Erlang function. A typ- NETSim - Network Element Test Simulator.
ical denition might be:
-module(error_handler). Mobility Server - The Mobility Server is a fully
-export(undefined_function/3]). featured PBX.
undefined_function(Mod, Func, Args) -> Distributed Resource Controller - the dis-
case code:is_loaded(Mod) of tributed resource controller (DRC) is a scalable,
{file,File} -> robust resource controller written in distributed
% the module is loaded Erlang and running on Windows-NT.
6.1 NetSim with cellular and PSTN phones, wide-area or
The rst commercial product developed using Er- local paging systems, and fax/fax mailboxes.
lang was a network simulator which simulates the
maintenance and operations behaviour of an Ericsson Interestingly, nowhere in the Ericsson press re-
AXE exchange. NetSim was released in 1994. At the leases was there any mention that the system was pro-
time of writing this product is in use at about twenty grammed in Erlang!
sites in several countries. The Mobility Server is the largest system pro-
6.2 Mobility Server grammed in Erlang yet written and contains over
In February 1995, Ericsson Business Networks 230,000 lines of production quality Erlang code. At
launched the Consono R Personal Mobility product the time of writing we believe this to be the largest
range.1 The principle component in the Consono sys- functional program ever written, it also demolishes the
tem is a device called the Mobility Server, most of the argument that "functional programming languages
code in the Mobility Server is written in Erlang. cannot be used for large-scale projects".
The Ericsson press release ERI96] at CeBIT-96 The Mobility Server is a good example of a non-
summarised some of the features of the Mobility numerical, non-scientic system problem which can be
Server: programmed in Erlang.
Ericsson's solution is a new system concept 6.2.1 Technical Details
called a Mobility Server, which acts as an in-
telligent call control system when linked to The Mobility Server (MS) is an interesting example of
a company PBX. The Mobility Server is pri- a large function program. Here are some details of the
marily designed as an add-on to the Ericsson program:
Consono MD110 PBX, although it can also
be used with PBX systems from other sup- The MS code was organised into 486 Erlang mod-
pliers. ules, containing 230,000 lines of commented Er-
One of the principal attractions of the Mo- lang code integrated with 20,000 lines of C.
bility Server is the introduction of Personal
Number services for all mobile users. Each The Erlang source code was 6.6 Mbytes of text,
employee is able to publish a single number, when compiled and loaded into the system it oc-
to which all phone calls and faxes can be sent. cupied 1.2 Mbytes of code space.
The person can then control when and where
and from whom they want to receive phone The system runs on a 40MHz Force 5TE 40Mhz
and fax calls. with 32 Mbytes of memory and the VxWorks op-
The Mobility Server automatically routes the erating system.
calls appropriately, to an extension within The MS was written by a team of 35 Program-
the company PBX, or to an external xed mers. These programmers had no previous ex-
or mobile phone in the public network. The perience. They were helped by four consultants
routing can be selective, so that only certain from \Erlang Systems".2
calls are directed to the person's extension,
with all other calls going to a secretary or The project started in a small way in 1987 as
voice-mail system. an experiment in prototyping using a very slow
The mobility server can be utilized by organ- Erlang interpretor written in Prolog.
isations with as few as 60 extensions but is
expected to be particularly attractive to or- 6.3 Distributed Resource Controller
ganisations with 400 or more extensions, and The DRC is written in distributed Erlang and runs
highly mobile workforces for whom the qual- on a network of Windows-NT machines. The DRC
ity and exibility of communications is im- has a robust scalable architecture and runs on low-
portant. Examples include sales and service cost hardware. The resource which are controlled are
organisations, and businesses with employees a collection of various audio devices, human operators
who telework, spending some or all of their together with various database systems.
time working from home. The allocation of resources is achieved using a dedi-
The services oered can be used with wired cated scripting language - the meta-interpreter for the
or cordless PBX extensions, and externally scripting language is written in Erlang.
1 Consono is a trademark owned by Telefonaktiebolaget L M 2 Erlang Systems is a company owned by Ericsson which was
Ericsson for marketing and sales of business communications set up to market Erlang and to provide consulting and training
systems by Ericsson. in Erlang.
The rst functioning system is currently in use on native code compilation of Erlang and porting Er-
two sites in Denmark where it is used by 638 operators lang to run on multi-processor architectures.
and controls 400 audio ports.
This project was delivered to the end-user and had Ecient implementation of protocol software in
completed its acceptance tests in less than a year after Erlang. We are investigating how to implement
the requirements had been agreed upon. TCP/IP directly in Erlang.
Programming took 9,000 man hours and testing Typed Erlang. A prototype type checker for Er-
2,000 man hours with seven programmers and two lang has been written by Simon Marlow and Phil
testers. The resulting program has 40 Erlang mod- Wadler of the University of Glasgow MAR96].
ules, two external C programs and 12,800 lines of Er-
lang code. A free version of Erlang ERL96] can be downloaded
7 Comments from the WWW.
7.1 Time to market Acknowledgments
Although the actual programming phase of the soft- I would like to thank Mike Williams and Peter
ware development process is only a very small part of Hogfeldt for their help in preparing this paper.
the total design work the choice of programming lan-
guage and tools has a profound eect on the total life References
cycle costs. Use of Erlang in a number of dierent
projects has dramatically shortened time to market ARM92a] Armstrong, J.L., Virding, S.R. and
and greatly improved the quality of delivered software. Williams, M.C. Use of Prolog for developing a new
7.2 Performance programming language The Practical Application of
Erlang is suciently fast for delivering real telecom- Prolog - 1 { 3 April 1992. Institute of Electrical En-
munication products. gineers, London.
Interestingly Erlang rarely does well in small bench- ARM92b]Armstrong, J.L., Dacker, B.O., Virding,
mark comparisons with C. C programs are usually S. R., and M. C. Williams, M. C. Implementing a
faster than the equivalent Erlang programs. functional language for highly parallel real-time appli-
However, when talking about large systems the op- cations Software Engineering for Telecommunication
posite seems to be true { usually the Erlang programs Switching Systems. March 30 { April 1, 1992 Flo-
are faster than the C. The reasons for this are not well rence.
understood. ARM95a] Concurrent Programming in Erlang, Joe
Armstrong, Mike Williams, Robert Virding and Claes
7.3 Real-time garbage collection Wikstrom. Prentice Hall, 1995.
It seems to be a widely accepted myth that garbage ARM95b] Armstrong, J.L., Virding, S.R., One
collected languages are unsuitable for programming pass real-time generational mark-sweep garbage col-
real-time control systems. lection. International Workshop on Memory Man-
In our experience the opposite appears to be true. agement (IWMM'95) September 27-29, 1995 Kinross,
In implementing large control systems in conventional Scotland
languages problems of memory leakage and fragmen- ERI96] http://www.ericsson.se/Eripress/
tation are extremely dicult to eliminate. The cost CeBIT-96/19.html
of ad hoc solutions seems to outweigh that of using a ERL96] http://www.ericsson.se/erlang/
purpose-built garbage collector. bulletin/pres_free.html
One thing that is clear is that the run-time dy- HAU94] Hausman, B. Turbo Erlang: Approaching
namic memory requirements of a real-time system pro- the speed of C. In Implementations of Logic Program-
grammed in Erlang are surprisingly small. ming Systems. Evan Tick, editor. Kluwer Academic
Publishers, 1994.
8 Future directions MAR96] http://www.dcs.gla.ac.uk/
It is clear that Erlang is suitable for programming ~simonm/erltc_toc.html
a wide range of soft real-time control applications. VIR95] Virding, S. R. Garbage Collector for the
What is less clear is if Erlang is suitable for program- concurrent real-time language Erlang. International
ming applications with very demanding real-time re- Workshop on Memory Management (IWMM'95)
quirements. September 27-29, 1995 Kinross, Scotland
Research eorts in the Ericsson Computer Science WIK94] Wikstrom, C. Distributed Programming
Laboratory are concerned with a number of interesting in Erlang. First International Symposium on Parallel
problems: Symbolic Computation (PASCO'94) September 26-28,
1994 Linz.
Ecient implementation of Erlang. Here we are
investigating several techniques - these include