Erlang Processes

From Elixir Wiki
Jump to navigation Jump to search

Erlang Processes[edit]

File:Elixir Erlenmeyer flask.png
Elixir language logo, inspired by the Erlenmeyer flask.

Erlang processes are a fundamental building block in the Elixir programming language, providing lightweight concurrent programming capabilities. They are not operating system processes but rather lightweight entities managed by the Erlang virtual machine (BEAM).

Overview[edit]

Erlang processes in Elixir provide a concurrent and fault-tolerant mechanism for executing code independently. Each process has its own mailbox to receive messages and can spawn new processes, creating a tree-like structure known as the process tree.

Key Features[edit]

The key features of Erlang processes in Elixir include:

Concurrency[edit]

Erlang processes allow for the execution of multiple code paths simultaneously, reducing the need for complex synchronization mechanisms.

Fault-tolerance[edit]

When a process crashes due to an exception, it can be easily restarted or terminated without affecting other processes in the system.

Message Passing[edit]

Processes communicate with each other by sending and receiving messages asynchronously. Messages are received in the order they were sent and are stored in the receiving process's mailbox until processed.

Process Linking[edit]

Processes can be linked together, creating a relationship where if one process terminates abnormally, the other linked processes are also terminated.

Process Monitoring[edit]

In addition to process linking, Elixir provides process monitoring capabilities, enabling processes to monitor the status of other processes and take appropriate actions in case of failure.

Spawn and Creating Processes[edit]

In Elixir, new processes are spawned using the `spawn/1` or `spawn/3` functions. The `spawn/1` function takes a function as an argument and creates a new process that executes the provided function.

Example: ```elixir spawn(fn ->

 IO.puts "Hello, world!"

end) ```

Sending and Receiving Messages[edit]

Elixir provides the `send/2` function to send messages to other processes. The messages are then stored in the receiving process's mailbox until processed using the `receive/1` or `receive/2` functions.

Example: ```elixir send(pid, :message) ```

```elixir receive do

 message -> IO.puts "Received: #{message}"

end ```

Process Monitoring[edit]

Elixir allows processes to monitor the status of other processes using the `Process.monitor/1` function. The monitoring process will receive an asynchronous message when the monitored process terminates.

Example: ```elixir pid = spawn(fn ->

 raise "Oops!"

end)

ref = Process.monitor(pid)

receive do

 {:DOWN, ^ref, :process, ^pid, _reason} -> IO.puts "Process terminated"

end ```

Conclusion[edit]

Erlang processes are a powerful feature of the Elixir programming language, offering lightweight concurrency, fault-tolerance, and message passing capabilities. Understanding how to create, monitor, and communicate between processes is crucial for building robust and concurrent Elixir applications.

See Also[edit]

References[edit]

Template:Reflist