Download presentation
Presentation is loading. Please wait.
1
Computer Science 312 Concurrent Programming II
Distributing Processes to Nodes on a Network 1
2
Concurrent and Distributed Programs
A concurrent program can run on multiple processes on a single computer (multicore) Or it can run on multiple processes distributed across a network of computers The latter is also called a distributed program
3
Two Ways to Set up DC Start up Erlang nodes; best when working with a cluster of computers on a subnet, where you control all the machines from a single machine, and everyone is “trusted” Use sockets, ports, and TCP to control access over the Internet, where no one is “trusted”
4
Erlang Processes and Nodes
An Erlang node is an abstraction of a host computer’s address and a process Nodes can be created to host processes on the same computer, or they can be created on different computers Each node runs its own copy of an Erlang virtual machine The rpc module (not to be confused with our own rpc function) includes resources for manipulating nodes
5
Example: A Name Server -module(kvs).
%% ==================================================================== %% API functions -export([start/0, store/2, lookup/1]). start() -> register(kvs, spawn(fun() -> loop() end)). store(Key, Value) -> rpc({store, Key, Value}). lookup(Key) -> rpc({lookup, Key}).
6
Example: A Name Server %% ==================================================================== %% Internal functions rpc(Q) -> kvs ! {self(), Q}, receive {kvs, Reply} -> Reply end. loop() -> {From, {store, Key, Value}} -> put(Key, {ok, Value}), From ! {kvs, true}, loop(); {From, {lookup, Key}} -> From ! {kvs, get(Key)}, loop()
7
Test Locally First 1> kvs:start(). true
2> kvs:store({hairColor, ken}, gray). 3> kvs:store(weather, cold). 4> kvs:lookup({hairColor, ken}). {ok, gray} 5> kvs:lookup(weather). {ok, cold} 6> kvs:lookup({age, ken}). undefined
8
The Fire Up a Server Node
In one terminal window, launch Erlang and start a server process: $ erl –sname gandalf 1> kvs:start(). true
9
Fire Up a Client Node In one terminal window, launch Erlang and start a server process: $ erl –sname gandalf 1> kvs:start(). true In a second terminal window on the same computer, launch Erlang and make a request of the server at its node: $ erl –sname bilbo 1> kvs, store, [weather, cold]). true 2> kvs, lookup, [weather]). {ok, cold}
10
Run Nodes on Different Computers
Must make port 4369 available for the Erlang Port Mapper Daemon (epmd) Must use the -name rather than the -sname option Must use -setcookie to establish a cookie for the server and all its clients Must choose a range of ports using the -kernel option
11
Run Nodes on Different Computers
Example: -setcookie abc Example (for any distributed app in Parmly 405 and 413): -kernel inet_dist_listen_min 9100 inet_dist_listen_max 9105
12
Run Nodes on Different Computers
In a terminal window on the server’s computer: $ erl –name gandalf –setcookie abc -kernel inet_dist_listen_min 9100 inet_dist_listen_max 9105 1> kvs:start(). true In a terminal window on the client’s computer, use the same options (note the quote marks around the server node’s name) $ erl –name bilbo –setcookie abc -kernel inet_dist_listen_min 9100 inet_dist_listen_max 9105 1> kvs, / store, [weather, cold]). true 2> kvs, lookup, [weather]). {ok, cold}
13
For next time Handling errors
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.