It's one weeks to go home tomorrow, take a good rest. Find someone else's translation of the Erlang Programming Manual today, and read the values carefully.
The so-called distributed Erlang application is a network that runs on a series of Erlang nodes. The nature of such a system is no different from the Erlang system on a single node. Distributed this is a "big words", Erlang supports distributed programming from a native language perspective, much simpler than Java.
First, the distributed mechanism
The following bifs are for distributed programming:
Spawn (Node, Mod, Func, Args)
Start a process for a remote node
Spawn_link (Node, Mod, Func, Args)
Start a process for the remote node and create a connection to the process
Monitor_node (node, Flag)
If flag is true, this function will enable the call (the function) of the process to monitor node nodes. If the node has been discarded or does not exist, the calling process will receive a message for {Nodedown,node}. If flag is false, monitoring will be turned off
Node ()
Return to our own process name
Nodes ()
Returns a list of other known node name
Node (Item)
Returns the node name of the original item, item can be PID, reference (reference), or port
Disconnect_node (Nodename)
Disconnect from node nodename.
Nodes are the core concept of distributed Erlang. In a distributed Erlang application, the term node means a running system that can join a distributed transactions. With a special process called Net Kernal, a standalone Erlang system can be part of a distributed Erlang system. When the net kernal process starts, we call the system alive.
Communicating with a process on a remote node is only a little different from a process within the same node:
{Name, Node}! Mess.
Obviously, you need to add a parameter to the receiver to specify the node where the acceptance process resides. The name of the node is typically an atom type separated by @, such as [email protected], which indicates that the computer is named Pong node on Dennis. by executing:
Erl-sname Pong
A node pong is created on the computer that is being executed. To run the following example, you may need two computers, if only one, as long as you open two Erlang systems and run with different node names.
Two or one some examples.
This example is entirely from the above mentioned translation of the connection, the section on distributed programming. I have added and explained.
The first is the code:
-module (tut17).
-export ([Start_ping/1, start_pong/0, PING/2, pong/0]).
Ping (0, Pong_node)
{Pong, pong_node}! Finished,
Io:format ("Ping Finished~n", []);
Ping (N, Pong_node),
{Pong, pong_node}! {ping, self ()},
Receive
Pong
Io:format ("Ping received Pong~n", [])
End
Ping (N-1, Pong_node).
Pong ()
Receive
Finished
Io:format ("Pong finished~n", []);
{Ping, ping_pid}
Io:format ("Pong received Ping~n", []),
Ping_pid! Pong
Pong ()
End.
Start_pong ()
Register (Pong, spawn (tut17, Pong, [])).
Start_ping (Pong_node)
Spawn (tut17, Ping, [3, Pong_node]).
The code is to create two processes that communicate with each other, send messages to each other and display on the screen via IO, which is an example of a single system, and now we have two processes running on a different two nodes. Note the Start_ping method, the process that is created calls the Ping method, the Ping method has two parameters, one is the number of times the message is sent, and the other is the name of the remote node, which is the node where the process we are going to create pong. Start_pong creates a process that invokes the function pong and registers as the name Pong (so you can send the message directly to Pong in the Ping method).
I was testing on a Windows machine, first open two cmd windows, and CD to the bin directory in Erlang's installation directory, such as C:\Program Files\erl5.5.3\bin, save the above program as Tut17.erl and copy it to the same directory. We will create two nodes, one called [email protected], one called [email protected], where Dennis is my machine name. See:
Use the same command
Erl-sname Ping
Create a ping node. Then execute Start_pong () under the Pong node:
OK, this starts the pong process on the node pong, and then calls Start_ping on the ping node, and the incoming parameter is [email protected]
tut17:start_ping ([email protected]).
Execution results such as:
Also on the Pong node you can see:
As a result, as we expected, two processes on different nodes communicate with each other in such a simple way. We add a method to the module tut17 to start the remote process, which is called the Spawn (Node,module,func,args) method:
Start (Ping_node)
Register (Pong, spawn (tut17, Pong, [])),
Spawn (Ping_node, tut17, Ping, [3, Node ()]).
The pong process starts the process ping on the Ping_node node. The specific results are no longer given.
Introduction to Erlang (iii)--distributed programming