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:
{, , }
:nonode@nohost
PIDs are unique addresses for Elixir processes.
Each Elixir process, whether it's a GenServer, a simple spawn
ed 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}
.
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
Key Functions Related to PIDs
Several built-in functions in Elixir interact with PIDs:
Function | Description | Example Usage |
---|---|---|
self/0 | Returns the PID of the current process. | pid = self() |
spawn/1 | Spawns a new process and returns its PID. | pid = spawn(fn -> IO.puts("Hello from new process!") end) |
send/2 | Sends a message to a process identified by its PID. | send(pid, {:hello, "world"}) |
Process.alive?/1 | Checks if a process with the given PID is still alive. | Process.alive?(pid) |
Process.whereis/1 | Looks 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
Node
Distributed
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:
defmodule MyProcess dodef start dospawn(fn -> loop end)enddef loop doreceive do{:hello, sender_pid} ->send(sender_pid, {:hi, self()})loop(){:exit, _} ->:okendendend# 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,
self()
Learning Resources
The official Elixir documentation provides a comprehensive overview of processes, PIDs, and message passing.
A beginner-friendly tutorial that explains the core concepts of Elixir processes and how they work.
A video tutorial that visually explains the concept of Elixir processes and their identifiers.
This video delves into the relationship between processes, PIDs, and mailboxes in the context of Elixir's OTP framework.
While a book, this link often leads to sample chapters or related blog posts that cover Elixir processes in detail.
An article discussing Elixir's approach to concurrency, highlighting the role of processes and PIDs.
A general overview of what a process is in computing, providing context for Elixir's implementation.
A Reddit discussion thread where Elixir developers share insights and practical advice on using PIDs.
Direct link to the documentation for the `spawn/1` function, which is central to creating new processes and obtaining PIDs.
The official documentation for the `send/2` function, illustrating how messages are sent to PIDs.