Who sends/receives messages? •other processes (obviously) •open files •open sockets •external programs (Ports) •Erlang VM notifications •a process you watched died •a remote node you watched died
defmodule
PingPong
do
def
start
do
:ok
=
:proc_lib.start_link(__MODULE__,
:init,
[:ping,
:pong])
:ok
=
:proc_lib.start_link(__MODULE__,
:init,
[:pong,
:ping])
:ping
<-‐
1
end
def
init(my_name,
other_name)
do
Process.register(self(),
my_name)
:proc_lib.init_ack(:ok)
loop(my_name,
other_name)
end
def
loop(my_name,
other_name)
do
receive
do
count
-‐>
other_name
<-‐
(count
+
1)
if
count
>
1000
do
IO.puts("#{my_name}
#{count}")
else
loop(my_name,
other_name)
end
end
end end
2000 reductions left: calculate(something) 1000 reductions left: still calculating(something) 0 reductions left: suspend process run next process green threads = cheap context switching