Skip to content

Instantly share code, notes, and snippets.

@fmamud
Created October 27, 2015 18:44
Show Gist options
  • Save fmamud/6f99587967bc64143c6f to your computer and use it in GitHub Desktop.
Save fmamud/6f99587967bc64143c6f to your computer and use it in GitHub Desktop.
Erlang ping & pong example
-module(pingpong).
-export([start/0, ping/2, pong/0]).
ping(0, Pong_PID) ->
Pong_PID ! finished,
io:format("Ping finished~n", []);
ping(N, Pong_PID) ->
Pong_PID ! {ping, self()},
receive
pong ->
io:format("Ping received pong~n", [])
end,
ping(N - 1, Pong_PID).
pong() ->
receive
finished ->
io:format("Pong finished~n", []);
{ping, Ping_PID} ->
io:format("Pong received ping~n", []),
Ping_PID ! pong,
pong()
end.
start() ->
Pong_PID = spawn(sample3, pong, []),
spawn(sample3, ping, [3, Pong_PID]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment