跳到主要內容

Concurrent Programming II

1.Concurrency is easy…?

( http://armstrongonsoftware.blogspot.com/2006/08/concurrency-is-easy.html )


2.Sequential Programming

-module(factorial).
-export([factoriala/1, factorialb/1, factorialc/1, factoriald/1]).


%Simplest:
factoriala(0) -> 1;
factoriala(N) -> N * factoriala(N - 1).


%Using function guards:
factorialb(0) -> 1;
factorialb(N) when N > 0 -> N * factorialb(N - 1).


%Using if:
factorialc(N) ->
if
N == 0 -> 1;
N > 0 -> N * factorialc(N - 1)
end.


%Using case:
factoriald(N) ->
case N of
0 -> 1;
N when N > 0 -> N * factoriald(N - 1)
end.



-module(set).
-export([new/0, add_element/2, del_element/2,is_element/2, is_empty/1, union/2, intersection/2]).

new() -> [].

is_element(H, [H|_]) -> true;
is_element(H, [_|Set]) -> is_element(H, Set);
is_element(_, []) -> false.


is_empty([]) -> true;
is_empty(_) -> false

add_element(X, Set) ->
case is_element(X, Set) of
true -> Set;
false -> [X|Set]
end.


del_element(X, [X|T]) -> T;
del_element(X, [Y|T]) -> [Y|del_element(X,T)];
del_element(_, []) -> [].


union([H|T], Set) -> union(T, add_element(H, Set));
union([], Set) -> Set.


intersection(S1, S2) -> intersection(S1, S2, []).
intersection([], _, S) -> S;
intersection([H|T], S1, S) ->
case is_element(H,S1) of
true -> intersection(T, S1, [H|S]);
false -> intersection(T, S1, S)
end.


3.concurrent programming

Processes and communication between processes are fundamental concepts in Erlang.

1. Process Create

Pid = spawn(Module, FunctionName, ArgumentList)

2. Inter-process Communication

Pid ! Message

receive
Message1 [when Guard1] ->Actions1 ;
Message2 [when Guard2] ->Actions2 ;
_AnyMessage -> Actions3 ;
after Timeout ->Action4
...
end.

3.Receiving messages from a specific process

Pid ! {self(),abc}

receive
{Pid,Msg} ->
...
end


4. Examples

a)
-module(cfac).
-export([start/0,loop/0]).
-import(factorial,[factoriala/1]).

start() ->spawn(counter, loop, []).

loop() ->
receive
{fac,Val}-> io:format("(from:~p)fac ~p=~p ~n",[node(),Val,
factoriala(Val)]),loop();
{exit}->io:format("end~n",[]);
_->loop()
end.


b) performance testing


c) programming with socket
-module(geturl).
-export([start/2,senddata/1,dataarrive/1,startN/3]).
-define(TCP_OPTIONS,[list, {packet, 0}, {active, false}, {reuseaddr,
true}]).

start(Url,Port)->
case gen_tcp:connect(Url,Port ,?TCP_OPTIONS ) of
{ok,CSocket}->geturl:senddata(CSocket);
{error,Why}->Why
end.

senddata(Socket)->
Data="GET / HTTP/1.0\r\nConnection: close\r\nCache-Control:
no-cache\r\n\r\n",
io:format("~p~n",[Socket]),

case gen_tcp:send(Socket,Data) of
ok->io:format("send:~p~n",[Data]),dataarrive(Socket);
{error, Why}->io:format("send_socket close:~p~n",[Why])
end.

dataarrive(Socket)->
case gen_tcp:recv(Socket, 0) of
{ ok, Data} ->io:format("receive:~p~n",[Data]),
dataarrive(Socket);
{error, Why} ->io:format("dataarrive_socket
close:~p~n",[Why])
end.

startN(_,_,0)->io:format("~p~n",["end"]);
startN(Url,Port,Num)->spawn(geturl,start,[Url,Port]),
startN(Url,Port,Num-1).


d) Distributes Programming

erl –sname local
erl –sname remote

Pid=rpc:call(remote@se6,cfac,start,[]).
Pid ! {fac,10}.



5.References

[1] J. Armstrong, R. Virding, C. Wikstr¨om, and M. Williams. Concurrent Programming in Erlang.
[2] J. Armstrong.Erlang Programming.

留言

這個網誌中的熱門文章

怎麼在網路上註冊成為youbike 會員?

新版官網請參考  怎麼在網路上註冊成為youbike 會員?   http://rd-program.blogspot.tw/2014/04/youbike.html 網路的申請步驟類似,下面將以網路申請來說明申請步驟:申請的時候需要準備悠遊卡、或晶片信用卡,以及手機門號。 1. 請先登入ubike網址: http://www.youbike.com.tw/ ,登入後選擇【正體中文】,要選英文也可以啦! 2. 在螢幕的右上角選擇【註冊】。 3.點擊【開始註冊】。 4. 點擊【同意】。(沒有其他選擇?) 5. 輸入您的【手機號碼】以及【認證碼】,然後按【送出】。這時候手機會收到ubike傳來的簡訊,通之驗證碼,有四個阿拉伯數字。 6. 輸入帳號(手機號碼)、驗證碼(ubike傳到手機的簡訊)、密碼,然後按【下一步】。 7. 還沒完成喔!這裡告訴你如何租車及還車的步驟。把螢幕拉到最下面,記得勾選【我已清楚瞭解租還車步驟】,然後按【下一步】。 8. 選擇悠遊卡或是晶片信用卡,然後輸入卡片號碼,卡片號碼請參卡畫面又下方的提示位置,請注意有些卡號可能已經模糊不清,可能無法輸入。每隻手機不只可以輸入一個卡號。 9. 填寫個人姓名及Email帳號,如果不想收到相關訊息就把前面的打勾取消。按【確認】按鈕。 10. 恭喜您註冊成功,可已開始使用YouBike了。

3分鐘學會使用程式發送msn訊息,php篇(一)

---電子發票整合方案 http:// rd-program.blogspot.com/ 2012/03/blog-post.html --- < imoo msn機器人測試平台,將於下週三(2009.6.24)取消所有未經申請試用的認證資訊 , 詳見相關資訊 > msnSDK訊息控制開發套件 同時支援MSN/Yahoo即時通 訊息雙向傳遞 ps.也請參考 msnSDK的使用流程 ================================ 步驟一:取得api 使用的權限(GETSPID) $file="http://59.120.234.84:8082/msnSDK/msn_cgi-win32?FUNC=GETSPID&USERID=apiblogt1&PASSWD=msnsdkt" ; $myArray=''; $mySession=''; $myResult=''; $getline=''; if (!($fp = fopen($file, "r"))) { die("could not open XML input"); } while ($getline = fgets($fp, 4096)) { $myArray=explode("\t",$getline); $myResult=$myArray[0]; $mySession= $myArray[1]; } fclose($fp); if ($myResult=="0") { echo "get session fail... "; exit(); } else { //$_SESSION["mySession"]= $mySession; echo "get session=".$mySession." "; } echo "<a href='REGISTER.php?uids=".$_GET["uids...