Tillämpad programmering
Erlang IV
Johan Montelius
Tillämpad programmering ID1218
1
Actor-modellen
{foo, 42}
{zot, 77}
{bar, 37}
Tillämpad programmering ID1218
2
delat minne
Tillämpad programmering ID1218
3
processer
●
Vi skapar en ny process med
–
spawn/3 eller
–
spawn/1
-spec spawn(module(), atom(), [any()]) -> pid().
-spec spawn(fun()) -> pid().
Tillämpad programmering ID1218
4
spawn/3
-module(foo).
-export([init/1]).
init(State) ->
:
:
Tillämpad programmering ID1218
:
Pid = spawn(foo, init, [42]),
:
:
5
spawn/1
-module(foo).
-export([start/1]).
start(State) ->
spawn(fun() -> init(State) end).
init(State) ->
:
:
Tillämpad programmering ID1218
:
Pid = foo:start(42)
:
:
6
self/0
• Den inbyggda funktionen self/0
returnerar processens egen identifierare.
Tillämpad programmering ID1218
7
att skicka ett meddelande
• Vi kan skicka vilken datastruktur som
helst.
hello(P) ->
P ! {hello, world}.
Tillämpad programmering ID1218
8
receive
• En process tar emot ett meddelande
med receive.
waiting() ->
receive
{hello, X} ->
X;
bye ->
ok
end.
Tillämpad programmering ID1218
9
process med tillstånd
-module(server).
-export([start/1]).
start(State) ->
spawn(fun() -> loop(State) end).
loop(State) ->
receive
{store, X} ->
loop(X);
stop ->
ok
end.
Tillämpad programmering ID1218
10
process med tillstånd
loop(State) ->
receive
{store, X} ->
loop(X);
{tellme, Pid} ->
Pid ! {state, State},
loop(State);
stop ->
ok
end.
Tillämpad programmering ID1218
11
process med tillstånd
test() ->
S = server:start(42),
S ! {store, 17},
S ! {store, 23},
S ! {tellme, ...},
receive
{state, X} ->
X
end.
Tillämpad programmering ID1218
12
processes
Tillämpad programmering ID1218
13
meddelanden
meddelandekö
(brevlåda)
Tillämpad programmering ID1218
14
ordning på meddelanden
P1
P2
ta emot hantera
meddelanden
leveras i FIFO-orning
m1
inga ack:ar
m2
m2
m1
Tillämpad programmering ID1218
mottagaren bestämmer
vilka meddelanden som
skall hanteras
15
när är ett meddelande leverat
:
S ! {store, 17},
:
:
S ! {store, 23},
:
:
Tillämpad programmering ID1218
16
vilket meddelande hanteras
{tack, 4}
{tick, 3}
Tillämpad programmering ID1218
:
receive
{tick,
X
{tack,
X
end,
X} ->
+ 3;
X} ->
+ 4
17
vilket meddelande hanteras
{tack, 4}
:
receive
{tick,
X
{tack,
X
end,
X} ->
+ 3;
X} ->
+ 4
{tick, 3}
Tillämpad programmering ID1218
18
vilken ordning
src(End, Relay) ->
End ! a,
Relay ! b.
relay(End) ->
receive
X -> End ! X
end.
a
src
b
end
b
relay
Distributed Systems ID220U
19
registrering
:
Pid = spawn(log, start, []),
register(log, Pid),
:
:
log ! {now, this, 42}
:
Distributed Systems ID220U
20
varning - kan misslyckas
:
Pid = spawn(log, start, []),
register(log, Pid),
:
:
log ! {now, this, 42}
:
Distributed Systems ID220U
21
processer med tillstånd
Processer är ett förbund
med djävulen!
Tillämpad programmering ID1218
22
referencial tranparency
:
:
foo(X, Y, Z),
:
:
X = spwan(... ),
:
foo(X, Y, Z),
:
Tillämpad programmering ID1218
23
process med tillstånd
-module(memory).
-export([new/0, read/1, write/2]).
new() -> spawn(fun() -> memory(0) end).
memory(State) ->
receive
{read, Pid} ->
Pid ! {ok, State},
memory(State);
{write, Val} ->
memory(Val)
end.
Tillämpad programmering ID1218
24
process med tillstånd
read(Mem) ->
Mem ! {read, self()},
receive
{ok, Val} ->
Val
end.
write(Val, Mem) ->
Mem ! {write, Val},
ok.
Tillämpad programmering ID1218
25
imperativ programmering :-0
:
Mem = memory:new(),
mem:write(15, Mem),
mem:write(32, Mem),
Val = mem:read(Mem),
:
Tillämpad programmering ID1218
26
delat minne :-0
Tillämpad programmering ID1218
27
rena funktionella moduler
funktioner
processer
Tillämpad programmering ID1218
28
Finite State Machine (FSM)
take
foo
bar
closed
open
release
quit
Tillämpad programmering ID1218
quit
29
Finite State Machine
open(...) ->
receive
foo ->
...;
take ->
...;
quit ->
...;
end.
Tillämpad programmering ID1218
30
Finite State Machine
closed(...) ->
receive
bar ->
...;
release ->
...;
quit ->
...;
end.
Tillämpad programmering ID1218
31
Finite State Machine
-module(lock).
-export([start/0, take/0, release/0,
foo/0, bar/0, quit/0]).
start(Arg, ..) ->
:
spawn(fun() -> init(...) end).
init(...) ->
:
open(....).
Tillämpad programmering ID1218
32
Finite State Machine
take(Lock) ->
Lock ! take.
release(Lock) ->
Lock ! release.
quit(Lock) ->
Lock ! quick.
Tillämpad programmering ID1218
33
Finite State Machine
open(...) ->
receive
foo ->
...;
{take, Pid} ->
Pid ! ok,
...;
quit ->
...;
end.
Tillämpad programmering ID1218
34
Finite State Machine
take(Lock) ->
Lock ! {take, self()},
receive
ok -> ok
end.
Tillämpad programmering ID1218
35
Finite State Machine
take(Lock) ->
Ref = make_ref();
Lock ! {take, Ref, self()},
receive
{ok, Ref} -> ok
end.
Tillämpad programmering ID1218
36
Finite State Machine
take(Lock) ->
Lock ! take.
release(Lock) ->
Lock ! release.
quit(Lock) ->
Lock ! quick.
Tillämpad programmering ID1218
37
Finite State Machine
open(State) ->
receive
foo ->
Transf = state:foo(State),
open(Transf);
take ->
...;
quit ->
...;
end.
Tillämpad programmering ID1218
38
Modululer
lock
Tillämpad programmering ID1218
state
39