Erlang Programming
description
Transcript of Erlang Programming
Slide titleIn CAPITALS
50 pt
Slide subtitle 32 pt
Erlang Programming
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-172 (209)
Session 4
Concurrent Programming– Processes.– Messages.– Process pattern.– Process aliases.– Timeouts.– Example: Client-Server model
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-173 (209)
Processes
Concurrent activities are modeled using light-weight processes.
Many processes may be running simultaneously.
The identity of the newly created process is returned from the BIF spawn/3:
Code run by PidA:PidB = spawn(Mod, Func, Args)
PidA
PidBPidA
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-174 (209)
Erlang Example
Creating a new process using spawn
-module(ex3).-export([activity/3]).
activity(Name,Pos,Size) -> …………
Pid = spawn(ex3,activity,[Joe,75,1024]) activity(Joe,75,1024)activity(Joe,75,1024)
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-175 (209)
Processes
spawn(Module, Function, Args).
Creates a new process, i.e. another thread of execution, starting in the function call given by the arguments.
Example: spawn(m, f, [1,2,3])
creates a process starting in m:f(1,2,3).
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-176 (209)
Processes
The function used in spawn/3 must be exported.
A process will terminate when there is no more code to execute.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-177 (209)
Processes
Messages are sent using “!” (Sometimes called bang).
Pid ! Msg
Msg is any legal Erlang term Pid is of type process identifier (most probably returned
by spawn)
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-178 (209)
Processes
Processes communicates by sending and receiving messages
PidA PidBhello
PidB ! hello receive
hello ->
...
end
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-179 (209)
Processes
The BIF self/0 returns the process identifier of the process calling it
Try it in the shell.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1710 (209)
Example: Area server
-module(area_server0).-export([loop/0]).loop() ->
receive{rectangle, Width, Ht} ->
io:format("Area of rectangle is ~p~n",
[Width * Ht]),loop();
{circle, R} ->io:format("Area of circle is
~p~n" , [3.14159 * R * R]),loop();
Other ->io:format("I don't know what the
area of a ~p is ~n" ,[Other]),loop()
end.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1711 (209)
Example: Area server
1> Pid = spawn(fun area_server0:loop/0).<0.36.0>
2> Pid ! {rectangle, 6, 10}.Area of rectangle is 60
{rectangle,6,10}3> Pid ! {circle, 23}.
Area of circle is 1661.90{circle,23}
4> Pid ! {triangle,2,4,5}.I don't know what the area of a {triangle,2,4,5} is{triangle,2,4,5}
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1712 (209)
Transparent Distribution
Erlang Run-Time SystemErlang Run-Time System Erlang Run-Time SystemErlang Run-Time System
B ! Msg
network
C ! Msg
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1713 (209)
Process Patternstart() -> spawn(mymod, init, [...]).
init() ->
<initializations>,
loop(...).
loop(...)
receive
stop ->
true; %%The process stops here
Pattern1 ->
<actions>,
loop(...);
...
PatternN ->
<actions>,
loop(...)
end.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1714 (209)
Process example (echo process)A process that receives a message and returns it back to the sender.
-module (echo).-export ([start/0, init/0]).
start() -> spawn(echo, init, []).
init() ->loop().
loop() ->receive
stop ->
true;{Pid, Msg} ->
Pid ! {self(), Msg},loop()
end.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1715 (209)
Process example (Testing echo)-module(test_echo).-export([start/1, init/1]).
start(N)-> spawn(test_echo, init, [N]).
init(N)-> Epid = echo:start(), loop(N, Epid).
loop(0, Epid) -> Epid ! stop, io:format("Echo passed this test ~n", []);loop(N, Epid) -> Epid ! {self(), N}, io:format("Sending message, N= ~p~n", [N]), receive
stop -> true;{Epid, N} -> loop(N-1, Epid)
end.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1716 (209)
Process example (echo process)-module (echo).-export ([start/0, init/0]).
start() -> spawn(echo, init, []).
init() ->loop().
loop()-> receive
stop -> io:format("I got message ~p from process ~p~n", [stop, unknown]),
true;{Pid, Msg} -> io:format("I got message ~p from process ~p~n", [Msg, Pid]), Pid ! {self(), Msg}, loop()end
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1717 (209)
Process aliases
To use a name instead of process ID, use the BIF register/2
register(Alias, Pid)
Any process can send a message using the alias.register(iti, Pid),……iti ! hello,
Sending a message to a non-existant alias, fails.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1718 (209)
Timeouts
To be able to program a process that does not wait forever for a message to arrive.
receive
hello ->
io:format(“hi”, [])
after
1000 -> %1000 millisec
true
end.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1719 (209)
Timeouts
test_echo with timeouts (in case the echo process dies).
loop(N, Epid) -> Epid ! {self(), N}, receive
stop ->true;
{Epid, N} -> loop(N-1, Epid) after
1000 ->io:format(“Echo timed out”)
end.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1720 (209)
Example: Client-Server model
Message protocol:
{request, ClientPid, alloc} {reply, Resource}
{request, ClientPid, {free, Resource}} {reply, ok}
PidA
PidB
PidC
server
server.erl
hw.erl
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1721 (209)
Client-Server (cont.)
Server Code
-module(server).-export([start/0]).-export([alloc/0, free/1]).-export([init/0]).
start() -> spawn(server, init, []).
init() ->register(server, self()),Resources = hw:get_resources(),
loop(Resources).
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1722 (209)
Client-Server (cont.)
loop(ResDB) ->
receive
{request, From, alloc} ->
{Resources, NewResDB} = hw:alloc(ResDB},
From ! {reply, Resource},
loop(NewResDB};
{requet, From, {free, Resource}}->
From ! {reply, ok},
loop(hw:free(Resource, ResDB));
_Other
loop(ResDB)
end.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1723 (209)
Client-Server (cont.)
Client functions:
alloc() -> call(alloc).
free(Resource)call({free, Resource}).
call(Request) ->server ! {request, self(), Request},receive
{reply, Reply} ->Reply
end.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1724 (209)
Lesson 4
Links. Exit signals. Trapping exit signals. Robust systems. Monitors.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1725 (209)
Links
Processes can be linked together using the BIF
link(Pid).or spawn_link(Module, Function, Args).
Links are removed using:unlink(Pid).
PidA PidB
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1726 (209)
Exit Signals
When a process terminates, exit signals are sent over all links connected to the terminating process
PidA PidBEXIT
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1727 (209)
Exit Signals
A process can terminate:
– normally: (when it has no more code to execute).
– abnormally: due to run-time error.
– abnormally: due to execution of BIF causing termination exit(Reason).
Note: a signal is not a message.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1728 (209)
Exit signals
When a process terminates normally (no more code to execute), the emitted exit-signals are ignored by the linked processes.
When the process terminates abnormally, linked process will notice the emitted exit signals…
… and terminate themselves
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1729 (209)
Exit Signals
When a process terminates, an exit signal is sent to all linked processes
… and the termination is propagated
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1730 (209)
Exit Signals
The BIF exit/2 emits an exit-signal to a process even if there is no link between them.
exit(DestPid, Reason).
PidA doesn’t terminate, it just terminates PidB
PidA PidBEXIT
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1731 (209)
Trapping exit signals
To protect itself from exit signals, a process uses the BIF:
process_flag(trap_exit, true).
Then, incoming exit signals will be transformed to a message and put in the message que in the format:
{‘EXIT’, Pid, Reasons}
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1732 (209)
Trapping exit signals
Trapping exit causes any exit signal to be transformed into a message even signals occuring due to normal termination of linked processes.
When a process PidA is linked to PidB, PidB is trapping exit and PidA terminates normally (no more code to execute), PidB receives the following message:
{‘EXIT’, Pid, Reasons}
Remember what happens in the above case when PidB is not trapping exit?
– PidB just receives the exit signal but ignores it.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1733 (209)
Trapping exit signals
Processes that traps exit can also be killed by sending them an exit signal with reason kill
The process terminates with the reason killed
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1734 (209)
Summary of trapping exits
Process Flag Reason: normal Abnormal reasons
Reason: kill Reason: Other
Not trapping exit
Trapping exit
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1735 (209)
Summary of trapping exits
Process Flag Reason: normal Abnormal reasons
Reason: kill Reason: Other
Not trapping exit
Ignores signal
Trapping exit
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1736 (209)
Summary of trapping exits
Process Flag Reason: normal Abnormal reasons
Reason: kill Reason: Other
Not trapping exit
Ignores signal Terminates
Reason = kill
Trapping exit
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1737 (209)
Summary of trapping exits
Process Flag Reason: normal Abnormal reasons
Reason: kill Reason: Other
Not trapping exit
Ignores signal Terminates
Reason = kill
Terminates
Reason = Other
Trapping exit
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1738 (209)
Summary of trapping exits
Process Flag Reason: normal Abnormal reasons
Reason: kill Reason: Other
Not trapping exit
Ignores signal Terminates
Reason = kill
Terminates
Reason = Other
Trapping exit Receives:
{‘EXIT’, Pid, normal}
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1739 (209)
Summary of trapping exits
Process Flag Reason: normal Abnormal reasons
Reason: kill Reason: Other
Not trapping exit
Ignores signal Terminates
Reason = kill
Terminates
Reason = Other
Trapping exit Receives:
{‘EXIT’, Pid, normal}
Terminates
Reason = killed
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1740 (209)
Summary of trapping exits
Process Flag Reason: normal Abnormal reasons
Reason: kill Reason: Other
Not trapping exit
Ignores signal Terminates
Reason = kill
Terminates
Reason = Other
Trapping exit Receives:
{‘EXIT’, Pid, normal}
Terminates
Reason = killed
Receives:
{‘EXIT’, Pid, Other}
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1741 (209)
Client-Server (robust)
Server Code
-module(server).-export([start/0]).-export([alloc/0, free/1]).-export([init/0]).
start() -> spawn(server, init, []).
init() ->process_flag(trap_exit, true),register(server, self()),Resources = hw:get_resources(),
loop(Resources).
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1742 (209)
Client-Server (cont.)
loop(ResDB) -> receive {request, From, alloc} ->
{Resources, NewResDB} = hw:alloc(ResDB}, link(From), From ! {reply, Resource}, loop(NewResDB};
{requet, From, {free, Resource}}-> unlink(From),
From ! {reply, ok}, loop(hw:free(Resource, ResDB));
{‘EXIT’, From, _Reason}-><do something>loop(NewResDB);
_Other ->loop(ResDB)
end.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1743 (209)
Monitors
The BIF erlang:monitor/2 offers a way to monitor another process.
The monitor is not bi-directional like links.MRef = erlang:monitor(process, Process).Process = pid() | atom() documentation style
If the process terminates the calling process will receive a
{‘DOWN’, MRef, process, Object, Reason}
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1744 (209)
Exercises
Write a process that upon receiving a certain message terminates normally and when receiving another message terminates ubnormaly.
loop() -> receive
stop -> io:format("Process stopped~n");error -> io:format("Process will terminate ubnormally~n"), abc = abcd;Msg -> io:format("Message ~p received ~n", [Msg]), loop()
end.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1745 (209)
Exercises
Write a function that starts another process– pmon:start(SupervisedPID).
The process shall use a link to monitor the supervised process.
The monitoring process shall print a message if the supervised process terminates abnormally.
The monitoring process should terminate whenever the supervised process does, regardless of the reason.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1746 (209)
Solution
-module(pmon).-export([start/1, init/1]).
start(Pid)-> spawn(pmon, init, [Pid]).
init(Pid)-> process_flag(trap_exit, true), link(Pid), loop(Pid).
loop(Pid)-> receive
{'EXIT', Pid, normal}-> ok;{'EXIT', Pid, Reason} -> io:format("Process ~p terminated abnormally, reason: ~p~n", [Pid, Reason]);_Other -> loop(Pid)
end.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1747 (209)
Master and Slaves
Write a module ms that has the following interfaces:– start(N): starts a master process and tells it to start N slave
processes. Register the master as master.
– to_slave(Message, N): Sends a message to the master and tell it to relay the message to slave N.
– The slave should exit if it receives the message die.
– The master should restart the dead slave and restarts it.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1748 (209)
Master and slaves
ms:start(4). true ms:to_slave(hello, 2). {hello, 2} Slave 2 got message hello.
ms:to_slave(die, 3). {die, 3} master restarting dead slave 3.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1749 (209)
Solution
-module(ms).-export([start/1, to_slave/2]).-export([master_init/1, slave_init/1]).
start(N)-> spawn(ms, master_init, [N]).
to_slave(Msg, N)-> master ! {to_slave, self(), Msg, N}.
master_init(N)-> register(master, self()), process_flag(trap_exit, true), Slaves = start_slaves(N, []), master_loop(Slaves).
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1750 (209)
Solution
start_slaves(0, Slaves)->
Slaves;
start_slaves(N, Slaves) ->
start_slaves(N-1, [spawn_link(ms, slave_init, [N])|Slaves]).
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1751 (209)
Solution
master_loop(Slaves)->
receive
{to_slave, _Pid, Msg, N}->
lists:nth(N, Slaves) ! Msg,
master_loop(Slaves);
{'EXIT', Pid, _Reason} ->
master_loop(replace(Pid, Slaves, 1));
_Other ->
master_loop(Slaves)
end.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1752 (209)
Solution
slave_init(N)->
slave_loop(N).
slave_loop(N)->
receive
die ->
ok;
Msg ->
io:format("Slave ~p got message ~p~n", [N, Msg]),
slave_loop(N)
end.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1753 (209)
Solution
replace(Pid, [Pid|Rest], N)->
io:format("master restarting dead slave ~p~n", [N]),
[spawn_link(ms, slave_init, [N]) | Rest];
replace(Pid, [OtherPid|Rest], N) ->
[OtherPid | replace(Pid, Rest, N+1)].
Top right corner for field-mark, customer or partner logotypes. See Best practice for example.
Slide title 40 pt
Slide subtitle 24 pt
Text 24 pt
Bullets level 2-520 pt
Ericsson Internal Erlang Programming for ITI 2009-05-1754 (209)
Summary
Links. Exit signals. Trapping exit signals. Robust systems. Monitors.