Erlang - a complete development environment for concurrent programming RTLab. Kim Tae-Hyon
Contents What is Erlang? Erlang features Sequential Programming Concurrent Programming Distribute Programming
What is Erlang? Erlang is a general-purpose programming language and runtime environment Erlang has built-in support for concurrency, distribution and fault tolerance Erlang is used in several large telecommunication systems from EricssonEricsson
Erlang features Concurrency Distribution Robustness Soft real-time Hot code upgrade Incremental code loading External Interface
Data Types Term A piece of data of any data type is called a term Numbers Integer (10, -234, 16#AB10F, $A) Floats (17.368, , 12.34E-10) Atoms abcdef, start_with_a_lower_case, ‘Blanks can be quoted’ Tuples {123, bcd}, {123, def, abc}, {abc, {def, 123}, jkl}, {} Lists [123, xyz], [123, def, abc], [{person, ‘Kim’, ‘Tae Hyon’}, {person, …}] Variables Abc, A_long_variable_name, AnObjectName
Data Types (Cont) String Not a data type in erlang Bit string and Binaries Red=2, Green=61, Blue=20. Mem = > > > = Mem. R1 = 2, G1 = 61, B1 = 20 Fun Functional object Fun1 = fun(x) -> X+1 end. Fun1(2). 3
Data Types (Cont) Pid A Process Identifier Record -record(person, {name, age}). new(Name, Age) -> #person{name=Name, age=Age}. 1> person:new(ernie, 44). {person,ernie,44} Boolean True, false
Sequential Programming Pattern Matching A = 10 (succeeds) {B, C, D} = {10, foo, bar} (succeeds) {A, A, B} = {abc, abc, foo} (succeeds) {A, A, B} = {abc, def, foo} (fails) [A, B, C] = [1, 2, 3] (succeeds) [A, B, C, D] = [1, 2, 3] (fails) [A, B|C] = [1,2,3,4,5,6,7] (succeeds) [H|T] = [1,2,3,4] (succeeds) [H|T] = [abc] (succeeds) [H|T] = [] (fails) {A, _, [B|_], {B}} = {abc, 23, [22, x], {22}} (succeeds)
Sequential Programming (Cont)
Function Calls module:func(Arg1, Arg2, …, Argn) func(Arg1, Arg2, …, Argn) Module System -module(seminar_demo). -export([double/1]). double(X) ->times(X,2). times(X, N) ->X * N.
Sequential Programming (Cont)
Built In Functions(BIF) date() time() length([1,2,3,4,5]) atom_to_list(an_atom) list_to_tuple([1,2,3,4]) integer_to_list(2234) tuple_to_list({})
Sequential Programming (Cont) Function Syntax func(Pattern1, Pattern2,...) ->... ; func(Pattern1, Pattern2,...) ->... ; func(Pattern1, Pattern2,...) ->.... Function Example area({square, Side}) -> Side * Side; area({circle, Radius}) -> 3 * Radius * Radius; area({triangle, A, B, C}) -> S = (A + B + C)/2, math:sqrt(S*(S-A)*(S-B)*(S-C)); area(Other) -> {invalid_object, Other}.
Sequential Programming (Cont) Guarded Function Clauses factorial(N) when N > 0 -> N * factorial(N - 1); factorial(0) -> 1. Examples of Guards is_atom(X) is_binary(X) Is_constant(X) Is_function(X) X == Y (equal) X =:= Y (exactly equal -> 1==1.0 succeds, 1=:=1.0 fails) ...
Concurrent Programming Creating a Process Pid2 = spawn(Mod, Func, Args) Simple Message Passing
Concurrent Programming (Cont) An Echo Process
Concurrent Programming (Cont) Selective Message Reception Selection of Any Message
Concurrent Programming (Cont) Pid Can Be Sent in Message Registered Process start() -> Pid = spawn(num_anal, server, []) register(analyser, Pid). analyse(Seq) -> analyser ! {self(),{analyse,Seq}}, receive {analysis_result,R} -> R end.
Concurrent Programming (Cont) Timeouts
Concurrent Programming (Cont) Primitives For Exit Signal Handling link(Pid) - Set a bi-directional link between the current process and the process Pid process_flag(trap_exit, true) - Set the current process to convert exit signals to exit messages, these messages can then be received in a normal receive statement. exit(Reason) - Terminates the process and generates an exit signal where the process termination information is Reason.
Concurrent Programming (Cont) Exit Signal are Sent when Processes Crash Exit Signals Propagate through Links
Concurrent Programming (Cont) Processes can trap exit signals receive {'EXIT', P1, Why} -> ... exit signals... {P3, Msg} -> ... normal messages... end
Concurrent Programming (Cont) Robust system made by layering
Distribute Programming Nodes A node is an executing erlang runtime system which has been given a name, using the command line flag -name or –sname Node connections spawn(Node, Module, Function, Arguments) net_adm:ping(Node)
Distribute Programming (Cont) doris $ erl –name gandalf –setcookie abc 1>kvs:start(). true george $ erl –name bilbo –setcookie abc kvs, store, [weather, kvs true kvs, lookup, kvs {ok, cold}