Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Life of an Erlang process

Life of an Erlang process

A high-level introduction to the Erlang programming language: among other topics, the talk covers Erlang processes, message passing, supervision trees, the let-it-crash approach and more.
We unfold the skeleton of an Erlang process, explaining how the functional model makes the implementation of Erlang processes straightforward. We reveal the recipe of the secret Erlang sauce which makes possible the creation of reliable, scalable systems which never go down.

C20f7f4a8aa455aa6e0c2a0092883cd0?s=128

Roberto Aloi

March 28, 2015
Tweet

Transcript

  1. None
  2. Life of an Erlang Process @robertoaloi

  3. None
  4. Let’s talk about Erlang

  5. I won’t talk about the Erlang syntax.

  6. Syntax is Irrelevant Programming Language is Not When you’ll look

    at the syntax, it’ll be too late.
  7. ERLANG HAS BEEN DESIGNED TO HELP YOU WRITING SYSTEMS SCALABLE

  8. ERLANG HAS BEEN DESIGNED TO HELP YOU WRITING SYSTEMS FAULT-TOLERANT

  9. ERLANG HAS BEEN DESIGNED TO HELP YOU WRITING SYSTEMS HIGHLY

    AVAILABLE
  10. ERLANG HAS BEEN DESIGNED TO HELP YOU WRITING SYSTEMS MASSIVELY

    CONCURRENT
  11. ERLANG HAS BEEN DESIGNED TO HELP YOU WRITING SYSTEMS DISTRIBUTED

  12. ERLANG HAS BEEN DESIGNED TO HELP YOU WRITING SYSTEMS SOFT

    REAL-TIME
  13. The Erlang Rationale Users as Processes Scalable System Tons of

    Users Tons of Processes
  14. Think Erlang Think Processes

  15. Tons of them.

  16. DEALING WITH TONS OF PROCESSES

  17. Cheap to create Cheap to context switch OS processes out

    of discussion Use lightweight processes Built-in distribution to horizontally scale
  18. SHARED MEMORY (or lack thereof)

  19. Shared memory can leave the system in an inconsistent state

    after a restart or crash. Avoid it.
  20. FAIL FAST

  21. Faults are everywhere, you cannot prevent them. Report failure and

    die.
  22. Processes are good. Can you show me one?

  23. <0.42.0>

  24. How are processes made? This is a little embarrassing, but…

    Ahem… I’m not sure I understand everything of it… I’ve heard rumours, still… Do you, do you mind if I ask you…
  25. spawn(math, fact, [5]) math:fact(5) Pid2 Pid1

  26. MESSAGE PASSING

  27. Pid2 Pid1 Pid2 ! {self(), Msg} {Pid1, Msg} receive {From,

    start} -> … {From, stop} -> … end Send Receive
  28. THE PROCESS SKELETON

  29. start(Args) -> spawn(server, init, [Args]) init(Args) -> State = do_init(Args),

    loop(State). loop(State) ->
 receive
 {handle, Msg} -> 
 NewState = handle(Msg, State),
 loop(NewState);
 stop -> terminate(State)
 end. terminate(State) -> clean_up(State). Start Stop Initialize Terminate Loop
  30. LINKS

  31. PidB PidA link(PidB)

  32. EXIT SIGNALS

  33. PidA PidB {'EXIT', PidA, Reason} PidC {'EXIT', PidB, Reason}

  34. TRAPPING EXIT SIGNALS

  35. process_flag(trap_exit, true) PidA {'EXIT', PidA, Reason} PidC PidB

  36. SUPERVISORS

  37. PidA PidC PidB Supervisor Workers

  38. erlang.org github.com/erlang/otp erlang-solutions.com www.learnyousomeerlang.org Erlang Programming Erlang and OTP in

    Action Official Home Page Sources Binary Packages, News, Events Best Online Tutorial Best book about basics Best book about OTP Where to Start
  39. Questions? @robertoaloi Cover Image: “How computers are made” (Reddit -

    Ness4114)