LibraryProcess Identifiers

Process Identifiers

Learn about Process Identifiers as part of Elixir Functional Programming and Distributed Systems

Understanding Process Identifiers (PIDs) in Elixir

In Elixir, concurrency is managed through lightweight processes. Each process has a unique identifier, known as a Process Identifier (PID). PIDs are fundamental to how processes communicate and interact within the Elixir runtime and across distributed systems. Think of them as the unique 'address' for each running process.

What is a Process Identifier (PID)?

A PID is a tuple that uniquely identifies a process within the Elixir virtual machine (BEAM). It typically looks like this:

code
{, , }
. For processes running on the local node, the node name is often omitted or represented by a special atom like
code
:nonode@nohost
.

PIDs are unique addresses for Elixir processes.

Each Elixir process, whether it's a GenServer, a simple spawned function, or part of a Supervisor tree, is assigned a unique PID. This PID is crucial for sending messages to that specific process.

When you spawn a new process using spawn/1, spawn/2, spawn_link/1, or spawn_link/2, the function returns the PID of the newly created process. This PID is then used as the destination for sending messages using the send/2 function or the ! operator. For example, send(pid, {:message, data}) or pid ! {:message, data}.

What is the primary purpose of a PID in Elixir?

To uniquely identify a process and serve as its address for message passing.

How PIDs Work with Message Passing

Elixir's concurrency model is based on the Actor Model, where processes communicate by sending immutable messages to each other. PIDs are the keys to this communication. When you send a message to a PID, the Elixir runtime ensures that the message is delivered to the mailbox of the process associated with that PID.

Imagine a postal system. Each house has a unique address (the PID). To send a letter (a message) to someone, you need their specific address. The postal worker (the Elixir runtime) uses this address to deliver the letter to the correct mailbox (the process's mailbox). The PID is the essential piece of information that directs the message to its intended recipient process.

📚

Text-based content

Library pages focus on text content

Several built-in functions in Elixir interact with PIDs:

FunctionDescriptionExample Usage
self/0Returns the PID of the current process.pid = self()
spawn/1Spawns a new process and returns its PID.pid = spawn(fn -> IO.puts("Hello from new process!") end)
send/2Sends a message to a process identified by its PID.send(pid, {:hello, "world"})
Process.alive?/1Checks if a process with the given PID is still alive.Process.alive?(pid)
Process.whereis/1Looks up a process registered with a name using Registry or Agent.Process.whereis(MyAgent)

PIDs in Distributed Systems

When working with distributed Elixir applications (using modules like

code
Node
and
code
Distributed
), PIDs become even more critical. A PID can include the node name, allowing you to send messages to processes running on different machines in your cluster. This is the foundation for building fault-tolerant and scalable distributed systems with Elixir and OTP.

Remember: PIDs are immutable and globally unique within a running Elixir cluster. This global uniqueness is key for reliable distributed communication.

Practical Example: Sending a Message

Let's see a simple example:

elixir
defmodule MyProcess do
def start do
spawn(fn -> loop end)
end
def loop do
receive do
{:hello, sender_pid} ->
send(sender_pid, {:hi, self()})
loop()
{:exit, _} ->
:ok
end
end
end
# In iex:
my_pid = MyProcess.start()
# => #PID<0.100.0>
send(my_pid, {:hello, self()})
# => {:hello, #PID<0.99.0>}
receive do
{:hi, process_pid} ->
IO.puts "Received hi from #{inspect(process_pid)}"
end
# => Received hi from #PID<0.100.0>
# To stop the process:
send(my_pid, {:exit, :now})

In this example,

code
self()
is used to get the PID of the process that initiated the message, allowing the spawned process to reply.

Learning Resources

Elixir Documentation: Processes(documentation)

The official Elixir documentation provides a comprehensive overview of processes, PIDs, and message passing.

Elixir School: Processes(tutorial)

A beginner-friendly tutorial that explains the core concepts of Elixir processes and how they work.

Understanding Elixir Processes and PIDs(video)

A video tutorial that visually explains the concept of Elixir processes and their identifiers.

Elixir OTP: Processes, PIDs, and Mailboxes(video)

This video delves into the relationship between processes, PIDs, and mailboxes in the context of Elixir's OTP framework.

Elixir in Action: Processes(blog)

While a book, this link often leads to sample chapters or related blog posts that cover Elixir processes in detail.

The Elixir Way: Concurrency(blog)

An article discussing Elixir's approach to concurrency, highlighting the role of processes and PIDs.

Process (computing)(wikipedia)

A general overview of what a process is in computing, providing context for Elixir's implementation.

Elixir PID - What is it and how to use it?(blog)

A Reddit discussion thread where Elixir developers share insights and practical advice on using PIDs.

Elixir's `spawn` function(documentation)

Direct link to the documentation for the `spawn/1` function, which is central to creating new processes and obtaining PIDs.

Elixir's `send` function(documentation)

The official documentation for the `send/2` function, illustrating how messages are sent to PIDs.