http://www.erlang.org/doc/getting_started/intro.html
spawn创建进程,返回PID
!运算符发送消息(无论是否在同一节点)
Process:
<span style="font-size:18px;">-module(tut).
-export([start/0,say/2]).
say(What,0) ->
done;
say(What,Times) ->
io:format("~p~n",[What]),
say(What,Times - 1).
start() ->
spawn(tut,say,[hello,3]),
spawn(tut,say,[bye,3]).
9> tut:start().
hello
bye
hello
bye
<0.62.0>
hello
bye
10></span>
Message passing:
每个进程有自己的接收消息队列,新收到的消息放到队尾,当进程执行receive,从队列中读取第一个消息匹配receive路径;
如果无匹配,留在消息队列中;
如果匹配成功,执行该路径并删除该消息;
然后读取第二个消息匹配receive路径。。。
如果达到队尾,进程停止执行并阻塞直到收到新消息;
self()返回调用该函数的进程的PID;
<span style="font-size:18px;">-module(tut).
-export([start/0,ping/2,pong/0]).
start() ->
Pong_PID = spawn(tut,pong,[]),
spawn(tut,ping,[3,Pong_PID]).
pong() ->
receive
finished ->
io:format("Pong finished~n");
{ping,Ping_PID} ->
io:format("pong received ping from pid ~w~n",[Ping_PID]),
Ping_PID ! pong,
pong()
end.
ping(0,Pong_PID) ->
Pong_PID ! finished,
io:format("ping finished ~n",[]);
ping(N,Pong_PID) ->
Pong_PID ! {ping,self()},
receive
pong ->
io:format("ping received from pid ~w~n",[Pong_PID])
end,
ping(N - 1,Pong_PID).
18> tut:start().
pong received ping from pid <0.102.0>
ping received from pid <0.101.0>
<0.102.0>
pong received ping from pid <0.102.0>
ping received from pid <0.101.0>
pong received ping from pid <0.102.0>
ping received from pid <0.101.0>
ping finished
Pong finished
19> </span>
<span style="font-size:18px;"> receive
condition ->
action1;
condition ->
action2
end</span>
Register process name :
register(atom,PID) 给当前节点注册进程名
<span style="font-size:18px;">-module(tut).
-export([start/0,pong/0,ping/1]).
start() ->
register(pong,spawn(tut,pong,[])),
spawn(tut,ping,[3]).
pong() ->
receive
finished ->
io:format("pong finished~n");
{ping,Ping_PID} ->
io:format("pong receive ping~n"),
Ping_PID ! pong,
pong()
end.
ping(0) ->
pong ! finished,
io:format("ping finished~n");
ping(N) ->
pong ! {ping,self()},
receive
pong ->
io:format("ping receive pong~n")
end,
ping(N - 1).
</span>
Distributed programming:
-module(tut).
-export([startping/1,startpong/0,ping/2,pong/0]).
startping(Pong_Node) ->
spawn(tut,ping,[3,Pong_Node]).
startpong() ->
register(pong,spawn(tut,pong,[])).
pong() ->
receive
finished ->
io:format("pong finished~n");
{ping,Ping_Pid} ->
io:format("pong receive ping~n"),
Ping_Pid ! pong,
pong()
end.
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 receive pong~n")
end,
ping(N-1,Pong_Node).
之前我们在两个分开节点分别运行,spawn可以用于在其它节点启动进程,这个例子假定ping在另个节点运行,若关闭ping节点将报错。
-module(tut).
-export([start/1,ping/2,pong/0]).
start(Ping_Node) ->
register(pong,spawn(tut,pong,[])),
spawn(Ping_Node,tut,ping,[3,node()]).
pong() ->
receive
finished ->
io:format("pong finished~n");
{ping,Ping_PID} ->
io:format("pong receive ping~n"),
Ping_PID ! pong,
pong()
end.
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 receive pong~n")
end,
ping(N-1,Pong_Node).