% $Id: concurrencydemo.erl,v 1.1 2013/04/09 13:07:16 leavens Exp leavens $ % AUTHOR: Gene Sher -module(concurrencydemo). -compile(export_all). concurrent()-> spawn(?MODULE,loop,[[]]). loop(State)-> receive {PId,Msg}-> g(Msg), loop([{PId,Msg,now()}|State]); terminate -> io:format("Terminating process:~p~n",[self()]); %Msg -> %io:format("Msg format unknown:~p~n",[Msg]), %loop() check_inbox -> check(); check_state -> io:format("Current state:~p~n",[State]), loop(State) after 100000 -> io:format("Time:~p~n",[time()]), loop(State) end. check()-> receive MSG -> io:format("INbox msg:~p~n",[MSG]), check() after 0 -> io:format("Done~n") end. g(Obj)-> case Obj of {circle,R}-> io:format("Area of circle:~p~n",[math:pi()*R*R]); {rectang,W,X}-> io:format("Area of rectangle:~p~n",[W*X]) end. bunch_of_processes(Val)-> _Pids = [spawn(?MODULE,n,[])|| _<-lists:seq(1,Val)]. n()-> io:format("Printing now the time and self():~p~n",[{self(),time()}]). prep()-> spawn(?MODULE,current_time,[]). current_time()-> receive time_now -> io:format("Current:~p~n",[time()]) end. p_fact(Val)-> [spawn(?MODULE,factor,[Val,self()])|| _<-lists:seq(1,10)], gather([]). gather(Acc)-> receive {result,Result}-> io:format("Received result:~p~n",[Result]), gather([Result|Acc]) after 10000 -> io:format("Results:~p~n",[Acc]) end. factor(Val,Supervisor_Pid)-> factor(Val,Supervisor_Pid,1). factor(0,Supervisor_Pid,Acc)-> Supervisor_Pid ! {result,Acc}; factor(Val,Supervisor_Pid,Acc)-> factor(Val-1,Supervisor_Pid,Acc*Val). general_f()-> receive {become,Module,Function,Arg}-> Module:Function(Arg); {circle, R}-> io:format("Area:~p~n",[R*R*math:pi()]), ?MODULE:general_f(); potato -> done after 1000 -> io:format("Modified2:~p~n",[time()]), ?MODULE:general_f() end. rpc(Pid,Msg)-> Pid ! Msg, receive {Pid,Reply} -> io:format("Reply:~p~n",[Reply]) end.