我的雲端生活網 - Life+

Sunday, December 16, 2007

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.

No comments:

Blog Archive