Lecture-5

Lecture-5 - Principles of Concurrency and Parallelism...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Principles of Concurrency and Parallelism Lecture 5: Erlang 1/31/12 CS390C: Principles of Concurrency and Parallelism Tuesday, February 7, 12 Example Joe Armstrong Suppose we have N threads (or processes, tasks) that form a ring Joe Armstrong COP COP Process creation times Each thread communicates with its neighbor forwarding a message Message passing times How big can we make the ring? How long does it take to send a message? CS390C: Principles of Concurrency and Parallelism Tuesday, February 7, 12 Philosophy Independent processes - suitable for executing on distributed machines (Deep) copy data sent on messages No sharing - no cross-machine pointers no locks, data races, synchronization issues, ... All processes have a unique name Asynchronous sends, synchronous receives - - Eventual delivery But if A sends messages m1 and m2 to B, m2 will never arrive before m1 CS390C: Principles of Concurrency and Parallelism 3 Tuesday, February 7, 12 Key features Functional - single assignment (every variable assigned to at most once) Lightweight first-class processes Pattern-matching Small collection of datatypes - lists, tuples, pairs Dynamic typing Realtime concurrent garbage collection CS390C: Principles of Concurrency and Parallelism 4 Tuesday, February 7, 12 Sequential Erlang in 5 examples 1 - Factorial 1 - Factorial -module(math). Examples -export([fac/1]). -module(math). -export([fac/1]). 0 -> N * fac(N-1); fac(N) when N > fac(0) -> 1. fac(N) when N > 0 -> N * fac(N-1); fac(0) > math:fac(25). -> 1. 15511210043330985984000000 > math:fac(25). 15511210043330985984000000 2 - Binary Tree 2lookup(Key, {Key, Val, _, _}) -> - Binary Tree {ok, Val}; lookup(Key, {Key, Val, _, _})when Key<Key1 -> lookup(Key,{Key1,Val,S,B}) -> {ok, Val}; lookup(Key, S); lookup(Key,{Key1,Val,S,B}) when Key<Key1 -> lookup(Key, {Key1,Val,S,B}) -> lookup(Key, S); lookup(Key, B); lookup(Key, {Key1,Val,S,B}) -> lookup(Key, nil) -> lookup(Key, B); not_found. lookup(Key, nil) -> CS390C: Principles of Concurrency and Parallelism not_found. Tuesday, February 7, 12 5 append([H|T], L) -> [H|append(T, L)]; append(, L) -> L. 3append([H|T], L) -> [H|append(T, L)]; - Append append(, L) -> L. 3 - Append 3 - Append Examples append([H|T], L) -> [H|append(T, L)]; 4append(, - Sort sort([Pivot|T])L) -> L. -> sort([X||X <- T, X < Pivot]) ++ sort([Pivot|T]) -> 4 - Sort [Pivot] ++ sort([X||X <- T, X < Pivot]) ++ sort([X||X <- T, X >= Pivot]); [Pivot] ++ sort([Pivot|T]) -> sort() -> . sort([X||X <- T, X >= Pivot]); sort([X||X <- T, X < Pivot]) ++ 5sort() -> . - Adder ++ [Pivot] sort([X||X <- T, X >= Pivot]); 5sort()= -> . -> fun(X) -> X + N end end. - Adder fun(N) > Adder #Fun > Adder = fun(N) -> fun(X) -> X + N end end. > G 5#Fun = Adder(10). - Adder #Fun > G = Adder(10). > G(5). > Adder = fun(N) -> fun(X) -> X + N end end. #Fun 15 #Fun > G(5). > CS390C: Principles of Concurrency and Parallelism 15 G = Adder(10). #Fun Tuesday, February 7, 12 > G(5). 4 - Sort 6 Concurrency -module(m). -export([loop/0]). loop() -> receive who_are_you -> io:format("I am ~p~n", [self()]), loop() end. 1> P = spawn(m, loop, ). <0.58.0> 2> P ! who_are_you. I am <0.58.0> who_are_you CS390C: Principles of Concurrency and Parallelism Tuesday, February 7, 12 7 Concurrency -module(counter). -export([start/0,loop/1]). start() -> spawn(counter, loop, [0]). loop(Val) -> receive increment -> loop(Val + 1) end. Issues: Cannot directly access counter value. Messaging protocol is explicit (via message increment) CS390C: Principles of Concurrency and Parallelism Tuesday, February 7, 12 8 Refinement -module(counter). -export([start/0,loop/1,increment/1,value/1,stop/1]). %% First the interface functions. start() -> spawn(counter, loop, [0]). increment(Counter) -> Counter ! increment. value(Counter) -> Counter ! {self(),value}, receive {Counter,Value} -> Value end. stop(Counter) -> Counter ! stop. loop(Val) -> receive increment -> loop(Val + 1); {From,value} -> From ! {self(),Val}, loop(Val); stop -> true; Other -> loop(Val) end. CS390C: Principles of Concurrency and Parallelism Tuesday, February 7, 12 9 Concurrency -module(M). -export([start/0, ping/1, pong/0]). ping(0) -> pong ! finished, io:format("ping finished~n", ); ping(N) -> pong ! {ping, self()}, receive pong -> io:format("Ping received pong~n", ) end, ping(N - 1). pong() -> receive finished -> io:format("Pong finished~n", ); {ping, Ping_PID} -> io:format("Pong received ping~n", ), Ping_PID ! pong, pong() end. start() -> register(pong, spawn(M, pong, )), spawn(M, ping, [3]). 10 CS390C: Principles of Concurrency and Parallelism Tuesday, February 7, 12 Distributed Programming Can generalize previous example to a distributed environment -module(M). -export([start/0, ping/1, pong/0]). ping(0,Pong_node) -> {pong, Pong_Node} ! finished, io:format("ping finished~n", ); ping(N) -> {pong, Pong_Node} ! {ping, self()}, receive pong -> io:format("Ping received pong~n", ) end, ping(N - 1, Pong_Node). pong() -> receive finished -> io:format("Pong finished~n", ); {ping, Ping_PID} -> io:format("Pong received ping~n", ), Ping_PID ! pong, pong() end. start_pong() -> register(pong, spawn(M, pong, )), start_ping(Pong_Node) -> spawn(M, ping, [3, Pong_Node]). CS390C: Principles of Concurrency and Parallelism 11 On one host: erl -sname ping On another: erl -sname pong On one node: M:start_pong(). On another: M:start_ping(pong@<host>). Tuesday, February 7, 12 Joe Armstrong Monitoring COP 10 - Monitor a process ... process_flag(trap_exit, true), Pid = spawn_link(fun() -> ... end), receive {'EXIT', Pid, Why} -> ... end CS390C: Principles of Concurrency and Parallelism Tuesday, February 7, 12 12 A Auniversal Client - -Server with hot code universal Client Server with hot code swapping :-):-) swapping server(Fun, Data) -> server(Fun, Data) -> receive receive {new_fun, Fun1} -> {new_fun, Fun1} -> server(Fun1, Data); server(Fun1, Data); {rpc, From, ReplyAs, Q} -> {rpc, From, ReplyAs, Q} -> {Reply, Data1} = = Fun(Q, Data), {Reply, Data1} Fun(Q, Data), From ! ! {ReplyAs, Reply}, From {ReplyAs, Reply}, server(Fun, Data1) server(Fun, Data1) end. end. rpc(A, B) -> rpc(A, B) -> Tag = = new_ref(), Tag new_ref(), A A ! {rpc, self(), Tag, B}, ! {rpc, self(), Tag, B}, receive receive {Tag, Val} -> Val {Tag, Val} -> Val end end CS390C: Principles of Concurrency and Parallelism Tuesday, February 7, 12 13 Client/Server Joe Armstrong Common concurrency patterns: Programming Patterns Concurrency Patterns Event Event receive A -> A end receive A -> A end RPC call Call (RPC) Call (RPC) A ! {self(), A ! {self(), receive receive {A, Reply} {A, Reply} Reply Reply end end Callback Cast Cast ACOP B ! A ! B Unicast Common concurrency patterns: Cast A ! B Event receive A -> Cast A ! B Call Handling (RPC) Event A ! Event {self(), receive receive A -> {A, Reply} Reply Call (RPC) end A ! {self(), A end B}, B}, -> -> B}, A end -> B}, Callback Callback receive receive {From, {From, From From end end A} -> A} -> ! F(A) ! F(A) receive Distributed Systems Laboratory CallbackReply} -> {A, Distributed Systems Laboratory receive Reply 14 {From, A} -> CS390C: Principles of Concurrency and Parallelism end From ! F(A) end Tuesday,Callback February 7, 12 Can we easily program tricky concurrency patterns? Concurrency Patterns Callback within RPC Callback within RPC A ! {Tag, X}, g(A, Tag). g(A, Tag) -> receive {Tag, Val} -> Val; {A, X} -> A ! F(X), go(A, Tag) end. Parallel RPC par_rpc([A,B,C], M) 15 22 CS390C: Principles of Concurrency and Parallelism Distributed Systems Laboratory Tuesday, February 7, 12 Timeouts receive Message1 [when Actions1 ; Message2 [when Actions2 ; ... after TimeOutExpr -> ActionsT end get_event() -> receive {mouse, click} -> receive {mouse, click} -> double_click after double_click_interval() -> single_click end ... end. CS390C: Principles of Concurrency and Parallelism Tuesday, February 7, 12 16 Readings Concurrent Programming in Erlang (Part 1) http://www.erlang.org/download/erlang-book-part1.pdf See also: www.erlang.org CS390C: Principles of Concurrency and Parallelism Tuesday, February 7, 12 17 ...
View Full Document

This note was uploaded on 03/26/2012 for the course CS 390 taught by Professor Staff during the Spring '08 term at Purdue.

Ask a homework question - tutors are online