Erlang学习四


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).



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MyObject-C

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值