Erlang (and Elixir) Process Primitives

parent = self
:io.format("parent pid ~p~n", [parent])
child_pid = spawn(fn ->
:io.format("child process ~p~n", [self])

:timer.sleep 5_000 # sleep for 5 seconds

:io.format("Child done ~p~n", [self])
send parent, {self, :dead}
end)
receive do
msg -> IO.inspect msg
end

Understanding Erlang Processes

proc_a = spawn(fn ->
:io.format("hello from proc A")
:io.format("Proc A is now going to wait for a message")
receive do
msg ->
IO.puts "proc A received message"
IO.inspect msg
end

IO.puts "Proc A is now ending"
end)
proc_b = spawn(fn ->
:io.format("hello from proc B")
:io.format("Proc Bis now going to wait for a message")
receive do
msg ->
IO.puts "proc Breceived message"
IO.inspect msg
end

IO.puts "Proc B is now ending"
end)

Process Communication

Linking Processes

proc_a = spawn(fn ->
:io.format("proc A: ~p~n", [self])
Process.register self, :proc_a
proc_b = spawn_link(fn ->
:io.format("proc B: ~p~n", [self])
Process.register self, :proc_b
receive do
end
end)
:io.format("proc A spawn_linked B at: ~p~n", [proc_b]) receive do
end
end)
:timer.sleep 1_000
IO.puts "killing proc B"
proc_b_pid = Process.whereis :proc_b
Process.exit proc_b_pid, :kill
:timer.sleep 100
IO.puts("Proc A: alive? #{Process.alive?(proc_a)}")
receive do
end

Monitoring Processes

proc_a_pid = spawn_link(fn ->
IO.puts "hello from A"
Process.register self, :proc_a
{mon, proc_b_pid} = spawn_monitor(fn ->
IO.puts "hello from B"
:io.format("procB pid: ~p~n", [self])
Process.register self, :proc_b
receive do
end
end)
receive do
msg ->
IO.puts "proc A received msg"
IO.inspect msg
end
receive do
end
end)
:timer.sleep 500
proc_b_pid = Process.whereis :proc_b
Process.exit proc_b_pid, :kill
:timer.sleep 100
proc_a_alive = Process.alive? proc_a_pid
IO.puts "Proc A alive?: #{proc_a_alive}"
{mon, proc_b_pid} = spawn_monitor(fn ->
{:DOWN, #Reference<0.4279228456.2173435907.188346>, :process, #PID<0.143.0>,
:killed}

Summary

  • spawn
  • send
  • receive
  • spawn_link
  • spawn_monitor

--

--

--

A96B F906 823F 22CA 63B9 CBE4 7025 A470 FFBF E06F

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Business records, spreadsheet bookkeeping. Who’s profit?

Deploying applications with ArgoCD in Multiple clusters

Day 1: HTML

Hackerrank DP: Equal

Create Pixel Art with CSS

AR Service Dog Simulation

Migrating Java CI to CD ontoAWS: Part 1: The Plan

Hacktoberfest’18

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Santosh Kumar

Santosh Kumar

A96B F906 823F 22CA 63B9 CBE4 7025 A470 FFBF E06F

More from Medium

F#: Documenting Domain with Unions and Pattern Matching

Erlang/OTP profiling: eprof

Beam me up!