Professional Documents
Culture Documents
Linking Processes: Erlang
Linking Processes: Erlang
Erlang
Linking processes Error handling Fault-tolerant systems Distributed Programming
Linking Processes
If a process in some way depends on another, then it may well want to keep an eye on the health of that second process. Linking:
2013 DEI-ISEP
Jorge Coelho
Linking Processes
What happens when a process receives an exit signal? If the receiver hasnt taken any special steps, the exit signal will cause it, too, to exit. However, a process can ask to trap these exit signals. When a process is in this state, it is called a system process. If a process linked to a system process exits for some reason, the system process is not automatically terminated. Instead, the system process receives an exit signal, which it can trap and process.
2013 DEI-ISEP Jorge Coelho 3
on_exit Handler
on_exit(Pid, Fun) -> spawn(fun() -> process_flag(trap_exit, true), link(Pid), receive {'EXIT', Pid, Why} -> Fun(Why) end end).
2013 DEI-ISEP
Jorge Coelho
Example
1> F = fun() -> receive X -> list_to_atom(X) end end. #Fun<erl_eval.20.69967518> 2> Pid = spawn(F). <0.61.0> 3> our_module:on_exit(Pid, fun(Why) -> io:format(" ~p died with:~p~n",[Pid, Why]) end). <0.63.0> 4> Pid ! hello. hello <0.61.0> died with:{badarg,[{erlang,list_to_atom,[hello]}]}
2013 DEI-ISEP Jorge Coelho 5
2013 DEI-ISEP
Jorge Coelho
2013 DEI-ISEP
Jorge Coelho
Trapping Exits
Option 1: I Dont Care If a Process I Create Crashes
2013 DEI-ISEP
Jorge Coelho
11
Trapping Exits
Option 2: I Want to Die If a Process I Create Crashes
2013 DEI-ISEP
Jorge Coelho
12
Trapping Exits
Option 3: I Want to Handle Errors If a Process I Create Crashes
... process_flag(trap_exit, true), Pid = spawn_link(fun() -> ... end), ... loop(...). loop(State) -> receive {'EXIT', SomePid, Reason} -> %% do something with the error loop(State1); ... end
2013 DEI-ISEP Jorge Coelho 13
2013 DEI-ISEP
Jorge Coelho
14
Keep-Alive Process
Make a registered process that is always alive. If it dies for any reason,it will be immediately restarted.
keep_alive(Name, Fun) -> register(Name, Pid = spawn(Fun)), on_exit(Pid, fun() -> keep_alive(Name, Fun) end).
2013 DEI-ISEP
Jorge Coelho
15
Distributed Programming
There are a number of reasons why we might want to write distributed applications. Here are some: Performance - We can make our programs go faster by arranging that different parts of the program are run in parallel on different machines. Reliability - We can make fault-tolerant systems by structuring the system to run on several machines. If one machine fails, we can continue on another machine. Scalability - As we scale up an application, sooner or later we will exhaust the capabilities of even the most powerful machine. At this stage we have to add more machines to add capacity. Adding a new machine should be a simple operation that does not require large changes to the application architecture.
2013 DEI-ISEP Jorge Coelho 16
Name Server
start() -> register(kvs, spawn(fun() -> loops() end)). store(Key, Value) -> rpc({store, Key, Value}). lookup(Key) -> rpc({lookup, Key}). rpc(Q) -> kvs ! {self(), Q}, receive {kvs, Reply} -> Reply end. loops() -> receive {From, {store, Key, Value}} -> put(Key, {ok, Value}), From ! {kvs, true}, loops(); {From, {lookup, Key}} -> From ! {kvs, get(Key)}, loops() end.
2013 DEI-ISEP Jorge Coelho 17
Name Server
1> kvs:store({location,isep},"Porto"). true 2> kvs:store(weather,raining). true 3> kvs:lookup(raining). undefined 4> kvs:lookup(weather). {ok,raining} 5> kvs:lookup({location,isep}). {ok,"Porto"}
2013 DEI-ISEP Jorge Coelho 18
2013 DEI-ISEP
Jorge Coelho
19
We can swap back to mickey and check the value of the {location,isep}:
(mickey@lucas)6> kvs:lookup({location,isep}). {ok,Porto"}
2013 DEI-ISEP Jorge Coelho 20
10
Different Hosts
Start Erlang with the -name parameter. When we have two nodes on the same machine, we use short names (as indicated by the -sname flag), but if they are on different networks, we use -name. We can also use -sname on two different machines when they are on the same subnet. Using -sname is also the only method that will work if no DNS service is available. Ensure that both nodes have the same cookie. Both nodes must be started with the same command-line argument setcookie. Make sure the fully qualified hostnames of the nodes concerned are resolvable by DNS. Make sure that both systems have the same version of the code that we want to run and the same version of Erlang.
2013 DEI-ISEP Jorge Coelho 21
Different Hosts
To connect two different hosts in the Internet:
Make sure that port 4369 is open for both TCP and UDP traffic. This port is used by a program called epmd (short for the Erlang Port Mapper Daemon). Choose a port or range of ports to be used for distributed Erlang, and make sure these ports are open. If these ports are Min and Max, then start Erlang with the following command:
werl -name ... -setcookie ... -kernel inet_dist_listen_min Min \ inet_dist_listen_max Max
2013 DEI-ISEP
Jorge Coelho
22
11
12
2013 DEI-ISEP
Jorge Coelho
26
13
2013 DEI-ISEP
Jorge Coelho
28
14
15
2013 DEI-ISEP
Jorge Coelho
31
16