concept Erlang in category erlang

appears as: Erlg, Erlg
Erlang and OTP in Action

This is an excerpt from Manning's book Erlang and OTP in Action.

If you’re reading this book, you probably know already that Erlang is a programming language—and as such it’s pretty interesting in itself—but as the title of the book indicates, our focus is on the practical use of Erlang for creating real, live systems. And for that, we also need the OTP framework. This is always included in any Erlang distribution and is such an integral part of Erlang these days that it’s hard to say where the line is drawn between OTP and the plain Erlang standard libraries; hence, we often say “Erlang/OTP” to refer to either or both. Despite this close relationship, not many Erlang programmers have a clear idea of what OTP can provide or how to start using it, even if it has always been just a few keystrokes away. This book is here to help.

Erlang processes are not operating system threads. They’re much more lightweight, implemented by the Erlang runtime system, and Erlang is easily capable of spawning hundreds of thousands of processes on a single system running on commodity hardware. Each of these processes is separate from all the other processes in the runtime system; it shares no memory with the others, and in no way can it be corrupted by another process dying or going berserk.

Processes need to do more than spawn and run—they need to exchange information. Erlang makes this communication simple. The basic operator for sending a message is !, pronounced “bang,” and it’s used in the form “Destination ! Message”. This is message passing at its most primitive, like mailing a postcard. The OTP framework takes process communication to another level, and we dive into that in chapter 3; for now, let’s marvel at the simplicity of communicating between two independent and concurrent processes, as illustrated in the following listing.

Listing 1.1. Process communication in Erlang

Take a minute or two and look at this code. You can probably understand it without any previous knowledge of Erlang. Points worth noting are a variant of the spawn function that gets a single reference to “the function named ping that takes zero arguments”; and the function self(), which produces the identifier of the current process, which is passed to the new process so that it knows where to reply .

That’s Erlang’s process communication in a nutshell. Every call to spawn yields a fresh process identifier that uniquely identifies the new child process. This identifier can then be used to send messages to the child. Each process has a mailbox where incoming messages are stored as they arrive, even if the receiving process is currently busy, and the messages are kept there until the process decides to check the mailbox. It can then search and retrieve messages from the mailbox at its convenience using a receive expression, as in the example (which grabs the first available message).

Listing 4.1. Application metadata file: ebin/tcp_rpc.app
%% -*- mode: Erlang; fill-column: 75; comment-column: 50; -*- {application, tcp_rpc, [{description, "RPC server for Erlang and OTP in action"}, {vsn, "0.1.0"}, {modules, [tr_app, tr_sup, tr_server]}, {registered, [tr_sup]}, {applications, [kernel, stdlib]}, {mod, {tr_app, []}} ]}.