Download presentation
Presentation is loading. Please wait.
Published byEdmund Stewart Modified over 8 years ago
1
Erlang - a complete development environment for concurrent programming RTLab. Kim Tae-Hyon
2
Contents What is Erlang? Erlang features Sequential Programming Concurrent Programming Distribute Programming
3
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
4
Erlang features Concurrency Distribution Robustness Soft real-time Hot code upgrade Incremental code loading External Interface
5
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, -56.234, 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
6
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
7
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
8
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)
9
Sequential Programming (Cont)
10
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.
11
Sequential Programming (Cont)
12
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({})
13
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}.
14
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) ...
15
Concurrent Programming Creating a Process Pid2 = spawn(Mod, Func, Args) Simple Message Passing
16
Concurrent Programming (Cont) An Echo Process
17
Concurrent Programming (Cont) Selective Message Reception Selection of Any Message
18
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.
19
Concurrent Programming (Cont) Timeouts
20
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.
21
Concurrent Programming (Cont) Exit Signal are Sent when Processes Crash Exit Signals Propagate through Links
22
Concurrent Programming (Cont) Processes can trap exit signals receive {'EXIT', P1, Why} -> ... exit signals... {P3, Msg} -> ... normal messages... end
23
Concurrent Programming (Cont) Robust system made by layering
24
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)
25
Distribute Programming (Cont) doris $ erl –name gandalf –setcookie abc 1>kvs:start(). true george $ erl –name bilbo –setcookie abc 1>rpc:call(gandalf@doris.myerl.example.com, kvs, store, [weather, cold]).gandalf@doris.myerl.example.com, kvs true 2>rpc:call(gandalf@doris.myerl.example.com, kvs, lookup, [weather]).gandalf@doris.myerl.example.com, kvs {ok, cold}
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.