Skip to content

Instantly share code, notes, and snippets.

@hhimanshu
Created December 17, 2014 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhimanshu/117bd0fe9a7a078fbc7c to your computer and use it in GitHub Desktop.
Save hhimanshu/117bd0fe9a7a078fbc7c to your computer and use it in GitHub Desktop.
Create 2 process and send M messages back and forth between them
% http://www.erlang.org/course/exercises.html#conc
-module(process).
-export([loop/0]).
loop() ->
receive
{From, Message} -> io:format("received [~p] from [~p]~n", [Message, From]),
loop()
end.
-module(process_function).
-compile(export_all).
start(M) ->
P1 = spawn(process, loop, []),
P2 = spawn(process, loop, []),
io:format("instantiated process ~p~n", [P1]),
io:format("instantiated process ~p~n", [P2]),
message(P1, P2, M),
message(P2, P1, M).
message(From, To, M) ->
[To ! {From, X} || X <- lists:seq(1,M)].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment