presentation
TRANSCRIPT
Erlang in a (real) nutshell
Luıs Ferreira
Universidade do MinhoSemana da Lei II
July 13, 2010
What is Erlang?
Distributed
Concurrent
Fault-tolerant
Functional based
Luıs Ferreira Erlang in a (real) nutshell
What is Erlang?
Distributed
Concurrent
Fault-tolerant
Functional based
Luıs Ferreira Erlang in a (real) nutshell
What is Erlang?
Distributed
Concurrent
Fault-tolerant
Functional based
Luıs Ferreira Erlang in a (real) nutshell
What is Erlang?
Distributed
Concurrent
Fault-tolerant
Functional based
Luıs Ferreira Erlang in a (real) nutshell
Why use Erlang?
NO
Number-crunching applications
Graphics intesive systems
YES
High-level, concurrent, robust, soft real-time system that willscale
Make full use of multicore processors
Integrate with components written in other languages
Luıs Ferreira Erlang in a (real) nutshell
Why use Erlang?
NO
Number-crunching applications
Graphics intesive systems
YES
High-level, concurrent, robust, soft real-time system that willscale
Make full use of multicore processors
Integrate with components written in other languages
Luıs Ferreira Erlang in a (real) nutshell
Some of the essential characteristics
Low memory overhead per process/task
Thousands of processes - Own memory space, heap and stack
No ”global” errors. Stop errors propagating
Predictable performance
Functional/single-assignment
Asynchronous message passing (send and pray) - Any Erlangvalue
OS independent
Messages retrieved selectively from the mailbox
Message passing by copy of data
Luıs Ferreira Erlang in a (real) nutshell
Some of the essential characteristics
Low memory overhead per process/task
Thousands of processes - Own memory space, heap and stack
No ”global” errors. Stop errors propagating
Predictable performance
Functional/single-assignment
Asynchronous message passing (send and pray) - Any Erlangvalue
OS independent
Messages retrieved selectively from the mailbox
Message passing by copy of data
Luıs Ferreira Erlang in a (real) nutshell
Some of the essential characteristics
Low memory overhead per process/task
Thousands of processes - Own memory space, heap and stack
No ”global” errors. Stop errors propagating
Predictable performance
Functional/single-assignment
Asynchronous message passing (send and pray) - Any Erlangvalue
OS independent
Messages retrieved selectively from the mailbox
Message passing by copy of data
Luıs Ferreira Erlang in a (real) nutshell
Some of the essential characteristics
Low memory overhead per process/task
Thousands of processes - Own memory space, heap and stack
No ”global” errors. Stop errors propagating
Predictable performance
Functional/single-assignment
Asynchronous message passing (send and pray) - Any Erlangvalue
OS independent
Messages retrieved selectively from the mailbox
Message passing by copy of data
Luıs Ferreira Erlang in a (real) nutshell
Some of the essential characteristics
Low memory overhead per process/task
Thousands of processes - Own memory space, heap and stack
No ”global” errors. Stop errors propagating
Predictable performance
Functional/single-assignment
Asynchronous message passing (send and pray) - Any Erlangvalue
OS independent
Messages retrieved selectively from the mailbox
Message passing by copy of data
Luıs Ferreira Erlang in a (real) nutshell
Some of the essential characteristics
Low memory overhead per process/task
Thousands of processes - Own memory space, heap and stack
No ”global” errors. Stop errors propagating
Predictable performance
Functional/single-assignment
Asynchronous message passing (send and pray) - Any Erlangvalue
OS independent
Messages retrieved selectively from the mailbox
Message passing by copy of data
Luıs Ferreira Erlang in a (real) nutshell
Some of the essential characteristics
Low memory overhead per process/task
Thousands of processes - Own memory space, heap and stack
No ”global” errors. Stop errors propagating
Predictable performance
Functional/single-assignment
Asynchronous message passing (send and pray) - Any Erlangvalue
OS independent
Messages retrieved selectively from the mailbox
Message passing by copy of data
Luıs Ferreira Erlang in a (real) nutshell
Some of the essential characteristics
Low memory overhead per process/task
Thousands of processes - Own memory space, heap and stack
No ”global” errors. Stop errors propagating
Predictable performance
Functional/single-assignment
Asynchronous message passing (send and pray) - Any Erlangvalue
OS independent
Messages retrieved selectively from the mailbox
Message passing by copy of data
Luıs Ferreira Erlang in a (real) nutshell
Some of the essential characteristics
Low memory overhead per process/task
Thousands of processes - Own memory space, heap and stack
No ”global” errors. Stop errors propagating
Predictable performance
Functional/single-assignment
Asynchronous message passing (send and pray) - Any Erlangvalue
OS independent
Messages retrieved selectively from the mailbox
Message passing by copy of data
Luıs Ferreira Erlang in a (real) nutshell
Sequential Erlang
Sort
sort([Pivot|T]) ->sort([X||X <- T, X =< Pivot]) ++[Pivot] ++ sort([X||X <- T, X >= Pivot]);
sort([]) -> [].
Luıs Ferreira Erlang in a (real) nutshell
Concurrent Erlang
Area Server
-module(server).-export([start/0]).
start() ->register(server,spawn(fun() -> loop(0) end)).
loop(Tot) ->receive
{Pid, {square, X}} ->Pid ! X*X,loop(Tot + X*X);
{Pid, {rectangle,[X,Y]}} ->Pid ! X*Y,loop(Tot + X*Y);
{Pid, areas} ->Pid ! Tot,loop(Tot)
end.
Luıs Ferreira Erlang in a (real) nutshell
Concurrent Erlang
Area Client
-module(client).-export([square/1]).
square(Val) ->server ! {self(),{square,Val}},
receiveArea ->
io:format("Area: ~p~n",[Val])end.
Luıs Ferreira Erlang in a (real) nutshell
Other features
Fault tolerance (catch/throw)Trapping exitsLinked processesHot code replacementGeneric behaviours. . .
Exception handle
try return(X) whenis integer(X) ->
try return error(X) ofVal -> {normal,Val}
catchexit:Reason ->
{exit,Reason}throw:Throw ->
{throw,Throw}error:Error ->
{error,Error}end.
Luıs Ferreira Erlang in a (real) nutshell
Other features
Fault tolerance (catch/throw)Trapping exitsLinked processesHot code replacementGeneric behaviours. . .
Trap exits
process flag(trap exits,true),P = spawn link(Node, Mod,Func, Args),receive
{’EXIT’, P, Why}->Actions;
...end
Luıs Ferreira Erlang in a (real) nutshell
Other features
Fault tolerance (catch/throw)Trapping exitsLinked processesHot code replacementGeneric behaviours. . .
worker
supervisor
Luıs Ferreira Erlang in a (real) nutshell
Other features
Fault tolerance (catch/throw)Trapping exitsLinked processesHot code replacementGeneric behaviours. . .
Luıs Ferreira Erlang in a (real) nutshell
Other features
Fault tolerance (catch/throw)Trapping exitsLinked processesHot code replacementGeneric behaviours. . .
Luıs Ferreira Erlang in a (real) nutshell