O'Reilly logo

Introducing Erlang, 2nd Edition by Simon St. Laurent

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 8. Playing with Processes

While Erlang is a functional language, Erlang programs are rarely structured around simple functions. Instead, Erlang’s key organizational concept is the process, an independent component (built from functions) that sends and receives messages. Programs are deployed as sets of processes that communicate with each other. This approach makes it much easier to distribute work across multiple processors or computers, and also makes it possible to do things like upgrade programs in place without shutting down the whole system.

But taking advantage of those features means learning how to create (and end) processes, how to send messages among them, and how to apply the power of pattern matching to incoming messages.

The Shell Is a Process

You’ve been working within a single process throughout this book so far, the Erlang shell. None of the previous examples sent or received messages, of course, but the shell is an easy place to send and (for test purposes, at least) receive messages.

The first thing to explore is the process identifier, often called a pid. The easiest pid to get is your own. In the shell you can just run the self() function:

1> self().
<0.36.0>

<0.36.0> is the shell’s representation of a triple, a set of three integers that provide the unique identifier for this process. (You will probably get a different set of numbers when you try it, though.) This group of numbers is guaranteed to be unique within this run of Erlang, but are not ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required