Skip to content

Instantly share code, notes, and snippets.

@johntyree
Created May 12, 2011 09:35
Show Gist options
  • Save johntyree/968250 to your computer and use it in GitHub Desktop.
Save johntyree/968250 to your computer and use it in GitHub Desktop.
#!/usr/bin/env escript
-define(DELAY, 1000).
main(_Depth) ->
S = self(),
spawn(fun() -> keepGoing(50, S, 0) end),
spawn(fun() -> keepGoing(50, S, 0) end),
X = collectKids(),
io:format("\p\n", [X]),
showValues(X),
ok.
collectKids() -> collectKids([]).
collectKids(Acc) ->
receive
{Pid,Sent} -> collectKids([{Pid, Sent, epochtime()}|Acc])
after ?DELAY ->
io:format("~p~n", [Acc]),
lists:foreach(
fun({Pid, Sent, Recv}) ->
{Pid, Recv - Sent}
end,
Acc
)
end.
keepGoing(Depth, Parent, N) ->
if N < Depth ->
S = self(),
spawn(fun() -> keepGoing(Depth, S, N+1) end),
receive
Pid -> Parent ! Pid
end;
true ->
Parent ! themsg()
end.
themsg() -> {self(), epochtime()}.
epochtime() ->
{Ms, S, Us} = now(),
Ms*1000000*1000 + S*1000 + Us.
showValues( Values ) ->
lists:foreach(
fun(I)-> io:format("~p\t", [I]) end,
Values
),
io:format("\n").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment