lukeshei 发表于 2013-2-4 19:48:09

Concurrent Programming II

1.Concurrency is easy…?

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


2.Sequential Programming

-module(factorial).
-export().


%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() -> [].

is_element(H, ) -> true;
is_element(H, ) -> 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 ->
end.


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


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


intersection(S1, S2) -> intersection(S1, S2, []).
intersection([], _, S) -> S;
intersection(, S1, S) ->
case is_element(H,S1) of
                      true -> intersection(T, S1, );
                      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 ->Actions1 ;
         Message2 ->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().
       -import(factorial,).

       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().
      -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",),
      
      case gen_tcp:send(Socket,Data) of
             ok->io:format("send:~p~n",),dataarrive(Socket);
             {error, Why}->io:format("send_socket close:~p~n",)
      end.                  

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

      startN(_,_,0)->io:format("~p~n",["end"]);
      startN(Url,Port,Num)->spawn(geturl,start,),                     
                        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

J. Armstrong, R. Virding, C. Wikstr¨om, and M. Williams. Concurrent Programming in Erlang.
J. Armstrong.Erlang Programming.
页: [1]
查看完整版本: Concurrent Programming II